【问题标题】:Does Github Actions have templatesGithub Actions 有模板吗
【发布时间】:2020-04-01 11:48:17
【问题描述】:

由于我的 Github Actions 中有重复的步骤,我想创建一个模板。举个例子吧

name: ci
on: ["push"]

jobs:
  build-and-test:
    strategy:
      matrix:
        os: [ubuntu-latest]

    runs-on: ${{ matrix.os }}
    steps:
      - name: checkout
        uses: actions/checkout@v1

      - name: do stuff
        run: |
          bash stuff

是否可以仅将步骤保存在单独的文件中?然后导入?

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    不幸的是,github-actions 似乎不支持重用工作流程。 Not even YAML anchors are supported.

    看来共享步骤(不是设置)的唯一方法是create actions

    更新:一场风暴正在酝酿中

    我也有caught wind 重用动作的可能性。关注issue 以保持最新状态。

    【讨论】:

    • 谢谢。在这种情况下,创建一个动作对我来说太费力了。
    • @RamonMedeiros 确实很不幸,但是当有任何变化时我会更新这个答案
    【解决方案2】:

    我在“Reuse portion of GitHub action across jobs”中提到,现在(2021 年 10 月)可以重用 GitHub Worfflow。

    文档“Reusing workflows”包含一个“Reusable workflows and workflow templates”部分,该部分导致“Creating workflow templates

    如果需要引用仓库的默认分支,可以使用$default-branch 占位符。

    当使用您的模板创建工作流时,占位符将自动替换为存储库默认分支的名称。

    例如,这个名为 octo-organization-ci.yml 的文件演示了一个基本的工作流程。

    name: Octo Organization CI
    
    on:
      push:
        branches: [ $default-branch ]
      pull_request:
        branches: [ $default-branch ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v2
    
          - name: Run a one-line script
            run: echo Hello from Octo Organization
    

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2023-03-25
      • 2020-01-14
      • 2019-11-28
      • 2021-08-30
      • 2020-04-15
      • 2020-09-06
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多