ENV interpolation within actions

I’m trying to make a generic action to build a docker image and publish to GitHub Packages. I don’t want people futzing with the file, so I want to make use of ENV. Crazy, right?

on:
  push:
    branches:
      - develop
name: Docker Latest
jobs:
  docker:
    name: Build and push Docker Image
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2 # https://github.com/actions/checkout
      - name: Set ENV
        run: echo "::set-env name=INPUT_REPOSITORY::${GITHUB_REPOSITORY}/${GITHUB_REPOSITORY##*/}"
      - uses: docker/build-push-action@v1 # https://github.com/marketplace/actions/build-and-push-docker-images
        with:
          username: $GITHUB_ACTOR
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          tags: latest

I also tried defining the ENV at the top level as well, and that actually seemed to give me the same result. ${GITHUB_REPOSITORY##*/} doesn’t seem to be working, though the shell says it’s executing within bash.

GITHUB_REPOSITORY=foo/bar
echo "::set-env name=INPUT_REPOSITORY::${GITHUB_REPOSITORY}/${GITHUB_REPOSITORY##*/}"
::set-env name=INPUT_REPOSITORY::foo/bar/bar

This works locally. As it stands, github is telling me that my image needs an image name.

...
Building image [docker.pkg.github.com/foo/bar:latest]
...
Please add an image name to "foo/bar" tag. e.g. "foo/bar/<image_name>"

I very much am… Any ideas?

Please add repository input in your docker/build-push-action@v1 step, like:

jobs:
  docker:
    name: Build and push Docker Image
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2 # https://github.com/actions/checkout
      - name: Set ENV
        run: echo "::set-env name=INPUT_REPOSITORY::${GITHUB_REPOSITORY}/${GITHUB_REPOSITORY##*/}"
      - uses: docker/build-push-action@v1 # https://github.com/marketplace/actions/build-and-push-docker-images
        with:
          username: $GITHUB_ACTOR
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: ${{env.INPUT_REPOSITORY}}
          tags: latest

Based on my test, the value of INPUT_REPOSITORY set with set-env command will be replaced by empty when run the docker container action without specify repository input variable. You still need to specify the input variable under with key in the docker contain action. You could refer to an environment variable there.

According to the action code https://github.com/docker/github-actions/blob/0f18e2abad9a4ac2963d2516246787375b5ec917/internal/options/tag.go#L31

func dockerRepo(github GitHub) string {
	if repo := os.Getenv("INPUT_REPOSITORY"); repo != "" {
		return repo
	}
	return strings.ToLower(github.Repository)
}

When INPUT_REPOSITORY is empty, repo will use github.Repository. In your scenario, it is foo/bar. That the reason why you got error message :

Please add an image name to "foo/bar" tag. e.g. "foo/bar/<image_name>"

Thanks! repository: ${{env.INPUT_REPOSITORY}} works, but that seems odd. I’m setting the env explicitly. The code you linked even fetches the env. Weird.

Please see the explanation in my previous reply.