指定GitHub父工作流中子工作流的运行标签的方式是什么?

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

Way to specify runs-on labels of sub workflow in parent GitHub workflow?

问题

我有一些GitHub工作流程,分为父工作流程和可重用的工作流程。

父工作流程是“build”和“deploy”,在不同情况下触发,并使用不同的输入参数调用子工作流程。

子工作流程基本上是包含多个步骤的构建脚本,如检出、获取其他文件,然后在Mac或Windows机器上构建。

问题是,我想从父脚本中指定子工作流程将在哪个运行程序上运行。

当从父脚本“deploy.yml”和“build.yml”中调用时,我想在不同的运行程序上运行构建子工作流程。我已经设置了两个自托管的运行程序,每个都具有不同的标签,比如“[self-hosted, macOS]”和“[self-hosted, macOS, deploy]”。

我尝试向子工作流程添加一个字符串输入参数,然后像这样使用它:

  MacBuild:
    name: Mac build
    uses: ./.github/workflows/build-mac.yml
    with:
      build_installer: true
      installer_artifact: true
      runner_tags: "[self-hosted, macOS, deploy]"
    secrets: inherit

然后在子工作流程中使用它:

 runs-on: ${{ inputs.runner_tags }}

实际上,它成功解析,显示“Requested labels: [self-hosted, macOS, deploy]”,但运行程序实际上从未接手作业,即使它处于空闲状态。

是否有一种方法可以实现我想要的?

这是“deploy.yml”文件:

name: Deploy

on:
  push:

jobs:

  MacBuild:
    name: Mac build
    uses: ./.github/workflows/build-mac.yml
    with:
      build_installer: true
      installer_artifact: true
    secrets: inherit

这是“build-mac.yml”文件:

name: Build Release Mac

on:
  workflow_call:
    inputs:
      build_installer:
        description: 'Sign products and build installer'
        type: boolean
        required: false
        default: false
      installer_artifact:
        description: 'Upload installer to artifacts'
        type: boolean
        required: false
        default: false

jobs:
  MacBuild:
    name: macOS Build Release
    runs-on: [self-hosted, macOS]
    steps:

      - name: Checkout Repository
        uses: actions/checkout@main
        with:
          path: ${{ github.event.repository.name }}

      - name: Check out other repository
        uses: actions/checkout@main
        with:
         repository: organization/other_repository
         token: ${{ secrets.RUNNERS_PAT }}
         path: other_repository
         ref: development   # branch

已尝试以下方式将运行程序标签传递为字符串参数:

  • "self-hosted,macOS,deploy"
  • "self-hosted, macOS, deploy"
  • "[self-hosted, macOS, deploy]"
    但都没有成功,运行程序未接手作业。
英文:

I have a couple of GitHub workflows divided into parent ones and reusable workflows.

The parents are build and deploy, each triggered at different circumstances and calling the subworkflows with different input parameters.

Subworkflows are basically build scripts that consist of several steps, like checkouts, fetching other artifacts and then building on mac or windows machine.

The problem is that I would like to specify on what runner the sub workflow is going to run from the parent script.

I would like to run the build subworkflow on a different runner when called from parent deploy.yml and from build.yml. I have setup 2 self-hosted runners, each with different labels, lets say [self-hosted, macOS] and [self-hosted, macOS, deploy].

I tried adding a string input parameter to the subworkflow and then used it like this:

  MacBuild:
    name: Mac build
    uses: ./.github/workflows/build-mac.yml
    with:
      build_installer: true
      installer_artifact: true
      runner_tags: "[self-hosted, macOS, deploy]"
    secrets: inherit

then in sub workflow

 runs-on: ${{ inputs.runner_tags }}

and it actually parsed it OK, saying that "Requested labels: [self-hosted, macOS, deploy]", but the runner never actually picked up the job, even it was idle.

Is there a way to do what I want?


This is the deploy.yml file:

name: Deploy

on:
  push:

jobs:          

  MacBuild:
    name: Mac build
    uses: ./.github/workflows/build-mac.yml
    with:
      build_installer: true
      installer_artifact: true
    secrets: inherit

This is the build-mac.yml file:

name: Build Release Mac

on:
  workflow_call:
    inputs:
      build_installer:
        description: 'Sign products and build isntaller'
        type: boolean
        required: false
        default: false
      installer_artifact:
        description: 'Upload installer to artifacts'
        type: boolean
        required: false
        default: false

  
jobs:
  MacBuild:
    name: macOS Build Release
    runs-on: [self-hosted, macOS]
    steps:
      
      - name: Checkout Repository
        uses: actions/checkout@main
        with:
          path: ${{ github.event.repository.name }}   

      - name: Check out other repository
        uses: actions/checkout@main
        with:
         repository: organization/other_repository
         token: ${{ secrets.RUNNERS_PAT }}
         path: other_repository
         ref: development   # branch

Already tried passing the runner labels as string parameter these ways:

  • "self-hosted,macOS,deploy"
  • "self-hosted, macOS, deploy"
  • "[self-hosted, macOS, deploy]"

didn't work, runner didn't pick up the job.

答案1

得分: 0

重复(相似)于https://stackoverflow.com/questions/73241812/how-to-dynamically-assign-the-runs-on-value-in-the-github-actions

这个答案有所帮助:
https://stackoverflow.com/a/73253764/19599681

我使用了另一个子工作流的输入参数 - 布尔值,来决定使用哪个运行器标志,所以脚本如下:

name: 构建 macOS 发布版

on:
  workflow_call:
    inputs:
      build_installer:
        description: '签名产品并构建安装程序'
        type: boolean
        required: false
        default: false
      installer_artifact:
        description: '上传安装程序到存储库'
        type: boolean
        required: false
        default: false
      deploy_runner:
        description: '使用部署运行器'
        type: boolean
        required: false
        default: false

jobs:
  MacBuild:
    name: macOS 构建发布版
    runs-on: ${{ (inputs.deploy_runner) && fromJSON('[ "self-hosted", "macOS", "deploy" ]') || fromJSON('[ "self-hosted", "macOS"]') }}

我认为只需将带有标签的字符串作为输入参数发送,并在此处使用 fromJSON() 进行解析可能是可行的,但我尚未测试过。

英文:

Duplicate (similar) to https://stackoverflow.com/questions/73241812/how-to-dynamically-assign-the-runs-on-value-in-the-github-actions

This answer helped:
https://stackoverflow.com/a/73253764/19599681

I used another sub workflow input parameter - bool, to decide what runner flags to use, so the script looks like this:

name: Build Mac Release

on:
  workflow_call:
    inputs:
      build_installer:
        description: 'Sign products and build isntaller'
        type: boolean
        required: false
        default: false
      installer_artifact:
        description: 'Upload installer to artifacts'
        type: boolean
        required: false
        default: false
      deploy_runner:
        description: 'Use deploy runner'
        type: boolean
        required: false
        default: false
  
jobs:
  MacBuild:
    name: macOS Build Release
    runs-on: ${{ (inputs.deploy_runner) && fromJSON('[ "self-hosted", "macOS", "deploy" ]') || fromJSON('[ "self-hosted", "macOS"]') }}

I believe it could be possible to just send the string with tags as an input parameter and parse it here by fromJSON(), but I have not tested that.

huangapple
  • 本文由 发表于 2023年3月20日 23:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792224.html
匿名

发表评论

匿名网友

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

确定