|
| 1 | +name: Container |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "develop" |
| 7 | + - "main" |
| 8 | + - "releases/**/*" |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - "develop" |
| 12 | + - "main" |
| 13 | + |
| 14 | +env: |
| 15 | + CARGO_TERM_COLOR: always |
| 16 | + |
| 17 | +jobs: |
| 18 | + test: |
| 19 | + name: Test (Docker) |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + strategy: |
| 23 | + matrix: |
| 24 | + target: [debug, release] |
| 25 | + |
| 26 | + steps: |
| 27 | + - id: setup |
| 28 | + name: Setup Toolchain |
| 29 | + uses: docker/setup-buildx-action@v3 |
| 30 | + |
| 31 | + - id: build |
| 32 | + name: Build |
| 33 | + uses: docker/build-push-action@v5 |
| 34 | + with: |
| 35 | + file: ./Containerfile |
| 36 | + push: false |
| 37 | + load: true |
| 38 | + target: ${{ matrix.target }} |
| 39 | + tags: torrust-tracker:local |
| 40 | + cache-from: type=gha |
| 41 | + cache-to: type=gha |
| 42 | + |
| 43 | + - id: inspect |
| 44 | + name: Inspect |
| 45 | + run: docker image inspect torrust-tracker:local |
| 46 | + |
| 47 | + - id: checkout |
| 48 | + name: Checkout Repository |
| 49 | + uses: actions/checkout@v4 |
| 50 | + |
| 51 | + - id: compose |
| 52 | + name: Compose |
| 53 | + run: docker compose build |
| 54 | + |
| 55 | + context: |
| 56 | + name: Context |
| 57 | + needs: test |
| 58 | + runs-on: ubuntu-latest |
| 59 | + |
| 60 | + outputs: |
| 61 | + continue: ${{ steps.check.outputs.continue }} |
| 62 | + type: ${{ steps.check.outputs.type }} |
| 63 | + version: ${{ steps.check.outputs.version }} |
| 64 | + |
| 65 | + steps: |
| 66 | + - id: check |
| 67 | + name: Check Context |
| 68 | + run: | |
| 69 | + if [[ "${{ github.repository }}" == "torrust/torrust-tracker" ]]; then |
| 70 | + if [[ "${{ github.event_name }}" == "push" ]]; then |
| 71 | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 72 | +
|
| 73 | + echo "type=development" >> $GITHUB_OUTPUT |
| 74 | + echo "continue=true" >> $GITHUB_OUTPUT |
| 75 | + echo "On \`main\` Branch, Type: \`development\`" |
| 76 | +
|
| 77 | + elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then |
| 78 | +
|
| 79 | + echo "type=development" >> $GITHUB_OUTPUT |
| 80 | + echo "continue=true" >> $GITHUB_OUTPUT |
| 81 | + echo "On \`develop\` Branch, Type: \`development\`" |
| 82 | +
|
| 83 | + elif [[ $(echo "${{ github.ref }}" | grep -P '^(refs\/heads\/releases\/)(v)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$') ]]; then |
| 84 | +
|
| 85 | + version=$(echo "${{ github.ref }}" | sed -n -E 's/^(refs\/heads\/releases\/)//p') |
| 86 | + echo "version=$version" >> $GITHUB_OUTPUT |
| 87 | + echo "type=release" >> $GITHUB_OUTPUT |
| 88 | + echo "continue=true" >> $GITHUB_OUTPUT |
| 89 | + echo "In \`releases/$version\` Branch, Type: \`release\`" |
| 90 | +
|
| 91 | + else |
| 92 | + echo "Not Correct Branch. Will Not Continue" |
| 93 | + fi |
| 94 | + else |
| 95 | + echo "Not a Push Event. Will Not Continue" |
| 96 | + fi |
| 97 | + else |
| 98 | + echo "On a Forked Repository. Will Not Continue" |
| 99 | + fi |
| 100 | +
|
| 101 | + publish_development: |
| 102 | + name: Publish (Development) |
| 103 | + environment: dockerhub-torrust |
| 104 | + needs: context |
| 105 | + if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'development' |
| 106 | + runs-on: ubuntu-latest |
| 107 | + |
| 108 | + steps: |
| 109 | + - id: meta |
| 110 | + name: Docker Meta |
| 111 | + uses: docker/metadata-action@v5 |
| 112 | + with: |
| 113 | + images: | |
| 114 | + "${{ secrets.DOCKER_HUB_USERNAME }}/${{secrets.DOCKER_HUB_REPOSITORY_NAME }}" |
| 115 | + tags: | |
| 116 | + type=ref,event=branch |
| 117 | +
|
| 118 | + - id: login |
| 119 | + name: Login to Docker Hub |
| 120 | + uses: docker/login-action@v3 |
| 121 | + with: |
| 122 | + username: ${{ secrets.DOCKER_HUB_USERNAME }} |
| 123 | + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} |
| 124 | + |
| 125 | + - id: setup |
| 126 | + name: Setup Toolchain |
| 127 | + uses: docker/setup-buildx-action@v3 |
| 128 | + |
| 129 | + - name: Build and push |
| 130 | + uses: docker/build-push-action@v5 |
| 131 | + with: |
| 132 | + file: ./Containerfile |
| 133 | + push: true |
| 134 | + tags: ${{ steps.meta.outputs.tags }} |
| 135 | + labels: ${{ steps.meta.outputs.labels }} |
| 136 | + cache-from: type=gha |
| 137 | + cache-to: type=gha |
| 138 | + |
| 139 | + publish_release: |
| 140 | + name: Publish (Release) |
| 141 | + environment: dockerhub-torrust |
| 142 | + needs: context |
| 143 | + if: needs.context.outputs.continue == 'true' && needs.context.outputs.type == 'release' |
| 144 | + runs-on: ubuntu-latest |
| 145 | + |
| 146 | + steps: |
| 147 | + - id: meta |
| 148 | + name: Docker Meta |
| 149 | + uses: docker/metadata-action@v5 |
| 150 | + with: |
| 151 | + images: | |
| 152 | + "${{ secrets.DOCKER_HUB_USERNAME }}/${{secrets.DOCKER_HUB_REPOSITORY_NAME }}" |
| 153 | + tags: | |
| 154 | + type=semver,value=${{ needs.context.outputs.version }},pattern={{raw}} |
| 155 | + type=semver,value=${{ needs.context.outputs.version }},pattern={{version}} |
| 156 | + type=semver,value=${{ needs.context.outputs.version }},pattern=v{{major}} |
| 157 | + type=semver,value=${{ needs.context.outputs.version }},pattern={{major}}.{{minor}} |
| 158 | +
|
| 159 | + - id: login |
| 160 | + name: Login to Docker Hub |
| 161 | + uses: docker/login-action@v3 |
| 162 | + with: |
| 163 | + username: ${{ secrets.DOCKER_HUB_USERNAME }} |
| 164 | + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} |
| 165 | + |
| 166 | + - id: setup |
| 167 | + name: Setup Toolchain |
| 168 | + uses: docker/setup-buildx-action@v3 |
| 169 | + |
| 170 | + - name: Build and push |
| 171 | + uses: docker/build-push-action@v5 |
| 172 | + with: |
| 173 | + file: ./Containerfile |
| 174 | + push: true |
| 175 | + tags: ${{ steps.meta.outputs.tags }} |
| 176 | + labels: ${{ steps.meta.outputs.labels }} |
| 177 | + cache-from: type=gha |
| 178 | + cache-to: type=gha |
0 commit comments