|
| 1 | +# Build when operator code changes |
| 2 | +name: Build and push operator container image |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - operator/**/*.go |
| 10 | + - containers/operator.Dockerfile |
| 11 | + - .github/workflows/operator-container.yaml |
| 12 | + push: |
| 13 | + branches: |
| 14 | + - main |
| 15 | + tags: |
| 16 | + - operator/* |
| 17 | + paths: |
| 18 | + - operator/**/*.go |
| 19 | + - containers/operator.Dockerfile |
| 20 | + - .github/workflows/operator-container.yaml |
| 21 | + |
| 22 | +# NOTE: we may want to switch to matrix build for multi-platform support if this is taking too long |
| 23 | +# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners |
| 24 | + |
| 25 | + |
| 26 | +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. |
| 27 | +env: |
| 28 | + REGISTRY: ghcr.io |
| 29 | + IMAGE_NAME: ${{ github.repository }} |
| 30 | + GO_VERSION: 1.23.4 |
| 31 | + |
| 32 | +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. |
| 33 | +jobs: |
| 34 | + build-and-push-operator: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
| 37 | + permissions: |
| 38 | + contents: read |
| 39 | + packages: write |
| 40 | + attestations: write |
| 41 | + id-token: write |
| 42 | + # |
| 43 | + steps: |
| 44 | + - name: Checkout repository |
| 45 | + uses: actions/checkout@v4 |
| 46 | + # Uses the `docker/login-action` action to log in to the Container registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. |
| 47 | + - name: Log in to the Container registry |
| 48 | + uses: docker/login-action@v3 |
| 49 | + with: |
| 50 | + registry: ${{ env.REGISTRY }} |
| 51 | + username: ${{ github.actor }} |
| 52 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 53 | + |
| 54 | + # Setup for multi-platform |
| 55 | + - name: Set up QEMU |
| 56 | + uses: docker/setup-qemu-action@v3 |
| 57 | + |
| 58 | + - name: Set up Docker Buildx |
| 59 | + uses: docker/setup-buildx-action@v3 |
| 60 | + |
| 61 | + - name: Build the operator container image |
| 62 | + id: build |
| 63 | + run: | |
| 64 | + apt-get update && apt-get install -y make git jq |
| 65 | + cd operator |
| 66 | + # if this is a tag build, use the tag as the version, otherwise use the sha |
| 67 | + TAGS="-t ${REGISTRY@L}/${{env.IMAGE_NAME}}/operator:${{ github.sha }} -t ${REGISTRY@L}/${{env.IMAGE_NAME}}/operator:latest" |
| 68 | + case ${{ github.ref_type }} in |
| 69 | + branch) |
| 70 | + # The last tag + current git sha |
| 71 | + export OPERATOR_VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")+${{ github.sha }} |
| 72 | + ;; |
| 73 | + tag) |
| 74 | + # The version part of the tag |
| 75 | + export OPERATOR_VERSION=$(echo "${{ github.ref_name }}" | cut -f 2 -d /) |
| 76 | + TAGS="$TAGS -t ${REGISTRY@L}/${{env.IMAGE_NAME}}/operator:${OPERATOR_VERSION}" |
| 77 | + ;; |
| 78 | + *) |
| 79 | + echo "Unkown type ${{ github.ref_type }}" |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + esac |
| 83 | + set -x |
| 84 | + docker buildx build \ |
| 85 | + --build-arg GIT_SHA=$${{ github.sha }} \ |
| 86 | + --build-arg VERSION=${OPERATOR_VERSION} \ |
| 87 | + --build-arg GO_VERSION=${GO_VERSION} \ |
| 88 | + --push \ |
| 89 | + --platform linux/amd64 \ |
| 90 | + ${TAGS@L} \ |
| 91 | + --metadata-file=metadata.json \ |
| 92 | + -f ../containers/operator.Dockerfile . |
| 93 | + cat metadata.json |
| 94 | + echo "digest=$(cat metadata.json | jq -r .\"containerimage.digest\")" >> $GITHUB_OUTPUT |
| 95 | + cat $GITHUB_OUTPUT |
| 96 | + |
| 97 | + # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see [AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds). |
| 98 | + - name: Generate artifact attestation |
| 99 | + uses: actions/attest-build-provenance@v2 |
| 100 | + with: |
| 101 | + subject-name: ${{ env.REGISTRY }}/${{env.IMAGE_NAME}}/operator |
| 102 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 103 | + push-to-registry: true |
| 104 | + |
0 commit comments