skip to Main Content

I’m trying to pull docker image from ECR and deploy it on ec2 instance. However it’s throwing an error like

docker pull  $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

======END======
err: invalid reference format
2022/11/03 15:31:54 Process exited with status 1

My yml file is:

name: Docker Image CI

on:
  push:
    branches: [ "main" ]

jobs:

  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.TF_USER_AWS_KEY }}
        aws-secret-access-key: ${{ secrets.TF_USER_AWS_SECRET }}
        aws-region: us-east-1

    - name: Login 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: githubactions
        IMAGE_TAG: githubactions_image
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
    - name: Docker pull & run from github
      uses: appleboy/ssh-action@master
      with:
        host: ec2-3-86-102-151.compute-1.amazonaws.com
        username: ec2-user
        key: ${{ secrets.ACTIONS_PRIVATE_KEY }}
        envs: GITHUB_SHA
        script: |
            docker pull  $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

I spent a lot of time and I can’t really understand what’s wrong. Any idea really appreciated.

2

Answers


  1. I would first add an ls and echo:

          run: |
            pwd
            ls -arth
            echo "docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ."
            docker ...
    

    That way, I would check if I am in the right folder (with a Dockerfile in it), and if all variables are indeed valued.

    If you see <aregistry>/<arepo>: (meaning no tag), the final ‘:‘ might be enough to trigger the error message.
    That or:

    Login or Signup to reply.
  2. Your issue is with the Env vars.
    You are confusing the github server to the ec2 server.

    The env var you mention in the github yml do no exist on the remote machine(ec2).
    The cmd -> docker pull $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG failing cause the remote mahicne(ec2) do not know about your github env vars.
    from the docs here you can see that there is an easy built-in way to pass it to the ec2 instance

      - name: pass environment
        uses: appleboy/ssh-action@master
    +   env:
    +     FOO: "BAR"
    +     BAR: "FOO"
    +     SHA: ${{ github.sha }}
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.KEY }}
          port: ${{ secrets.PORT }}
    +     envs: FOO,BAR,SHA
          script: |
            echo "I am $FOO"
            echo "I am $BAR"
            echo "sha: $SHA"
    

    hope it helps and goodluck

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search