【问题标题】:Nested templates (calling a yaml file from another yaml file) in GitHub ActionsGitHub Actions 中的嵌套模板(从另一个 yaml 文件调用 yaml 文件)
【发布时间】:2021-01-11 06:21:21
【问题描述】:

GitHub 操作是否支持嵌套模板?例如,这里是一个调用另一个 yaml 文件的 Azure Pipeline yaml 示例:

- job: BuildFunctions
    
  steps:
  - ${{ each func in parameters.functionApps }}:
    - template: yaml/build-functionapps.yml
      parameters:

是否可以在 GitHub 操作中从另一个 yaml 文件调用 yaml 文件?

【问题讨论】:

标签: github github-actions


【解决方案1】:

您可以使用复合运行步骤操作。这些是仅在 YAML (documentation) 中定义的操作。

除了以前可用的run 步骤之外,您现在还可以指定容器、其他复合操作(深度最多为 9)和节点操作

节点动作可能是指叶动作,即不调用任何其他动作的动作。

来源:https://github.com/actions/runner/issues/646#issuecomment-901336347

工作流程

[...]

jobs:
  job:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: ./.github/workflows/composite-action

[...]

复合运行步骤操作
.github/workflows/composite-action/action.yml(与工作流相同的存储库)

name: "My composite action"
description: "Checks out the repository and does something"
runs:
  using: "composite"
  steps:
  - uses: actions/checkout@v2
  - uses: actions/setup-node@v2
    with:
      node-version: 12
  - run: npm test
    shell: bash
  - run: |
      echo "Executing action"
    shell: bash

旧限制:

复合运行步骤目前支持什么?

对于复合动作中的每个运行步骤,我们支持:

  • 姓名
  • 身份证
  • 运行
  • 环境
  • 外壳
  • 工作目录

此外,我们支持在整个动作中映射输入和输出。

请参阅docs 了解更多信息。

复合运行步骤不支持什么

我们目前不支持设置条件、错误继续、超时分钟、“使用”[备注:即使用其他操作]以及复合操作中各个步骤的机密。

(注意:我们确实支持在工作流中为使用复合运行步骤操作的步骤设置这些属性)

来源:https://github.com/actions/runner/issues/646

【讨论】:

  • 为什么actions/checkout必须在复合动作之前和复合动作内部调用?
  • @Merlin-they-them- 如果您在本地引用该操作(如本例中的uses: ./.github/workflows/composite-action),您必须先签出该操作,以便它在磁盘上并且可以在本地引用。在操作中,您不一定必须使用结帐。你也可以只运行一个脚本。
【解决方案2】:

不,不是。我在 GitHub 论坛中问了完全相同的问题:

是否可以通过工作流语法/YML 中的代码在没有 Docker 或 JS 的情况下创建/发布操作?

如文档中所述:目前,操作类型仅列出 Docker 容器和 JavaScript,所以没有这样的功能 达到你的要求。 来源:https://github.community/t/how-to-create-ready-to-use-action-without-docker-js/124889/2

这将简化为系统管理员的用户创建模板。

【讨论】:

    【解决方案3】:

    我认为使用复合动作模式,你可以实现你想要的。

    您需要定义您认为将在其他地方重用的步骤,并通过提供输入将其参数化。在我看来,它比模板在gitlab 或其他类似平台中的工作方式更强大。

    这样,您定义了一个函数,该函数可以接受输入,并根据这些输入为您完成工作。

    此外,即使文档建议,您应该将叶子操作创建为单独的公共存储库,并在您的基本操作中使用它 - 这不是必需的,您可以简单地使用如下结构(以我们的实时工作流程),并在您的工作流程中使用这些叶子操作-

    .github
      - actions
        - deploy-to-k8s
          - action.yaml
        - publish-image
          - action.yaml
      - workflows
        - deploy-from-pr.yaml <-- this will make use of all the actions defined
    

    deploy-from-pr.yaml 工作流程如下所示-

    name: deploy-from-pr
    
    on:
      pull_request:
        branches:
          - master
    
    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: ${{ github.repository }}
    
    jobs:
      deploy-from-pr:
        name: Deploy from PR to Development
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
    
          - name: Set version
            id: release
            run: echo ::set-output name=version::$(git describe --always)
    
          # custom action to build and push image
          - name: Build & publish image
            uses: ./.github/actions/publish-image # see how it's referred from same repo directly
            with:
              registry: ${{ env.REGISTRY }}
              registry_username: ${{ github.REGISTRY_USERNAME }}
              registry_password: ${{ secrets.REGISTRY_PASSWORD }}
              image_name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
              tag: ${{ steps.release.outputs.version }}
    
          # custom action to deploy into kubernetes
          - name: Deploy to kubernetes
            uses: ./.github/actions/deploy-to-k8s # see how it's referred from same repo directly
            with:
              digitalocean_token: ${{ secrets.DIGITALOCEAN_TOKEN }}
              cluster_name: ${{ secrets.CLUSTER_NAME }}
              overlay_path: .k8s/overlays/dev
              image_name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
              tag: ${{ steps.release.outputs.version }}
    

    Github Gist

    你可以查看deploy-to-k8s/action.yaml,看看它是怎么写的。

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 1970-01-01
      • 2022-08-13
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      相关资源
      最近更新 更多