skip to Main Content

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 }}

2

Answers


  1. 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"
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search