26

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
  • 327
  • 1
  • 3
  • 8

3 Answers3

30

Now that set-env is deprecated you can use set-output to accomplish the same thing in this answer

- name: Retrieve version
  run: |
    echo "::set-output name=TAG_NAME::$(cat projectFile | grep -Po '(?<=Version>).*(?=</Version>)')"
  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/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions

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

w33b
  • 798
  • 1
  • 11
  • 26
23

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
  • 75,681
  • 63
  • 142
  • 179
  • 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
14

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
  • 75,681
  • 63
  • 142
  • 179
SolalVall
  • 246
  • 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
  • 4
    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