如何在GitHub Actions中使用bash表达式设置env var? [英] How do I set an env var with a bash expression in GitHub Actions?

查看:652
本文介绍了如何在GitHub Actions中使用bash表达式设置env var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GitHub Actions中,我想评估一个bash表达式,然后将其分配给一个环境变量:

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; a somewhat clean workaround would be fine.

推荐答案

该问题的原始答案使用了Actions运行器功能set-env.由于安全性漏洞 set-env已被弃用,不应再使用.

The original answer to this question used the Actions runner function set-env. Due to a security vulnerability set-env is being deprecated and should no longer be used.

这是设置环境变量的新方法.

This is the new way to set environment variables.

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

设置环境变量 echo "{name}={value}" >> $GITHUB_ENV

Setting an environment variable echo "{name}={value}" >> $GITHUB_ENV

为作业中接下来运行的任何操作创建或更新环境变量.创建或更新环境变量的操作无法访问新值,但是作业中的所有后续操作都可以访问.环境变量区分大小写,并且可以包含标点符号.

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.

(来自 https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable )

这是在工作流程中引用环境变量的另一种方法.

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

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

这篇关于如何在GitHub Actions中使用bash表达式设置env var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆