Can't Pull Docker Image:

I can’t figure how how to get the second script right, after I create the image:

I’m trying to move away from using a script I had been using to build and push docker images where I was using docker/build-push-action.

I don’t care about retaining the old images from GitHub Packages Docker registry. I just want to push a new image to the Google Container Registry at this point.

I am now running a script to create the image myself and push it to the new Google Container Registry route. So here are my new jobs:

  build_docker_image_from_artifact:
    needs: build
    name: Build a docker image based off the build artifact
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: dist-artifact
          path: dist
      - run: docker build . --file Dockerfile --tag dschinkel/we-do-tdd:latest

Screen Shot 2020-09-01 at 11.56.37 PM

Now I’m not really sure what the image name is that it actually creates. I see that it generated a sha or hash or something and my tag.

Apparently push ghcr.io/dschinkel/we-do-tdd:latest isn’t quite right though in my next script when trying to reference it:

  push_image_to_github_container_registry:
    needs: login_github_container_registry
    name: Push Docker image to GitHub Container Repository.  Creates a Docker Image Package
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: dist-artifact
          path: dist
      - run: ls dist
      # ghcr.io/OWNER/IMAGE_NAME.latest
      - run: docker push ghcr.io/dschinkel/we-do-tdd:latest

I get:

Run docker push ghcr.io/dschinkel/we-do-tdd:latest
An image does not exist locally with the tag: ghcr.io/dschinkel/we-do-tdd
The push refers to repository [ghcr.io/dschinkel/we-do-tdd]

Other Info (in case it helps)
My last push that was using my old script based on docker/build-push-actionthat I built a couple days ago generated this image but not sure what my new script is actually creating because I haven’t packaged it yet:

I took a look at Migrating to GitHub Container Registry for Docker images but not sure if I’m missing something related to that or it’s just a dumb mistake I made above somewhere that I’m just not seeing. I want to think it’s maybe the latter…?

@dschinkel ,

Before you push an image to a Docker or container registry, you need to build (if need) and tag the image.

docker build -t REPOSITORY:TAG -f Dockerfile .

OR

docker tag IMAGE_ID REPOSITORY:TAG

The REPOSITORY should contain the registry’s domain and name of the target repository or owner on the registry, so that the ‘docker push’ command can know where the image is pushed to.

If the REPOSITORY does not contain registry’s information when you tag the image, and only set the registry’s information when pushing the image, the ‘docker push’ command will not recognize registry’s information and it will return the error message like as you have seen:

Run docker push ghcr.io/dschinkel/we-do-tdd:latest
An image does not exist locally with the tag: ghcr.io/dschinkel/we-do-tdd
The push refers to repository [ghcr.io/dschinkel/we-do-tdd]

This also has been mentioned in “Migrating to GitHub Container Registry for Docker images”.

Re-tag the image with the new domain and a new image name.

Yes I’m already building it, above I have:

run: docker build . --file Dockerfile --tag dschinkel/we-do-tdd:latest

I’ve since then gone a little further.

Pretty sure I need to be using the Image ID not we-do-tdd in my push job. So I’m working on trying to persist that newly created image ID as an environment variable that my push job can use:

  build_docker_image_from_artifact:
    needs: build
    name: Build Docker Image Off Artifact
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/download-artifact@v2
        with:
          name: dist-artifact
          path: dist
      - run: docker build . --file Dockerfile --tag latest
      - run: echo "::set-env name=IMAGE_NAME::$(docker images latest --format "{{.ID}}")"

Notice this updated script sets it as an environment variable called IMAGE_NAME that holds the newly created image ID. Problem I have now is it’s not persisting that environment variable to my next job:

  push_image_to_github_container_registry:
    needs: login_github_container_registry
    name: Push image to GitHub Container Repository
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # ghcr.io/OWNER/IMAGE_NAME.latest
      # - run: docker push ghcr.io/dschinkel/${{ env.IMAGE_NAME }}.latest
      - run: docker push ghcr.io/dschinkel/${IMAGE_NAME}.latest

Problem here now is ${IMAGE_NAME}.latest - ${IMAGE_NAME} isn’t being persisted from the environment variable I set in the previous job.

I have verified that this does indeed set the environment variable:
run: echo "::set-env name=IMAGE_NAME::$(docker images latest --format "{{.ID}}")"

but just my last job when it tries to access it, that value is lost.

And again I’m not MOVING an EXISTING image, I’m creating a NEW image and on the fly, trying to push that up to the new GitHub Container Repository.

@dschinkel ,

In a job, the environment variable you set via the ‘set-env’ command in a step will be only available to the subsequent steps in the same job. The steps in other jobs can’t access this environment variable. More details, see “Setting an environment variable”.

If you want to access this value in the subsequent job, you can set it as an output of the previous job.
In your case, you can do like as this:

jobs:
  build_docker_image_from_artifact:
    outputs:
      IMAGE_NAME: ${{ steps.get-image-name.outputs.IMAGE_NAME }}
    . . .
    steps:
      . . .
      - name: get image name
        id: get-image-name
        run: echo "::set-output name=IMAGE_NAME::$(docker images latest --format "{{.ID}}")"

  push_image_to_github_container_registry:
    needs: [build_docker_image_from_artifact, . . .]
    . . .
    steps:
      . . .
      - name: push image
        run: docker push ghcr.io/dschinkel/${{ needs.build_docker_image_from_artifact.outputs.IMAGE_NAME }}.latest
1 Like

Thanks very much! I’ll try this, I was spending hours on trying to figure out this combination! I’m off to bed so will try it when I wake for the day

you’re welcome, @dschinkel .
Any progress, feel free to tell me.

In addition, the following is an example mentioned in the docs about workflow syntax, you also can reference to it:
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjobs_idoutputs

So I was able to get the environment variable working, thanks for the tip. I hadn’t seen that part of the documentation for some reason.