Build and Push Docker Image to ECR #7
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push Docker Image to ECR | |
| on: | |
| release: | |
| types: [published] | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Need write to push the Dockerfile commit | |
| pull-requests: write # needed to push docker version | |
| id-token: write # For OIDC (optional if using access keys) | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Extract version from release tag | |
| run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | |
| - name: Update Dockerfile version label | |
| run: | | |
| sed -i "s/\(version=\)[\"']\?[^\"']*\([\"']\?\)/\1\"${VERSION}\"/" Docker/Dockerfile | |
| echo "Updated Dockerfile version to ${VERSION}" | |
| - name: Commit and push Dockerfile version update via PR | |
| env: | |
| VERSION: ${{ env.VERSION }} | |
| run: | | |
| git config user.name "github-actions" | |
| git config user.email "[email protected]" | |
| BRANCH="update-dockerfile-version-${VERSION}" | |
| git checkout -b $BRANCH | |
| git add Docker/Dockerfile | |
| git commit -m "Update Dockerfile version to ${VERSION} from release" | |
| git push origin $BRANCH | |
| echo "branch_name=$BRANCH" >> $GITHUB_OUTPUT | |
| id: dockerfile_update | |
| - name: Create Pull Request | |
| uses: peter-evans/create-pull-request@v6 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| branch: ${{ steps.dockerfile_update.outputs.branch_name }} | |
| title: "Update Dockerfile version to ${{ env.VERSION }}" | |
| body: "Automated PR to update Dockerfile version to match release ${{ env.VERSION }}" | |
| base: main | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v1 | |
| with: | |
| role-to-assume: arn:aws:iam::538673716275:role/sar_pipeline_github-actions-role | |
| aws-region: ap-southeast-2 | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| - name: Build and tag Docker image | |
| env: | |
| ECR_REGISTRY: 538673716275.dkr.ecr.ap-southeast-2.amazonaws.com/geoscienceaustralia | |
| ECR_REPOSITORY: sar-pipeline | |
| run: | | |
| IMAGE_TAG=${{ env.VERSION }} | |
| IMAGE_URI=${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG | |
| docker build --build-arg VERSION=$IMAGE_TAG -t $IMAGE_URI -f Docker/Dockerfile . | |
| echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV | |
| - name: Push image to ECR | |
| run: | | |
| docker push $IMAGE_URI |