|
| 1 | +name: Build/Push Images |
| 2 | + |
| 3 | +# This workflow uses actions that are not certified by GitHub. |
| 4 | +# They are provided by a third-party and are governed by |
| 5 | +# separate terms of service, privacy policy, and support |
| 6 | +# documentation. |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: [ 'master', 'main', 'release-*' ] |
| 11 | + # Publish semver tags as releases. |
| 12 | + pull_request: {} |
| 13 | + release: |
| 14 | + types: [ 'published' ] |
| 15 | + |
| 16 | +env: |
| 17 | + # Use docker.io for Docker Hub if empty |
| 18 | + REGISTRY_GITHUB: ghcr.io |
| 19 | + REGISTRY_DOCKERHUB: docker.io |
| 20 | + |
| 21 | +jobs: |
| 22 | + build: |
| 23 | + |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: read |
| 27 | + packages: write |
| 28 | + # This is used to complete the identity challenge |
| 29 | + # with sigstore/fulcio when running outside of PRs. |
| 30 | + id-token: write |
| 31 | + strategy: |
| 32 | + matrix: |
| 33 | + target: [katalyst-agent, katalyst-scheduler, katalyst-controller, katalyst-webhook, katalyst-metric] |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Checkout repository |
| 37 | + uses: actions/checkout@v3 |
| 38 | + |
| 39 | + # Workaround: https://github.com/docker/build-push-action/issues/461 |
| 40 | + - name: Setup Docker buildx |
| 41 | + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf |
| 42 | + |
| 43 | + # Login against a Docker registry except on PR |
| 44 | + # https://github.com/docker/login-action |
| 45 | + - name: Log into registry ${{ env.REGISTRY_GITHUB }} |
| 46 | + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c |
| 47 | + with: |
| 48 | + registry: ${{ env.REGISTRY_GITHUB }} |
| 49 | + username: ${{ github.actor }} |
| 50 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 51 | + - name: Log into registry ${{ env.REGISTRY_DOCKERHUB }} |
| 52 | + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c |
| 53 | + with: |
| 54 | + registry: ${{ env.REGISTRY_DOCKERHUB }} |
| 55 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 56 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 57 | + |
| 58 | + # Extract metadata (tags, labels) for Docker |
| 59 | + # https://github.com/docker/metadata-action |
| 60 | + - name: Extract Docker metadata |
| 61 | + id: meta |
| 62 | + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 |
| 63 | + with: |
| 64 | + images: ${{ env.REGISTRY_GITHUB }}/${{ github.repository_owner }}/${{ matrix.target }} |
| 65 | + |
| 66 | + # For pushes on main or release-* branch, |
| 67 | + # use ${repo_branch_name}-${commit_sha} as image tag |
| 68 | + - name: Set image tag for pushes |
| 69 | + id: image-tag-push |
| 70 | + if: ${{ github.event_name == 'push' }} |
| 71 | + run: echo "IMAGE_TAG=${GITHUB_REF#refs/*/}-${GITHUB_SHA}" >> $GITHUB_ENV |
| 72 | + |
| 73 | + # For releases use ${repo_tag} as image tag |
| 74 | + - name: Set image tag for release |
| 75 | + id: image-tag-release |
| 76 | + if: ${{ github.event_name == 'release' }} |
| 77 | + run: echo "IMAGE_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV |
| 78 | + |
| 79 | + # For pull requests use ${repo_sha} as image tag |
| 80 | + - name: Set image tag for pr |
| 81 | + id: image-tag-pr |
| 82 | + if: ${{ github.event_name == 'pull_request' }} |
| 83 | + run: echo "IMAGE_TAG=${GITHUB_SHA}" >> $GITHUB_ENV |
| 84 | + |
| 85 | + # Build and push Docker image with Buildx (don't push on PR) |
| 86 | + # https://github.com/docker/build-push-action |
| 87 | + - name: Build and push Docker image |
| 88 | + id: build-and-push |
| 89 | + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a |
| 90 | + with: |
| 91 | + context: . |
| 92 | + platforms: linux/amd64,linux/arm64 |
| 93 | + file: build/dockerfiles/Dockerfile |
| 94 | + push: ${{ github.event_name != 'pull_request' }} |
| 95 | + build-args: | |
| 96 | + BINARY=${{ matrix.target }} |
| 97 | + tags: | |
| 98 | + ${{ env.REGISTRY_GITHUB }}/${{ github.repository_owner }}/${{ matrix.target }}:${{ env.IMAGE_TAG }} |
| 99 | + ${{ env.REGISTRY_DOCKERHUB }}/${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.target }}:${{ env.IMAGE_TAG }} |
| 100 | + labels: ${{ steps.meta.outputs.labels }} |
| 101 | + cache-from: type=gha,target={{ matrix.target }} |
| 102 | + cache-to: type=gha,mode=max,target={{ matrix.target }} |
0 commit comments