gpt4 book ai didi

github - 在 actions/checkout@v2 中使用环境变量作为引用

转载 作者:行者123 更新时间:2023-12-05 05:59:48 28 4
gpt4 key购买 nike

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

1 之后我想查看分支机构。问题是我似乎无法在“ref”上使用 env 变量来检查它。

有办法吗?或者 github 还不支持这个。

示例代码:

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

[编辑] 答案:

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

  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"

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

  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

最佳答案

This question问了同样的事情。

这里要用的不是env variables但是outputs .

工作输出

You can specify a set of outputs that you want to pass to subsequent jobs and then access those values from your needs context.

参见 documentation :

jobs.<jobs_id>.outputs

作业输出图

Job outputs are available to all downstream jobs that depend on this job.
For more information on defining job dependencies, see jobs.<job_id>.needs.

Job outputs are strings, and job outputs containing expressions are evaluated on the runner at the end of each job. Outputs containing secrets are redacted on the runner and not sent to GitHub Actions.

To use job outputs in a dependent job, you can use the needs context.
For more information, see "Context and expression syntax for GitHub Actions."

To use job outputs in a dependent job, you can use the needs context.

示例

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}}

关于github - 在 actions/checkout@v2 中使用环境变量作为引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67957150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com