如何在我的 GitHub Action 的 action.yml 文件中引用其他操作?

19

我可以从我的 action.yml 文件中引用另一个 GitHub Action 吗?

注意,我在这里谈论的是 action,而不是 workflow。我知道可以使用 workflows 来完成此操作,但是 action 可以引用其他 action 吗?


1
action.yml 中可以做的事情似乎是有意限制的,所以我想这可能不可能实现,但你能否更详细地解释一下你想要做什么以及你的使用情况。可能有其他方法可以达到相同的效果。 - peterevans
看起来支持正在途中:您可以在 https://github.com/actions/runner/issues/646 上跟踪进展。 - Aleksi
从#646开始,子问题用于引用其他操作的操作:https://github.com/actions/runner/pull/612 - emi
3个回答

20
答案似乎是:您可以(现在,2021年8月)

GitHub Actions: 使用操作组合减少重复

Previously, actions written in YAML could only use scripts.

Now, they can also reference other actions.
This makes it easy to reduce duplication in your workflows.

For example, the following action uses 3 actions to setup buildx, log in to Docker, and publish an image.
By combining these into a single action it provides a larger unit of reuse that you can put into the job of any workflow.

name: "Publish to Docker"
description: "Pushes built artifacts to Docker"

inputs:
 registry_username:
   description: “Username for image registry”
   required: true
 registry_password:
   description: “Password for image registry”
   required: true

runs:
 using: "composite"
 steps:
     - uses: docker/setup-buildx-action@v1

     - uses: docker/login-action@v1
       with:
         username: ${{inputs.registry_username}}
         password: ${{inputs.registry_password}}

     - uses: docker/build-push-action@v2
       with:
         context: .
         push: true
         tags: user/app:latest

Developers can then reference this action in all of their repositories as a single action:

on: [push]

jobs:
 publish:
   runs-on: ubuntu-latest
   steps:
     - uses: actions/checkout@v2
     - uses: my-org/publish-docker@v1
       with:
         registry_username: ${{secrets.REGISTRY_USERNAME}}
         registry_password: ${{secrets.REGISTRY_PASSWORD}}

Learn more about action composition.

因此,如"GitHub Actions元数据语法中的组合操作运行"所述:

runs.steps[*].uses

[Optional] Selects an action to run as part of a step in your job.

An action is a reusable unit of code.
You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image.

We strongly recommend that you include the version of the action you are using by specifying a Git ref, SHA, or Docker tag number.
If you don't specify a version, it could break your workflows or cause unexpected behavior when the action owner publishes an update.

runs:
 using: "composite"
 steps:
   # Reference a specific commit
   - uses: actions/checkout@a81bbbf8298c0fa03ea29cdc473d45769f953675
   # Reference the major version of a release
   - uses: actions/checkout@v2
   # Reference a specific version
   - uses: actions/checkout@v2.2.0
   # Reference a branch
   - uses: actions/checkout@main
   # References a subdirectory in a public GitHub repository at a specific branch, ref, or SHA
   - uses: actions/aws/ec2@main
   # References a local action
   - uses: ./.github/actions/my-action
   # References a docker public registry action
   - uses: docker://gcr.io/cloud-builders/gradle
   # Reference a docker image published on docker hub
   - uses: docker://alpine:3.8

4
答案似乎是:你无法做到。
然而,我最终从NPM内部下载了不同的操作,然后在自己的操作中重新使用它们。
一般来说可能不是一个好主意,但我正在创建的特定操作旨在在我的项目上运行,而不需要太多配置,因此这使得它更加可行。

你能给个例子吗?我在传递参数方面遇到了麻烦,而且我不确定如何调用操作的不同部分,例如:https://github.com/actions/cache/blob/23e301d35cb55b348f1f4524e353b1fc29567e5f/action.yml#L19-L20 - thisismydesign

3

如果这两个操作都是使用nodejs编写的,你将它们在GitHub上提供,并且不介意它们读取同一组输入,那么这种方法非常有效:

npm install --save MyGitHubOrg/MyRepo#master
git add -f node_modules/ package.json package-lock.json

async function run() {
    try {
        require('my-action');
    } catch (err) {
        core.setFailed(`Failed to run habitat-action: ${err.message}`);
        return;
    }

    // ...
}

1
这样做会将您锁定在特定版本中,因此最好在操作的一部分中使用可配置版本运行 npm install - thisismydesign
不建议将节点模块添加到Git。 - Mahmoud Mabrok

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