10

Goal:

In GitHub Actions, to define my commit message dynamically from shell:

      - name: Commit changes
        uses: EndBug/add-and-commit@v7
        with:
          message: "added on $(date -I)"

However, it seems that I have to define a environment variable then use it. I'm following How do I set an env var with a bash expression in GitHub Actions? and other help files like this, but still cannot tell how to make use of such environment variable that I've define previously. This is what I tried but failed:

      - name: Checkout repo
        uses: actions/checkout@v2
      - run: |
          touch sample.js
          echo "today=$(date -I)" >> $GITHUB_ENV

      - name: Commit changes
        uses: EndBug/add-and-commit@v7
        with:
          message: "added on ${today}"

How to make it work?

1 Answer 1

15

If you want to reference an environment variable set using the $GITHUB_ENV environment file in the arguments to another task, you'll need to use workflow syntax to access the appropriate key of the top level env key, like this:

- name: Commit changes
  uses: EndBug/add-and-commit@v7
  with:
    message: "added on ${{env.today}}"

You can access it as a standard environment from inside of a running task, for example:

- name: Show an environment variable
  run: |
      echo "today is $today"

In that example, the expression $today is expanded by the shell, which looks up the environment variable named today. You could also write:

- name: Show an environment variable
  run: |
      echo "today is ${{env.today}}"

In this case, the expansion would be performed by github's workflow engine before the run commands execute, so the shell would see a literal command that looks like echo "today is 2021-07-14".


You can accomplish something similar using output parameters, like this:

- name: "Set an output parameter"
  id: set_today
  run: |
    echo "::set-output name=today::$(date -I)"

- name: Commit changes
  uses: EndBug/add-and-commit@v7
  with:
    message: "added on ${{steps.set_today.outputs.today}}"

Using output parameters is a little more granular (because they are qualified by the step id), and they won't show up in the environment of processes started by your tasks.

Sign up to request clarification or add additional context in comments.

1 Comment

Kudo! to have answered the things that I'd like to ask but don't know they exist.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.