如何根据分支机构设置工作流环境变量 [英] How to set workflow env variables depending on branch

查看:45
本文介绍了如何根据分支机构设置工作流环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个GitHub动作工作流文件,它们几乎相同,只有一个配置为对分支 master 上的push / pull_requests做出反应,另一个在生产


分段工作流程如下所示:

  env:
GCLOUD_PROJECT:my-project-stg
VERCEL_TARGET:暂存

上:
推送:
分支机构:[master ]
pull_request:
分支机构:[master]

生产工作流程如下:

  env:
GCLOUD_PROJECT:my-project-prd
VERCEL_TARGET:生产

开:
推:
分支:[生产]
pull_request:
分支:[生产]

其余的工作流程文件是相同的,因此显然不是很干。


我想拥有1个单个工作流程文件并以某种方式进行切换赌注基于分支名称的两组变量之间。有没有一种方法可以实现这一目标,或者我可能是从错误的角度来解决这一问题?

解决方案

无法从作业设置工作流级环境变量。每个作业都在自己的计算机上运行,​​并且在其中设置env变量只会影响该作业的所有后续步骤。您可以创建一个工件并使用它,或者声明并设置作业输出。后者适用于字符串值。


这里是一个示例:

  name : Main; 

上:
推送:
分支机构:
-主
-生产
pull_request:
分支机构:
-主
-生产

工作:
初始运行:
运行:ubuntu最新
输出:
gcloud_project:$ {{ steps.setvars.outputs.gcloud_project}}
阶段:$ {{steps.setvars.outputs.phase}}

步骤:
-名称:取消上一个工作流程
使用:styfle/cancel-workflow-action@0.4.0
与:
access_token:$ {{github.token}}

-名称:设置变量
id:setvars
运行:|
if [[" $ {{github.base_ref}}" ==大师; || " $ {{{github.ref}}" ==参考/负责人/主 ]];然后
echo :: set-output name = gcloud_project :: my-project-dev
echo :: set-output name = phase :: staging;
fi

if [[" $ {{github.base_ref}}" ==生产 || " $ {{{github.ref}}" ==参考/标题/制作; ]];然后
echo :: set-output name = gcloud_project :: my-project-prd
echo :: set-output name = phase :: production;
fi

打印:
运行:ubuntu最新
需要:init
步骤:
-名称:Print
运行:echo gcloud_project = $ {{needs.init.outputs.gcloud_project}}


I currently have two GitHub actions workflow files that are pretty much identical, only one is configured to react to push/pull_requests on branch master the other on production.

The staging workflow starts like this:

env:
  GCLOUD_PROJECT: my-project-stg
  VERCEL_TARGET: staging

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

The production workflow starts like:

env:
  GCLOUD_PROJECT: my-project-prd
  VERCEL_TARGET: production

on:
  push:
    branches: [ production ]
  pull_request:
    branches: [ production ]

The rest of the workflow files is the same, so this is clearly not very DRY.

I would like to have 1 single workflow file and somehow switch between two sets of variables based on the branch name. Is there a way to achieve this or am I maybe approach this from the wrong angle?

If it were possible to extend both workflow files on a shared base definition that would also solve it I guess.

解决方案

It is not possible to set workflow-level environment variables from a job. Each job runs in its own machine and setting an env variable there only affects all of the later steps in that job.

There are currently two ways to share data between jobs; either you create an artifact and use that, or you declare and set job outputs. The latter works for string values.

Here's an example:

name: "Main"

on:
  push:
    branches:
      - master
      - production
  pull_request:
    branches:
      - master
      - production

jobs:
  init:
    runs-on: ubuntu-latest
    outputs:
      gcloud_project: ${{ steps.setvars.outputs.gcloud_project }}
      phase: ${{ steps.setvars.outputs.phase }}

    steps:
      - name: Cancel previous workflow
        uses: styfle/cancel-workflow-action@0.4.0
        with:
          access_token: ${{ github.token }}

      - name: Set variables
        id: setvars
        run: |
          if [[ "${{github.base_ref}}" == "master" || "${{github.ref}}" == "refs/heads/master" ]]; then
            echo "::set-output name=gcloud_project::my-project-dev"
            echo "::set-output name=phase::staging"
          fi

          if [[ "${{github.base_ref}}" == "production" || "${{github.ref}}" == "refs/heads/production" ]]; then
            echo "::set-output name=gcloud_project::my-project-prd"
            echo "::set-output name=phase::production"
          fi

  print:
    runs-on: ubuntu-latest
    needs: init
    steps:
      - name: Print
        run: echo "gcloud_project=${{needs.init.outputs.gcloud_project}}"
       

这篇关于如何根据分支机构设置工作流环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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