|
| 1 | +name: Build and Push Backend Docker Image to ECR |
| 2 | +run-name: Build and Push to ECR - ${{ inputs.environment }} - ${{ inputs.version }} |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + type: string |
| 8 | + required: true |
| 9 | + description: "environment to deploy to, used for selecting correct AWS credentials and CloudFront distribution based on environment specific secrets and variables" |
| 10 | + aws-region: |
| 11 | + type: string |
| 12 | + required: true |
| 13 | + description: "AWS region for deployment and AWS CLI commands" |
| 14 | + ecr-repository-name: |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + version: |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + description: "Version tag for the Docker image" |
| 21 | + secrets: |
| 22 | + AWS_OIDC_ROLE_ARN: |
| 23 | + description: "OIDC role ARN for AWS credentials" |
| 24 | + required: true |
| 25 | + AWS_DEPLOYMENT_ROLE_ARN: |
| 26 | + description: "Deployment role ARN for AWS credentials" |
| 27 | + required: true |
| 28 | + outputs: |
| 29 | + image: |
| 30 | + value: ${{ jobs.build-push-image.outputs.image }} |
| 31 | + |
| 32 | +jobs: |
| 33 | + build-push-image: |
| 34 | + name: Build and push docker image |
| 35 | + runs-on: ubuntu-latest |
| 36 | + environment: ${{ inputs.environment }} |
| 37 | + timeout-minutes: 5 |
| 38 | + outputs: |
| 39 | + image: ${{ steps.push-image.outputs.image }} |
| 40 | + env: |
| 41 | + AWS_REGION: ${{ inputs.aws-region }} |
| 42 | + ECR_REPOSITORY: ${{inputs.ecr-repository-name}} |
| 43 | + permissions: |
| 44 | + id-token: write |
| 45 | + contents: read |
| 46 | + steps: |
| 47 | + - name: Print GitHub Context Safely |
| 48 | + run: | |
| 49 | + echo "--- GitHub Context ---" |
| 50 | + echo "${GITHUB_REPOSITORY}" |
| 51 | + echo "${GITHUB_REF}" |
| 52 | + echo "${GITHUB_SHA}" |
| 53 | + - name: Validate deployment inputs |
| 54 | + env: |
| 55 | + VERSION: ${{ inputs.version }} |
| 56 | + run: | |
| 57 | + set -euo pipefail |
| 58 | + [[ "$VERSION" =~ ^[0-9]+(\.[0-9]+){2}([.-][0-9A-Za-z]+)*$ ]] || { echo "Invalid version"; exit 1; } |
| 59 | + echo "VERSION=$VERSION" >> "$GITHUB_ENV" |
| 60 | + - name: Checkout |
| 61 | + uses: actions/checkout@v6 |
| 62 | + with: |
| 63 | + ref: ${{ format('v{0}', inputs.version) }} |
| 64 | + |
| 65 | + - name: Set up JDK |
| 66 | + uses: actions/setup-java@v5 |
| 67 | + with: |
| 68 | + java-version: '25' |
| 69 | + distribution: 'corretto' |
| 70 | + |
| 71 | + - name: Restore Maven Cache |
| 72 | + uses: actions/cache/restore@v5 |
| 73 | + with: |
| 74 | + path: ~/.m2/repository |
| 75 | + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} |
| 76 | + restore-keys: | |
| 77 | + ${{ runner.os }}-maven- |
| 78 | +
|
| 79 | + - name: Maven package |
| 80 | + run: mvn -B clean package -DskipTests -T 1C |
| 81 | + |
| 82 | + - name: Set image tag to GITHUB_ENV |
| 83 | + run: echo "IMAGE_TAG=${VERSION}" >> $GITHUB_ENV |
| 84 | + |
| 85 | + - name: Configure AWS credentials |
| 86 | + uses: aws-actions/configure-aws-credentials@v6 |
| 87 | + with: |
| 88 | + role-to-assume: ${{ secrets.AWS_OIDC_ROLE_ARN }} |
| 89 | + aws-region: ${{ env.AWS_REGION }} |
| 90 | + |
| 91 | + # activate once blueprint is ready |
| 92 | + # - name: Assume deployment role |
| 93 | + # uses: aws-actions/configure-aws-credentials@v4 |
| 94 | + # with: |
| 95 | + # role-to-assume: ${{ secrets.AWS_DEPLOYMENT_ROLE_ARN }} |
| 96 | + # aws-region: ${{ env.AWS_REGION }} |
| 97 | + # role-chaining: true |
| 98 | + # role-skip-session-tagging: true |
| 99 | + |
| 100 | + - name: Login to AWS ECR |
| 101 | + id: login-ecr |
| 102 | + uses: aws-actions/amazon-ecr-login@v2 |
| 103 | + |
| 104 | + - name: Set up QEMU |
| 105 | + uses: docker/setup-qemu-action@v4 |
| 106 | + |
| 107 | + - name: Set up Docker Buildx |
| 108 | + uses: docker/setup-buildx-action@v4 |
| 109 | + |
| 110 | + - name: Generate Docker metadata |
| 111 | + id: meta |
| 112 | + uses: docker/metadata-action@v6 |
| 113 | + with: |
| 114 | + images: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }} |
| 115 | + tags: | |
| 116 | + type=raw,value=${{ env.VERSION }} |
| 117 | + type=sha |
| 118 | +
|
| 119 | + - name: Build and push to ECR |
| 120 | + id: build |
| 121 | + uses: docker/build-push-action@v7 |
| 122 | + with: |
| 123 | + context: . |
| 124 | + file: ./Dockerfile |
| 125 | + platforms: linux/amd64,linux/arm64 |
| 126 | + push: true |
| 127 | + tags: ${{ steps.meta.outputs.tags }} |
| 128 | + # Caching makes subsequent builds much faster |
| 129 | + cache-from: type=gha |
| 130 | + cache-to: type=gha,mode=max |
| 131 | + provenance: false |
| 132 | + sbom: true |
| 133 | + |
0 commit comments