Search code examples
shellyamlgithub-actions

Update declared variables in GIthub Actions workflow


How does one go about updating a variable that is declared in github action workflow?

Consider the following:


name: Test Variable

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
env:
  DAY_OF_WEEK: Monday

jobs:
  job1:
    name: Job1
    runs-on: ubuntu-latest
    env:
      Greeting: Hello
    steps:
      - name: "Say Hello John it's Monday"
        run: |
          echo $Greeting=Holla
          echo "$Greeting $First_Name. Today is $DAY_OF_WEEK!"
        env:
          First_Name: John
      
      - name: "Eval"
        run: echo $Greeting $First_Name

So here I'm attempting to update Greeting then eval it later but GH is throwing

Invalid workflow file.You have an error in your yaml syntax on line 21.

So, if I were to update Greeting First_Name and DAY_OF_WEEKhow would I go about doing that?

Update

Fixed yaml syntax but the variable is not updated. Output for Eval is

Run echo $Greeting $First_Name
  echo $Greeting $First_Name
  shell: /usr/bin/bash -e {0}
  env:
    DAY_OF_WEEK: Monday
    Greeting: Hello
Hello

Solution

  • Assign a variable:

    run echo "Greeting=HOLLA" >> $GITHUB_ENV
    

    using the variable

    run echo "$Greeting"
    

    docs: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable

    (Also make sure yours yml-file's indentation is correct.)