45

I have a workflow where after a push to master I want to create a release and upload an asset to it.
I'm using actions/create-release@v1 and actions/upload-release-asset@v1.

I would like to pass the outputs of a bash commands to the action parameters. However I found out the syntax of "$(command)" does not work.

How can I pass the output of a bash command to an action's parameter.

For example I'd like to do something like this:

- name: Create Release
  id: create_release
  uses: actions/create-release@v1
  env:
    GITHUB_TOKEN: ${{ secrets.token }}
  with:
    tag_name: $(cat projectFile | grep -Po '(?<=Version>).*(?=</Version>)')
user13288253
  • 513
  • 1
  • 4
  • 9

3 Answers3

59

Update: set-output is being deprecated as well "Starting 1st June 2023 workflows using save-state or set-output commands via stdout will fail with an error."

Now that set-env is deprecated and set-output is soon to be deprecated, you can use GITHUB_OUTPUT environment files: to accomplish the same thing in this answer

- name: Retrieve version
  run: |
    echo "TAG_NAME=$(cat projectFile | grep -Po '(?<=Version>).*(?=</Version>)')" >> $GITHUB_OUTPUT
  id: version

- name: Create Release
  id: create_release
  uses: actions/create-release@v1
  env:
    GITHUB_TOKEN: ${{ secrets.token }}
  with:
    tag_name: ${{ steps.version.outputs.TAG_NAME }}

References:

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

How to save the output of a bash command to output parameter in github actions

l33tHax0r
  • 1,384
  • 1
  • 15
  • 31
  • 2
    GitHub is also deprecating set-output - see https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ instead use `echo "{name}={value}" >> $GITHUB_OUTPUT` where in the example above `{name}` would be `TAG_NAME` and the `{value}` would be the `$(...` command output – Sandra Oct 28 '22 at 15:34
35

Use environment files

steps:
  - name: Set the value
    id: step_one
    run: |
        echo "FOO=$(git status)" >> $GITHUB_ENV
  - name: Use the value
    id: step_two
    run: |
        echo "${{ env.FOO }}"
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
  • Thank you for providing this example as I couldn't see any useful example code in the linked section in the docs. – Tobias Feil Nov 29 '21 at 16:45
  • 1
    this is now (Oct. 2022) the preferred way as GitHub is deprecating both `set-env` and `set-output`, see https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ – Sandra Oct 28 '22 at 15:36
16

UPDATE: This answer will not work as GitHub as disabled this syntax for security reasons. You should use environment files instead.

I would create an environment variable based of your command output:

- name: Retrieve version
  run: |
    echo ::set-env name=TAG_NAME::$(cat projectFile | grep -Po '(?<=Version>).*(?=</Version>)')

And then access it like the following:

- name: Create Release
  id: create_release
  uses: actions/create-release@v1
  env:
    GITHUB_TOKEN: ${{ secrets.token }}
  with:
    tag_name: ${{ env.TAG_NAME }}
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
SolalVall
  • 292
  • 2
  • 5
  • Wait, there's one problem, I also want to get the release notes, which have newlines in them and I use `cat projectFile | grep -Pzo '(?<=PackageReleaseNotes>)(.|\n)*(?=)'`, however only the first line gets set in the variable. – user13288253 Apr 16 '20 at 20:51
  • The following link should help you to handle multilines: (https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/52466#M8527) You would have something like: `run: | RELEASE_NOTES="$(grep -Pzo '(?<=PackageReleaseNotes>)(.|\n)*(?=)' projectFile)" RELEASE_NOTES="${RELEASE_NOTES//'%'/'%25'}" RELEASE_NOTES="${RELEASE_NOTES//$'\n'/'%0A'}" RELEASE_NOTES="${RELEASE_NOTES//$'\r'/'%0D'}" echo "::set-env name=RELEASE_NOTES::$RELEASE_NOTES"` – SolalVall Apr 16 '20 at 22:06
  • 6
    Note ::set-env is now deprecated - https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files – rantoniuk Oct 26 '20 at 11:16