Continuous Deployment With Github Action for AWS ECR

November 29th, 2020 254 Words

Automated releases and deployments wil speed up your development prozess. After setting up Semantic Releases, a tagged software version can be deployed with GitHub Actions. For projects using Amazon Elastic Container Registry, you might want to build and tag a docker image for every GitHub release.

Create a file in .github/workflows to store the configuration:

name: Deployment

on:
  release:
    types: ["created"]

jobs:
  deployment:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Set $VERSION variable
        id: version
        run: echo ::set-output name=VERSION::$(echo $GITHUB_REF | cut -d / -f 3)

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      - name: Login to Amazon ECR
        uses: aws-actions/amazon-ecr-login@v1

      - name: Publish to Container Registry
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: make publish

      - uses: chrnorm/deployment-action@releases/v1
        name: Trigger Deployment
        id: deployment
        with:
          token: ${{ secrets.CUSTOM_GITHUB_TOKEN}}
          environment: production
          ref: refs/tags/${{ steps.version.outputs.VERSION }}

When using docker and AWS ECR, you will need a pair of AWS Credentials to authorize with the registry. The referenced publish task needs to build, tag, and finally push the docker image to the registry:

publish: REPOSITORY_NAME=example-repository
publish: REPOSITORY_HOST=123456789.dkr.ecr.eu-central-1.amazonaws.com
publish:
	@ docker build . -t $(REPOSITORY_NAME):$(VERSION)
	@ docker tag $(REPOSITORY_NAME):$(VERSION) $(REPOSITORY_HOST)/$(REPOSITORY_NAME):$(VERSION)
	@ docker push $(REPOSITORY_HOST)/$(REPOSITORY_NAME):$(VERSION)

After the docker image is pushed to the Amazon Elastic Container Registry, a new GitHub Deployment is created. Next, a GitHub Action needs to update any consumer to the new tagged version: Github Deployments and Github Actions for Continuous Releases.