如何在一个仓库中合并两个 action.yml 文件?

14

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

  1. 在标签推送时创建一个发布版本
  2. 在 Docker 环境中测试我的软件

这两个功能都需要在仓库中有一个 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 }}
2个回答

42

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

但是,action.yml文件并非必需品。

仅当您计划在GitHub Marketplace中列出操作时,此文件才是必需的。

如果您将操作放在同一存储库中,则可以将它们各自的action.yml文件与其Dockerfile或节点脚本一起放置。以下是具有两个Dockerfile的示例:

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

这里是同一个仓库中调用两个操作的工作流程:

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

这里有一个在不同存储库中调用该操作的工作流程:

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
比被接受的答案好多了。谢谢。 - afilina
这个答案非常有帮助,如果想进一步扩展,可以将操作放入子目录中,例如 "actions/one/action.yml" 和 "actions/two/action.yml",这样就不必拥有多个顶级子目录。 - Kevin P. Fleming
当然可以。只要您使用完整的Dockerfile路径正确引用它们,您可以将操作放在存储库中的任何目录中。 - Michael J
1
@Nakilon 如果您正在尝试将两个操作合并为一个操作,并在Marketplace中发布组合操作,则应考虑使用复合操作:https://docs.github.com/en/actions/creating-actions/creating-a-composite-action。我会诚实地说,我没有尝试过这种方法,但这是我能想到的唯一适用于必须将两个操作组合并且全部打包发布的存储库的方法。但是,我认为一旦您达到那个点,最好使用可重用的工作流程:https://github.blog/2022-02-10-using-reusable-workflows-github-actions/。 - Michael J
显示剩余3条评论

9
两者都需要在仓库中有一个 action.yml 文件,我该如何将它们合并?您可以将每个操作留在它们自己的 GitHub Action 仓库中。自 2020 年 8 月起,可以将其合并。参见:

GitHub Actions: Composite Run Steps

You can now create reusable actions using shell scripts and even mix multiple shell languages in the same action.
You probably have a lot of shell script to automate many tasks, now you can easily turn them into an action and reuse them for different workflows. Sometimes it's easier to just write a shell script than JavaScript or Docker.
Now you don't have to worry about wrapping it up your scripts in a Docker containers.

Here's an example of how you can use composite run steps actions:

Composite Run Step Example -- https://i2.wp.com/user-images.githubusercontent.com/8660827/89449714-7bdd0480-d727-11ea-9863-7486fe666fd5.png?ssl=1

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

Learn more about composite run steps and visit the GitHub Actions community forum for questions.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接