Reputation: 23064
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?
Upvotes: 10
Views: 12285
Reputation: 312430
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.
Upvotes: 15