【问题标题】:How to combine two action.yml files in one repo?如何在一个仓库中合并两个 action.yml 文件?
【发布时间】:2020-04-03 04:48:21
【问题描述】:

我希望我的仓库有 2 个功能:

  1. 在标签推送上创建发布
  2. 在 docker 环境中测试我的软件

两者都需要 repo 中的 action.yml。如何组合它们?

name: "Upload a Release Asset"
description: "Upload a release asset to an existing release on your repository"
author: "Github"
inputs:
  upload_url:
    description: "The URL for uploading assets to the release"
    required: true
  asset_path:
    description: "The path to the asset you want to upload"
    required: true
  asset_name:
    description: "The name of the asset you want to upload"
    required: true
  asset_content_type:
    description: "The content-type of the asset you want to upload. See the supported Media Types here: https://www.iana.org/assignments/media-types/media-types.xhtml for more information"
    required: true
outputs:
  browser_download_url:
    description: "The URL users can navigate to in order to download the uploaded asset"
runs:
  using: "node12"
  main: "dist/index.js"
branding:
  icon: "package"
  color: "gray-dark"
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.who-to-greet }}

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    @chenghopan!如果你想在同一个存储库中有两个操作,它们应该位于不同的目录中。

    但是,action.yml 文件不是必需的。

    如果您计划在 GitHub Marketplace 中列出该文件,则该文件仅是必需的

    如果您在同一个 repo 中有操作,它们可以拥有自己的 action.yml 文件以及它们的 Dockerfile 或节点脚本。这是一个包含两个 dockerfile 的示例:

    .
    ├── README.md
    ├── .github
    │   └── workflows
    │       └── main.yml
    ├── action1
    │   ├── Dockerfile
    │   ├── action.yml
    │   └── entrypoint.sh
    └── action2
        ├── Dockerfile
        ├── action.yml
        └── entrypoint.sh
    

    这是在同一个 repo 中调用两个操作的工作流:

    name: Test two actions
    on: [push]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v1
          - uses: ./action1
          - uses: ./action2
    

    这是调用操作的不同 repo 中的工作流:

    name: Test two actions
    on: [push]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: managedkaos/github-actions-two-actions/action1@master
          - uses: managedkaos/github-actions-two-actions/action2@master
    

    如果您可以不在 GitHub Marketplace 中列出操作,只需将 action.yml 文件与操作放在同一目录中即可!

    作为参考,您可以在此处找到这些示例中的代码:

    1. https://github.com/managedkaos/github-actions-two-actions
    2. https://github.com/managedkaos/test-two-actions

    【讨论】:

    • ...entrypoint.sh 是什么?
    • @TSR 我不会复制和粘贴文档中的所有内容,但请查看此链接:docs.github.com/en/actions/creating-actions/…“Docker ENTRYPOINT 指令具有 shell 形式和 exec 形式。D​​ocker ENTRYPOINT 文档建议使用 ENTRYPOINT 指令的 exec 形式。有关 exec 和 shell 形式的更多信息,请参阅 Docker 文档中的 ENTRYPOINT 参考。"
    • 比公认的答案好得多。谢谢。
    • 这个答案非常有帮助,为了进一步扩展它,您可以将操作放入“actions/one/action.yml”和“actions/two/action.yml”等子目录中,这样你就不会必须有多个顶级子目录。
    • 当然。只要使用 Dockerfile 的完整路径正确引用它们,就可以将操作放在 repo 中的任何目录中。
    【解决方案2】:

    两者都需要 repo 中的 action.yml。如何组合它们?

    您可以将每个操作留在各自独立的 GitHub 操作存储库中。

    并且,自 2020 年 8 月起,将它们合并

    见:

    GitHub Actions: Composite Run Steps

    您现在可以使用 shell 脚本创建可重用的操作,甚至可以在同一个操作中混合使用多种 shell 语言。
    您可能有很多 shell 脚本来自动执行许多任务,现在您可以轻松地将它们变成一个动作并将它们重用于不同的工作流程。有时只编写一个 shell 脚本比编写 JavaScript 或 Docker 更容易。
    现在您不必担心将脚本包装在 Docker 容器中。

    以下是如何使用复合运行步骤操作的示例:

    workflow.yml:

    jobs:
     build:
       runs-on: windows-latest
       steps:
       - uses: actions/checkout@v2
       - uses: octocat/say-hello@v1
         with: 
           name: OctoCat
    

    octocat/say-hello/action.yml:

    inputs:
     name: 
       description: 'Your name'
       default: 'No name provided'
    runs:
     using: "composite"
     steps: 
       - run: echo Hello ${{ inputs.name }}.
         shell: bash
       - run: echo "Nice to meet you!"
         shell: pwsh
    

    了解有关 composite run steps 的更多信息,并访问GitHub Actions community forum 了解问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 2010-11-07
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 2023-02-08
      相关资源
      最近更新 更多