Unable to read variable set by set-env in github action

Yaml:

name: Release to GitHub
on:
  workflow_dispatch:
  workflow_run:
    workflows: ["Version bump"]
    branches: [master]
    types:
      - completed
jobs:
  version-bump:
    name: Release to github releases
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set env
        run: echo ::set-env name=pkg_version::$(awk '/version/{gsub(/("|",)/,"",$2);print $2}' package.json)
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ pkg_version }}
          release_name: ${{ pkg_version }}
          body: |
            Changes in this Release
            - Change this
          draft: false
          prerelease: false

I had to use this to fix the eror:

name: Publish release to GitHub
on:
  workflow_dispatch:
  workflow_run:
    workflows: ["Version bump"]
    branches: [master]
    types:
      - completed
jobs:
  version-bump:
    name: Release to github releases
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set env
        run: echo ::set-output name=pkg_version::$(awk '/version/{gsub(/("|",)/,"",$2);print $2}' package.json)
        id: env_out
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.env_out.outputs.pkg_version }}
          release_name: ${{ steps.env_out.outputs.pkg_version }}
          body: |
            Changes in this Release
            - Change this
          draft: false
          prerelease: false

You need to reference the env context to read environment variables outside the shell:

        with:
          tag_name: ${{ env.pkg_version }}
          release_name: ${{ env.pkg_version }}
1 Like

Hi @gautamkrishnar,

After you use set-env, the variable has been added to env context. You can add below code for confrimation.

      - name: Dump env context
        env:
          ENV_CONTEXT: ${{ toJson(env) }}
        run: echo "$ENV_CONTEXT"

image

Hence, you can use ${{ env.pkg_version }} to invoke it.

Thanks

1 Like

Thanks, it is now solved.