我可以从我的 action.yml
文件中引用另一个 GitHub Action 吗?
注意,我在这里谈论的是 action,而不是 workflow。我知道可以使用 workflows 来完成此操作,但是 action 可以引用其他 action 吗?
我可以从我的 action.yml
文件中引用另一个 GitHub Action 吗?
注意,我在这里谈论的是 action,而不是 workflow。我知道可以使用 workflows 来完成此操作,但是 action 可以引用其他 action 吗?
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
如果这两个操作都是使用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;
}
// ...
}
npm install
。 - thisismydesign
action.yml
中可以做的事情似乎是有意限制的,所以我想这可能不可能实现,但你能否更详细地解释一下你想要做什么以及你的使用情况。可能有其他方法可以达到相同的效果。 - peterevans