From 29cd39f8dd7310407d88b5dde2db45469efaec4f Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 5 Nov 2020 15:16:42 +0100 Subject: [PATCH] Automatically push Docker image to Amazon ECR (#9) --- .github/workflows/deploy.yml | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..a80a9bf --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,59 @@ +name: Deploy to Amazon ECR + +env: + aws_region: eu-west-2 + ecr_repository: ${{ github.event.repository.name }} + dockerfile: "Dockerfile" + +on: + push: + branches: [master] + +jobs: + deploy: + name: Upload image to ECR + runs-on: ubuntu-latest + steps: + ############### + ############### + # Setup Steps # + ############### + ############### + - name: Checkout + uses: actions/checkout@v2 + + - uses: satackey/action-docker-layer-caching@v0.0.5 + continue-on-error: true + with: + concurrency: 30 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ECR_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_ECR_SECRET_ACCESS_KEY }} + aws-region: ${{ env.aws_region }} + + - name: Login to Amazon ECR + id: login_to_ecr + uses: aws-actions/amazon-ecr-login@v1 + + ################### + ################### + # Build the image # + ################### + ################### + - name: Build and Push Docker image + env: + ECR_REGISTRY: ${{ steps.login_to_ecr.outputs.registry }} + ECR_REPOSITORY: ${{ env.ecr_repository }} + IMAGE_TAG: "${{ github.sha }}" + DOCKERFILE: ${{ env.dockerfile }} + run: | + # Build a docker container and push it to ECR + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f $DOCKERFILE . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + + # Retag this as the production tag to deploy it + docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:production + docker push $ECR_REGISTRY/$ECR_REPOSITORY:production