|
| 1 | +--- |
| 2 | +name: Build and publish Docker Image |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_call: |
| 6 | + inputs: |
| 7 | + acr_name: |
| 8 | + type: string |
| 9 | + default: crjore4prod001 |
| 10 | + docker_image_name: |
| 11 | + type: string |
| 12 | + required: true |
| 13 | + build_arm64_image: |
| 14 | + description: Should arm64 Docker image be built |
| 15 | + type: boolean |
| 16 | + required: false |
| 17 | + default: false |
| 18 | + file: |
| 19 | + description: Path to Dockerfile |
| 20 | + type: string |
| 21 | + required: false |
| 22 | + default: Dockerfile |
| 23 | + context: |
| 24 | + description: Docker build context. This needs to be Git repository context; see https://docs.docker.com/build/concepts/context/#url-fragments |
| 25 | + type: string |
| 26 | + required: false |
| 27 | + default: null |
| 28 | + build_args: |
| 29 | + description: Docker build time arguments |
| 30 | + type: string |
| 31 | + required: false |
| 32 | + default: null |
| 33 | + target: |
| 34 | + description: Sets the target stage to build |
| 35 | + type: string |
| 36 | + required: false |
| 37 | + default: null |
| 38 | + secrets: |
| 39 | + azure_tenant_id: |
| 40 | + required: true |
| 41 | + azure_subscription_id: |
| 42 | + required: true |
| 43 | + azure_client_id: |
| 44 | + required: true |
| 45 | + outputs: |
| 46 | + docker_image: |
| 47 | + description: Docker image with tag |
| 48 | + value: ${{ jobs.shared_build_and_publish_docker_image.outputs.docker_image }} |
| 49 | + |
| 50 | +permissions: |
| 51 | + id-token: write |
| 52 | + contents: read |
| 53 | + |
| 54 | +jobs: |
| 55 | + shared_build_and_publish_docker_image: |
| 56 | + runs-on: ubuntu-24.04 |
| 57 | + outputs: |
| 58 | + docker_image: ${{ steps.variables.outputs.docker_image }} |
| 59 | + steps: |
| 60 | + - name: Set variables |
| 61 | + id: variables |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + # GITHUB_HEAD_REF is the source branch of a pull request |
| 65 | + # GITHUB_REF is the name of the branch or tag that triggered the workflow |
| 66 | + # i.e. for pull requests the branch name is read from GITHUB_HEAD_REF and for |
| 67 | + # branch/tag updates from GITHUB_REF |
| 68 | + ref="${GITHUB_HEAD_REF:-${GITHUB_REF}}" |
| 69 | +
|
| 70 | + # Strip refs/heads/ or refs/tags/ out |
| 71 | + ref_name=$(basename ${ref}) |
| 72 | +
|
| 73 | + docker_tag=$(echo -n "${ref_name}" | tr -C '0-9a-zA-Z._' '-')-$(date +%Y-%m-%d)-${GITHUB_SHA} |
| 74 | +
|
| 75 | + base_docker_image=${{ inputs.acr_name }}.azurecr.io/${{ inputs.docker_image_name }} |
| 76 | + echo "docker_image_latest=${base_docker_image}:latest" >> ${GITHUB_OUTPUT} |
| 77 | + echo "docker_image_cache=${base_docker_image}:cache" >> ${GITHUB_OUTPUT} |
| 78 | + echo "docker_image=${base_docker_image}:${docker_tag}" >> ${GITHUB_OUTPUT} |
| 79 | +
|
| 80 | + - name: Set up QEMU |
| 81 | + if: ${{ inputs.build_arm64_image }} |
| 82 | + uses: docker/setup-qemu-action@v3 |
| 83 | + with: |
| 84 | + platforms: 'arm64' |
| 85 | + |
| 86 | + - name: Set up Docker Buildx |
| 87 | + uses: docker/setup-buildx-action@v3 |
| 88 | + |
| 89 | + - name: Azure login |
| 90 | + uses: azure/login@v2 |
| 91 | + with: |
| 92 | + client-id: ${{ secrets.azure_client_id }} |
| 93 | + tenant-id: ${{ secrets.azure_tenant_id }} |
| 94 | + subscription-id: ${{ secrets.azure_subscription_id }} |
| 95 | + |
| 96 | + - name: Login to ACR via OIDC |
| 97 | + run: az acr login --name ${{ inputs.acr_name }} |
| 98 | + |
| 99 | + - name: Build and push |
| 100 | + uses: docker/build-push-action@v6 |
| 101 | + with: |
| 102 | + tags: | |
| 103 | + ${{ steps.variables.outputs.docker_image }} |
| 104 | + ${{ github.ref_name == 'main' && steps.variables.outputs.docker_image_latest || '' }} |
| 105 | + cache-from: type=registry,ref=${{ steps.variables.outputs.docker_image_cache }} |
| 106 | + cache-to: ${{ github.ref_name == 'main' && format('type=registry,ref={0},mode=max', steps.variables.outputs.docker_image_cache) || '' }} |
| 107 | + push: true |
| 108 | + platforms: ${{ inputs.build_arm64_image && 'linux/amd64,linux/arm64' || 'linux/amd64' }} |
| 109 | + context: ${{ inputs.context }} |
| 110 | + build-args: ${{ inputs.build_args }} |
| 111 | + file: ${{ inputs.file }} |
| 112 | + target: ${{ inputs.target }} |
0 commit comments