Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the output of a bash command to Github Action parameter

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>)')
like image 746
user13288253 Avatar asked Apr 16 '20 17:04

user13288253


People also ask

How do I pass an environment variable in GitHub Actions?

Passing values between steps and jobs in a workflow If you generate a value in one step of a job, you can use the value in subsequent steps of the same job by assigning the value to an existing or new environment variable and then writing this to the GITHUB_ENV environment file.

Can you CD in GitHub Actions?

With GitHub Actions, you can trigger CI/CD workflows and pipelines of webhooks from these apps (even something simple, like a chat app message, if you've integrated your chat app into your GitHub repository, of course).


Video Answer


3 Answers

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

like image 149
w33b Avatar answered Oct 01 '22 04:10

w33b


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 }}"
like image 26
Sridhar Ratnakumar Avatar answered Oct 01 '22 02:10

Sridhar Ratnakumar


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 }}
like image 37
SolalVall Avatar answered Oct 01 '22 04:10

SolalVall