在 actions/checkout@v2 中使用 env 变量作为引用

Sha*_*aiB 5 github github-actions

我有一个工作流程,它创建一个新分支,其名称保存为环境变量。原因是我需要工作流程在新的干净分支上运行。

1 工作之后我想去看看分行。问题是我似乎无法在“ref”上使用环境变量来检查它。

有没有办法做到这一点 ?或者 github 还不支持这个吗?

示例代码:

  - name: Checkout to branch
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: $NEW_BRANCH_NAME
Run Code Online (Sandbox Code Playgroud)

[编辑] 答案:

对于遇到同样问题的任何人,这就是我最终的做法:首先我创建了一个仅创建并推送一个新分支的作业:

  Create-new-Branch:
runs-on: [ self-hosted ]
outputs:
  branch_name: ${{ steps.create-branch.outputs.BRANCH_NAME}}

steps:
  - name: Checkout master
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: master

  - name: create and push branch
    id: create-branch
    run: |
      export TEST="new-branch-name-`date '+%F'`"
      git checkout -b $TEST origin/master
      git push -u origin $TEST
      echo "::set-output name=BRANCH_NAME::$TEST"
Run Code Online (Sandbox Code Playgroud)

然后在下一个作业中,当我想使用它时,我使用上面作业的作业输出作为存储库名称

  Job-Name:
needs: Create-new-Branch
runs-on: [self-hosted]

steps:

  - name: Checkout to branch
    uses: actions/checkout@v2
    with:
      repository: org/repo
      token: ${{secrets.GIT_TOKEN}}
      ref: ${{needs.Create-new-Branch.outputs.branch_name}} # the new branch name
Run Code Online (Sandbox Code Playgroud)

Gui*_*urd 1

这个问题问了同样的事情。

你想在这里使用的不是env variablesbut outputs

工作产出

您可以指定一组要传递给后续作业的输出,然后从您的需求上下文中访问这些值。

请参阅文档

jobs.<jobs_id>.outputs
Run Code Online (Sandbox Code Playgroud)

作业的输出图

作业输出可用于依赖于该作业的所有下游作业。
有关定义作业依赖性的更多信息,请参阅jobs.<job_id>.needs

作业输出是字符串,包含表达式的作业输出在每个作业结束时在运行器上进行评估。包含机密的输出会在运行器上进行编辑,不会发送到 GitHub Actions。

要在依赖作业中使用作业输出,您可以使用上下文needs
有关更多信息,请参阅“ GitHub Actions 的上下文和表达式语法。

要在依赖作业中使用作业输出,您可以使用需求上下文。

例子

jobs:
  job1:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      output1: ${{ steps.step1.outputs.test }}
    steps:
    - id: step1
      run: echo "::set-output name=test::hello-world"
   
  job2:
    runs-on: ubuntu-latest
    needs: job1
    steps:
    - run: echo ${{needs.job1.outputs.output1}}
Run Code Online (Sandbox Code Playgroud)