Home > Back-end >  Github reuse env variable inside other
Github reuse env variable inside other

Time:07-07

I want to reuse one env variable inside other env variable in YAML Github actions.

I have tried this:

name: BUILD AND PUBLISH

on:
  push:
    branches:  "master" 

  pull_request:
    branches:  "master"
    
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
 
    - name: BEFORE Set env variables
      run: echo "NOW=$(date  '%Y.%m.%d-%H.%M.%S')" >> $GITHUB_ENV;
           echo "LINUX_ARM_64_NAME=linux-arm64_${{ env.NOW }}" >> $GITHUB_ENV;

...

But the result is:

env:
    NOW: 2022.07.06-21.56.48
    LINUX_ARM_64_NAME: linux-arm64_

And desired result is (I don't want to repeat $(date '%Y.%m.%d-%H.%M.%S') line):

env:
    NOW: 2022.07.06-21.56.48
    LINUX_ARM_64_NAME: linux-arm64_2022.07.06-21.56.48

CodePudding user response:

Environment variables are only available in steps after the one they're set in (docs).

You can either use separate steps:

    - run: echo "NOW=$(date  '%Y.%m.%d-%H.%M.%S')" >> "$GITHUB_ENV"
    - run: echo "LINUX_ARM_64_NAME=linux-arm64_${{ env.NOW }}" >> "$GITHUB_ENV"

or you can use a temporary variable:

    - name: BEFORE Set env variables
      run: |
        NOW=$(date  '%Y.%m.%d-%H.%M.%S')
        echo "NOW=$NOW" >> "$GITHUB_ENV"
        echo "LINUX_ARM_64_NAME=linux-arm64_$NOW" >> "$GITHUB_ENV"
  • Related