Github actions: aws ecr login fails

7.2k Views Asked by At

I am trying to setup CI for my github repository. After each push in sandbox branch I want build a docker image my project and push to AWS ECR.

Here is my .github/workflows/aws.yml file -

name: be-harvester CI

on:
  pull_request:
    branches:         
    - sandbox
  push:
    branches:         
    - sandbox   

env:
  AWS_REPOSITORY_URL: ${{ secrets.AWS_REPOSITORY_URL }}
  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

jobs:
  build-and-push:
    name: Build and push image to AWS ECR
    runs-on: ubuntu-latest
    steps:

    - name: Checkout
      uses: actions/checkout@master

    - name: Check REPO url
      run: echo $AWS_REPOSITORY_URL

    - name: Setup ECR
      run: $( aws ecr get-login --no-include-email --region ap-south-1)

    - name: Build and tag the image
      run: docker build -t $AWS_REPOSITORY_URL .

    - name: Push
      run: docker push $REPOSITORY_URL

Build fails at aws ecr login step, here is the screenshot of build log. enter image description here

What is the reason of this error?

1

There are 1 best solutions below

0
DannyB On

Use the official AWS actions:

Example:

jobs:
  build:
    steps:
    # see: https://github.com/aws-actions/configure-aws-credentials
    - 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: us-east-1

    # see: https://github.com/aws-actions/amazon-ecr-login
    - name: Log in to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: reponame
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

    - name: Log out of Amazon ECR
      if: always()
      run: docker logout ${{ steps.login-ecr.outputs.registry }}