2

How do I create a github workflow step name with a variable value.

I tried this but it does not work.

name: Publish
on:
  push:
    branches:
      - main
env:
  REGISTRY: ghcr.io

jobs:
  Publish:
    runs-on: ubuntu-latest
    steps:
      - name: Log into Container registry ${{ env.REGISTRY }}
1
  • You might want to wait until the job reaches the step. At first it's visible as ${{ env.NAME }} but when it gets there it gets replaced by the proper value. Commented Jan 26, 2024 at 9:21

2 Answers 2

3

I know you tried it, but reproducing the workflow here with your implementation (as below) actually worked for me.

name: Publish

on:
  push:

env:
  REGISTRY: ghcr.io

jobs:
  Publish:
    runs-on: ubuntu-latest
    steps:
      - name: Log into Container registry ${{ env.REGISTRY }}
        run: echo "Ok"

The job step name was generated dynamically according to the workflow env variable set.

Here is the workflow run

evidence

Sign up to request clarification or add additional context in comments.

3 Comments

Weird at first it was not working for me and now it just started working. Thanks for going to the trouble of testing this.
I see why mine was not working. I was going back and forth with a if statement and if you set the if statement to not run after the name it doesn';t add the variable. Example: - name: Log into Container registry ${{ env.REGISTRY }} if: ${{ !contains(github.event.head_commit.message, '!RELEASE') }}
Good to know the order may matter in how you configure your steps with env variables! Thanks for sharing
0

Since this does not seem supported, it is better to add an echo which prints the variable, before one processing it:

name: Publish
on:
  push:
    branches:
      - main
env:
  REGISTRY: ghcr.io

jobs:
  Publish:
    runs-on: ubuntu-latest
    steps:
      - name: Log into Container registry 
        run: echo "registry is '$REGISTRY'"

After that, for runtime variables, you can add conditions:

on:
  push:
    branches:
      - actions-test-branch

jobs:
  Echo-On-Commit:
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout Repository"
        uses: actions/checkout@v2

      - name: "Set flag from Commit"
        env:
          COMMIT_VAR: ${{ contains(github.event.head_commit.message, '[commit var]') }}
        run: |
          if ${COMMIT_VAR} == true; then
            echo "flag=true" >> $GITHUB_ENV
            echo "flag set to true"
          else
            echo "flag=false" >> $GITHUB_ENV
            echo "flag set to false"
          fi

      - name: "Use flag if true"
        if: env.flag
        run: echo "Flag is available and true"

2 Comments

Thanks what does echo "flag=true" >> $GITHUB_ENV do. I thought echo was just to log in console not to set variables ?
@me-me It is to set the environment variable: "You can make an environment variable available to any subsequent steps in a workflow job by defining or updating the environment variable and writing this to the GITHUB_ENV environment file."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.