how to use variables in "with" block in github actions

Viewed 1434

I'm struggling a lot with github actions. This seems to work:

      - name: Tag & Push docker image
        run: |
          docker push myrepo/myapp:${GITHUB_SHA::8}

However, this does not:

      - name: create release
        uses: some-custom-action
        with:
          release_version: 1.0.0-${GITHUB_SHA::8}

nor this:

      - name: create release
        uses: some-custom-action
        with:
          release_version: "1.0.0-${{ env.GITHUB_SHA }}"

I'm completely new to github actions and more than a little surprised at the lacking documentation etc.

I simply need to pass a variable into the "with" parameters of a github action.

If anyone is able to help me figure out what I'm doing wrong, I'd be very grateful!

1 Answers

When you use a run context, you're invoking a shell. (For macOS and Linux hosts, this is /bin/bash.) So for this step:

- name: Tag & Push docker image
  run: |
    docker push myrepo/myapp:${GITHUB_SHA::8}

you're using a shell and ${GITHUB_SHA::8} will be passed to it literally. The shell will then parse that and interpolate it with its normal parsing rules.

However, when you specify an action to run, instead of a script to execute, you're just invoking a different program. There's no shell, so there's nothing that will parse ${GITHUB_SHA::8}.

You can use ${{ ... }} to reference things in the contexts that are available. For example, there's an env context that is open for you to set key/value pairs and re-use them. (But the env context is not part of a bash shell, so there's no ${{ env.PWD }} for instance.)

There is a mapping, however, between the github context and environment variables that are set when you do run a shell. The ${{ github.sha }} context variable will be set in your shell as the $GITHUB_SHA environment variable.

So in your example, this should work:

- name: create release
  uses: some-custom-action
  with:
    release_version: "1.0.0-${{ github.sha }}"
Related