5

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.

Vamsi
  • 41
  • 5

2 Answers2

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

helpper
  • 2,058
  • 4
  • 13
  • 32
0

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:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Here dockerfile is there on my github repo and for that repo trying to build and push to ECR. And later need to deploy on new EC2 instance. – Vamsi Nov 04 '22 at 07:31
  • @Vamsi OK, what does the echo show? – VonC Nov 04 '22 at 07:36
  • out: docker build -t /: . – Vamsi Nov 04 '22 at 08:56
  • @Vamsi So your variables are empty, which explains the error message. – VonC Nov 04 '22 at 09:18
  • Yes..How can i use/define variable already define in the jobs section( ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}) – Vamsi Nov 04 '22 at 10:02
  • without using export ECR_REGISTRY="***.dkr.ecr.us-east-1.amazonaws.com" command – Vamsi Nov 04 '22 at 10:03
  • @Vamsi [Check first](https://github.com/aws-actions/amazon-ecr-login#troubleshooting) why `steps.login-ecr.outputs.registry` is empty – VonC Nov 04 '22 at 10:21
  • It's not empty...it's a URI of ECR repo ECR_REGISTRY: ***.dkr.ecr.us-east-1.amazonaws.com – Vamsi Nov 04 '22 at 12:49