How do i set an env var with a bash expression in

2020-02-09 13:37发布

问题:

In GitHub Actions i'd like to evaluate a bash expression and then assign it to an environment variable -

    - name: Tag image
      env:
        GITHUB_SHA_SHORT: ${{ $(echo $GITHUB_SHA | cut -c 1-6) }}
      ..do other things...

However this naive attempt has failed. According to the docs this doesn't seem to be supported however a somewhat clean workaround would be fine.

回答1:

Using set-env in a previous step works for me.

name: my workflow
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Set env
      run: echo ::set-env name=GITHUB_SHA_SHORT::$(echo $GITHUB_SHA | cut -c 1-6)
    - name: Test
      run: echo $GITHUB_SHA_SHORT

Set an environment variable: set-env ::set-env name={name}::{value}

Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.

(From https://help.github.com/en/articles/development-tools-for-github-actions#set-an-environment-variable-set-env)

This is an alternative way to reference the environment variable in workflows.

    - name: Test
      run: echo ${{ env.GITHUB_SHA_SHORT }}