|
| 1 | +name: Build Docker |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - develop |
| 9 | + - 'release**' |
| 10 | + tags: |
| 11 | + - 'v*.*.*' |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-build-steps: |
| 15 | + name: Check if build and push should be performed |
| 16 | + runs-on: ubuntu-latest |
| 17 | + timeout-minutes: 5 |
| 18 | + |
| 19 | + outputs: |
| 20 | + dockerhub-enable: ${{ steps.dockerhub.outputs.dockerhub-publish }} |
| 21 | + ghcr-enable: ${{ steps.check-ghcr.outputs.enable }} |
| 22 | + build-and-push-enable: ${{ steps.check-build-and-push.outputs.enable }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + persist-credentials: false |
| 28 | + |
| 29 | + - name: Determine if images should be published to DockerHub |
| 30 | + id: dockerhub |
| 31 | + run: | |
| 32 | + # check if a release branch, or main, or a tag |
| 33 | + if [[ "${{ github.ref }}" =~ ^refs/heads/release([0-9]+)$ || "${{ github.ref }}" == "refs/heads/main" || "${{ github.ref }}" == refs/tags/* ]] |
| 34 | + then |
| 35 | + DOCKERHUB_PUBLISH="1" |
| 36 | + else |
| 37 | + DOCKERHUB_PUBLISH="0" |
| 38 | + fi |
| 39 | + # debug output |
| 40 | + echo "dockerhub-publish $DOCKERHUB_PUBLISH" |
| 41 | + echo "dockerhub-publish=$DOCKERHUB_PUBLISH" >> $GITHUB_OUTPUT |
| 42 | +
|
| 43 | + - name: Check if push to GHCR is enabled |
| 44 | + id: check-ghcr |
| 45 | + env: |
| 46 | + GHCR_ENABLED: ${{ secrets.GHCR_ENABLED }} |
| 47 | + run: | |
| 48 | + echo "Enable push to GHCR: ${{ env.GHCR_ENABLED != '' }}" |
| 49 | + echo "enable=${{ env.GHCR_ENABLED != '' }}" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + - name: Check if there is access to repo secrets (needed for build and push) |
| 52 | + if: steps.dockerhub.outputs.dockerhub-publish == '1' || steps.check-ghcr.outputs.enable == 'true' |
| 53 | + id: check-build-and-push |
| 54 | + env: |
| 55 | + SECRET_ACCESS: ${{ secrets.DOCKERHUB_USERNAME }} |
| 56 | + run: | |
| 57 | + echo "Enable build and push: ${{ env.SECRET_ACCESS != '' }}" |
| 58 | + echo "enable=${{ env.SECRET_ACCESS != '' }}" >> $GITHUB_OUTPUT |
| 59 | +
|
| 60 | + build: |
| 61 | + name: Build and publish docker image for ${{ matrix.repo }} |
| 62 | + runs-on: ubuntu-latest |
| 63 | + timeout-minutes: 15 |
| 64 | + needs: |
| 65 | + - check-build-steps |
| 66 | + |
| 67 | + steps: |
| 68 | + - uses: actions/checkout@v4 |
| 69 | + with: |
| 70 | + persist-credentials: false |
| 71 | + |
| 72 | + - name: Get the docker tag for GHCR |
| 73 | + id: ghcr-tag |
| 74 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' |
| 75 | + uses: docker/metadata-action@v5 |
| 76 | + with: |
| 77 | + images: | |
| 78 | + ghcr.io/${{ github.repository }} |
| 79 | + tags: | |
| 80 | + type=schedule |
| 81 | + type=ref,event=branch |
| 82 | + type=ref,event=tag |
| 83 | + type=raw,value=latest,enable={{is_default_branch}} |
| 84 | + type=raw,value=nightly,enable={{is_default_branch}} |
| 85 | +
|
| 86 | + - name: Get the docker tag for DockerHub |
| 87 | + id: dockerhub-tag |
| 88 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' |
| 89 | + uses: docker/metadata-action@v5 |
| 90 | + with: |
| 91 | + images: | |
| 92 | + sofietv/input-gateway |
| 93 | + tags: | |
| 94 | + type=schedule |
| 95 | + type=ref,event=branch |
| 96 | + type=ref,event=tag |
| 97 | + type=raw,value=latest,enable={{is_default_branch}} |
| 98 | + type=raw,value=nightly,enable={{is_default_branch}} |
| 99 | +
|
| 100 | + - name: Set up Docker Buildx |
| 101 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' |
| 102 | + uses: docker/setup-buildx-action@v3 |
| 103 | + |
| 104 | + - name: Login to DockerHub |
| 105 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' && needs.check-build-steps.outputs.dockerhub-enable == '1' |
| 106 | + uses: docker/login-action@v3 |
| 107 | + with: |
| 108 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 109 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 110 | + |
| 111 | + - name: Login to GitHub Container Registry |
| 112 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' && needs.check-build-steps.outputs.ghcr-enable == 'true' |
| 113 | + uses: docker/login-action@v3 |
| 114 | + with: |
| 115 | + registry: ghcr.io |
| 116 | + username: ${{ github.repository_owner }} |
| 117 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + |
| 119 | + - name: Build and push to GHCR |
| 120 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' && needs.check-build-steps.outputs.ghcr-enable == 'true' |
| 121 | + uses: docker/build-push-action@v6 |
| 122 | + with: |
| 123 | + context: . |
| 124 | + file: Dockerfile |
| 125 | + push: true |
| 126 | + provenance: false |
| 127 | + labels: ${{ steps.ghcr-tag.outputs.labels }} |
| 128 | + tags: '${{ steps.ghcr-tag.outputs.tags }}' |
| 129 | + |
| 130 | + - name: Build and push to DockerHub |
| 131 | + if: needs.check-build-steps.outputs.build-and-push-enable == 'true' && needs.check-build-steps.outputs.dockerhub-enable == '1' |
| 132 | + uses: docker/build-push-action@v6 |
| 133 | + with: |
| 134 | + context: . |
| 135 | + file: Dockerfile |
| 136 | + push: true |
| 137 | + provenance: false |
| 138 | + labels: ${{ steps.dockerhub-tag.outputs.labels }} |
| 139 | + tags: '${{ steps.dockerhub-tag.outputs.tags }}' |
| 140 | + |
| 141 | + trivy-scanning: |
| 142 | + name: Run Trivy scan for ${{ matrix.repo }} |
| 143 | + uses: nrkno/github-workflow-docker-build-push/.github/workflows/[email protected] |
| 144 | + with: |
| 145 | + runs-on: "['ubuntu-latest']" |
| 146 | + registry-url: ghcr.io |
| 147 | + name: nrkno/sofie-package-manager |
| 148 | + # Don't actually push any images, this is just for trivy scanning for now |
| 149 | + push: false |
| 150 | + trivy-severity: 'CRITICAL' |
| 151 | + trivy-summary-enabled: true |
| 152 | + trivy-sbom-enabled: true |
| 153 | + dockerfile: Dockerfile |
| 154 | + secrets: |
| 155 | + registry-username: ${{ github.repository_owner }} |
| 156 | + registry-password: ${{ secrets.GITHUB_TOKEN }} |
| 157 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments