如何从同一分支调用 GitHub 可重用工作流?

huangapple go评论61阅读模式
英文:

How do I call a GitHub reusable workflow from the same branch?

问题

我有一个存储库,其中包含一些自定义操作和可重用工作流程。为了测试对这些工件的任何更改,我想从同一存储库中的工作流中调用它们,并验证它们是否按预期运行。

这里的一个重要点是,我希望从与正在运行PR的相同分支中调用它们。原因很简单,我想测试我在当前分支中正在进行的更改。

以下是我想出的方法:

---
    name: '测试自定义操作和可重用工作流程'

    on:
      pull_request:
        branches:
          - main
  
    jobs:
      call-custom-action:
        runs-on: [self-hosted, default]
        steps:
          - name: 获取环境变量
            id: get-env-vars
            # owner/repo 是此工作流所在的存储库
            uses: owner/repo/actions/my-custom-action@${{ env.GITHUB_HEAD_REF }}
  
      call-reusable-workflow:
        uses: owner/repo/.github/workflows/my-reusable-workflow.yml@${{ env.GITHUB_HEAD_REF }}

当我提交并推送此工作流时,它会出现错误:

无效的工作流文件:.github/workflows/test-reusable-workflow.yml#L19。
错误解析被调用的工作流
".github/workflows/test-reusable-workflow.yml"
-> "owner/repo/.github/workflows/my-reusable-workflow.yml@${{env.GITHUB_HEAD_REF}}"
: 无法获取工作流程:工作流程的引用应为有效的分支、标签或提交

我尝试将有问题的行更改为

        uses: owner/repo/.github/workflows/my-reusable-workflow.yml

但是失败了,显示以下错误:

无效的工作流引用值:未指定版本

正如可以观察到的那样,似乎没有一种通过表达式引用版本来调用可重用工作流的方法。是否有一种从相同分支调用可重用工作流的方法?

英文:

I have a repository that contains some custom actions and reusable workflows. In order to test any changes to those artefacts I would like to call them from a workflow in the same repository and verify that they behave as expected.

One important point here is that I want to call them from the same branch that the PR is running for. The reason is simple, I want to test the changes that I am making in the current branch.

Here is what I have come up with:

---
    name: 'Test custom actions and reusable workflows'

    on:
      pull_request:
        branches:
          - main
  
    jobs:
      call-custom-action:
        runs-on: [ self-hosted, default ]
        steps:
          - name: get env vars
            id: get-env-vars
            # owner/repo is the repo that this workflow resides in
            uses: owner/repo/actions/my-custom-action@${{ env.GITHUB_HEAD_REF }}
  
      call-reusable-workflow:
        uses: owner/repo/.github/workflows/my-reusable-workflow.yml@${{ env.GITHUB_HEAD_REF }}

When I commit and push this workflow it fails with error:

> Invalid workflow file: .github/workflows/test-reusable-workflow.yml#L19.
> error parsing called workflow
".github/workflows/test-reusable-workflow.yml"
> -> "owner/repo/.github/workflows/my-reusable-workflow.yml@${{env.GITHUB_HEAD_REF}}"
> : failed to fetch workflow: reference to workflow should be either a valid branch, tag, or commit

I tried changing the offending line to

        uses: owner/repo/.github/workflows/my-reusable-workflow.yml

but that failed with:

> invalid value workflow reference: no version specified

As can be observed there doesn't seem to be a way to call a reusable workflow by referring to the version using an expression. Is there a way to call reusable workflow from the same branch?

答案1

得分: 1

你可以通过提供相对路径来调用当前签出的分支上的工作流程(参见文档):

        uses: ./.github/workflows/my-reusable-workflow.yml
英文:

You can call a workflow on the currently checked out branch by providing a relative path (see docs):

        uses: ./.github/workflows/my-reusable-workflow.yml

huangapple
  • 本文由 发表于 2023年7月4日 23:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定