Skip to content

Build and Push Docker Image to ECR #6

Build and Push Docker Image to ECR

Build and Push Docker Image to ECR #6

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
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
env:
VERSION: ${{ env.VERSION }}
run: |
git config user.name "github-actions"
git config user.email "[email protected]"
git add Docker/Dockerfile
git commit -m "Update Dockerfile version to ${VERSION} from release"
git push origin HEAD: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