如何从我的 GitHub 操作的 action.yml 文件中引用其他操作?
How can I reference other actions from my GitHub Action's action.yml file?
是否可以从我的 action.yml
文件中引用另一个 GitHub 操作?
请注意,我在这里谈论的是操作,而不是工作流程。我知道这可以通过工作流来完成,但是操作可以引用其他操作吗?
答案似乎是:你不能。
但是,我最终在内部从 NPM 下载了不同的操作,然后在我自己的操作中重新使用它们。
一般来说这可能不是一个好主意,但我正在执行的这个特定操作是为了 运行 在我自己的项目上设计的,而不需要太多配置,所以它更合适。
如果这两个操作都是您在 GitHub 上服务的 nodejs 操作,并且您不介意它们读取一组输入,则效果很好:
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;
}
// ...
}
答案似乎是:你可以(现在,2021 年 8 月)
GitHub Actions: Reduce duplication with action composition
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.
因此,如“runs for composite 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
是否可以从我的 action.yml
文件中引用另一个 GitHub 操作?
请注意,我在这里谈论的是操作,而不是工作流程。我知道这可以通过工作流来完成,但是操作可以引用其他操作吗?
答案似乎是:你不能。
但是,我最终在内部从 NPM 下载了不同的操作,然后在我自己的操作中重新使用它们。
一般来说这可能不是一个好主意,但我正在执行的这个特定操作是为了 运行 在我自己的项目上设计的,而不需要太多配置,所以它更合适。
如果这两个操作都是您在 GitHub 上服务的 nodejs 操作,并且您不介意它们读取一组输入,则效果很好:
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;
}
// ...
}
答案似乎是:你可以(现在,2021 年 8 月)
GitHub Actions: Reduce duplication with action composition
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.
因此,如“runs for composite 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