60

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>)')

3 Answers 3

78

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

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

2 Comments

GitHub is also deprecating set-output - see github.blog/changelog/… 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
The deprecation has been postponed as there is still significant use of save-state and set-output - ref github.blog/changelog/…
40

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 }}"

4 Comments

Thank you for providing this example as I couldn't see any useful example code in the linked section in the docs.
this is now (Oct. 2022) the preferred way as GitHub is deprecating both set-env and set-output, see github.blog/changelog/…
This unfortunately does not work further as the output of git status is a multi-line output and as such the handling of thereof has to look a bit different. Note further, I tried to retrieve the second-to-last tag to calculate the changes in the latest release via git tag --sort=-v:refname | sed -n '2p' and no matter what I tried so far, the env-variable I assign the output to remains an empty string, even though the output should be a single line
@RomanVottner By default, actions/checkout does not pull down the tags. Make sure you've done that and that the output of the command is correct without setting a var. Setting the var is the easy part.
19

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 }}

3 Comments

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)*(?=</PackageReleaseNotes>)', however only the first line gets set in the variable.
The following link should help you to handle multilines: (github.community/t5/GitHub-Actions/…) You would have something like: run: | RELEASE_NOTES="$(grep -Pzo '(?<=PackageReleaseNotes>)(.|\n)*(?=</PackageReleaseNotes>)' 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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.