SLN
SLN

Reputation: 5082

GitHub Action: How to get value from expression evaluation and assign it to the environment variable

An environment expression is often assigned directly like the example below

  - name: set up env var
    env:
      TAG: v1.2.3
    run: echo $TAG

But how can I get the value from shell script evaluation? For example in my terminal I can get the current Tag by git describe --exact-match --tags $(git log -n1 --pretty='%h')

but when I try to put this script into the env as follow

  - name: set up env var
    env:
      TAG: $(git describe --exact-match --tags $(git log -n1 --pretty='%h'))
    run: echo $TAG 

the echo printed out $(git describe --exact-match --tags $(git log -n1 --pretty='%h')) which means it is not evaluated but treated as a string.

How can I get the value of git describe --exact-match --tags $(git log -n1 --pretty='%h') and assign it to the environment variable TAG ?

Upvotes: 7

Views: 10916

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 22990

You can add variables to the GITHUB_ENV by using: echo "{name}={value}" >> $GITHUB_ENV

This allows to create or update an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.

Source

So your workflow could for example look like this:

 - name: set up env var
    run: echo "TAG=$(echo git describe --exact-match --tags $(git log -n1 --pretty='%h'))" >> $GITHUB_ENV
 - name: use env var
    run: echo ${{ env.TAG }}

Upvotes: 10

Related Questions