|
| 1 | +# |
| 2 | +name: Create and publish a Docker image |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_call: |
| 6 | + secrets: |
| 7 | + PUSH_IMAGE_ROLE: |
| 8 | + required: true |
| 9 | + inputs: |
| 10 | + ecr_name: |
| 11 | + type: string |
| 12 | + description: "The name of the ECR repository to push the dev container image to." |
| 13 | + required: true |
| 14 | + container_image_tag: |
| 15 | + type: string |
| 16 | + description: "The tag of an existing image to publish to github." |
| 17 | + required: true |
| 18 | + |
| 19 | +# 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. |
| 20 | +env: |
| 21 | + REGISTRY: ghcr.io |
| 22 | + IMAGE_NAME: ${{ github.repository }} |
| 23 | + |
| 24 | +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. |
| 25 | +jobs: |
| 26 | + build-and-push-image: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + packages: write |
| 32 | + attestations: write |
| 33 | + id-token: write |
| 34 | + # |
| 35 | + steps: |
| 36 | + - name: Configure AWS Credentials |
| 37 | + uses: aws-actions/configure-aws-credentials@00943011d9042930efac3dcd3a170e4273319bc8 |
| 38 | + id: connect-aws-deploy |
| 39 | + with: |
| 40 | + aws-region: eu-west-2 |
| 41 | + role-to-assume: ${{ secrets.PUSH_IMAGE_ROLE }} |
| 42 | + role-session-name: dev-container-build-x64 |
| 43 | + output-credentials: true |
| 44 | + - name: Retrieve AWS Account ID |
| 45 | + id: retrieve-deploy-account-id |
| 46 | + run: | |
| 47 | + ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) |
| 48 | + echo "account_id=$ACCOUNT_ID" >> "$GITHUB_OUTPUT" |
| 49 | +
|
| 50 | + - name: Login to Amazon ECR |
| 51 | + run: | |
| 52 | + aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin ${{ steps.retrieve-deploy-account-id.outputs.account_id }}.dkr.ecr.eu-west-2.amazonaws.com |
| 53 | +
|
| 54 | + - name: pull image |
| 55 | + run: | |
| 56 | + docker pull "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/${ECR_REPOSITORY}:${IMAGE_TAG}-amd64" |
| 57 | + docker pull "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/${ECR_REPOSITORY}:${IMAGE_TAG}-arm64" |
| 58 | + env: |
| 59 | + ACCOUNT_ID: ${{ steps.retrieve-deploy-account-id.outputs.account_id }} |
| 60 | + ECR_REPOSITORY: ${{ inputs.ecr_name }} |
| 61 | + IMAGE_TAG: ${{ inputs.container_image_tag }} |
| 62 | + |
| 63 | + - name: Log in to the Container registry |
| 64 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 |
| 65 | + with: |
| 66 | + registry: ${{ env.REGISTRY }} |
| 67 | + username: ${{ github.actor }} |
| 68 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 69 | + |
| 70 | + - name: Tag and push amd64 image |
| 71 | + run: | |
| 72 | + docker tag "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/${ECR_REPOSITORY}:${IMAGE_TAG}-amd64" "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-amd64" |
| 73 | + docker push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-amd64" |
| 74 | + env: |
| 75 | + ACCOUNT_ID: ${{ steps.retrieve-deploy-account-id.outputs.account_id }} |
| 76 | + ECR_REPOSITORY: ${{ inputs.ecr_name }} |
| 77 | + IMAGE_TAG: ${{ inputs.container_image_tag }} |
| 78 | + |
| 79 | + - name: Tag and push arm64 image |
| 80 | + run: | |
| 81 | + docker tag "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/${ECR_REPOSITORY}:${IMAGE_TAG}-arm64" "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-arm64" |
| 82 | + docker push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-arm64" |
| 83 | + env: |
| 84 | + ACCOUNT_ID: ${{ steps.retrieve-deploy-account-id.outputs.account_id }} |
| 85 | + ECR_REPOSITORY: ${{ inputs.ecr_name }} |
| 86 | + IMAGE_TAG: ${{ inputs.container_image_tag }} |
| 87 | + |
| 88 | + - name: Create and push multi-arch manifest |
| 89 | + run: | |
| 90 | + docker manifest create "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}" \ |
| 91 | + --amend "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-amd64" \ |
| 92 | + --amend "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}-arm64" |
| 93 | + docker manifest push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}" |
| 94 | + env: |
| 95 | + IMAGE_TAG: ${{ inputs.container_image_tag }} |
0 commit comments