How can I reference other actions from my GitHub Action's action.yml file?

Is it possible to reference another GitHub Action from my action.yml file?

Note, I'm talking about an action here, not a workflow. I know this can be done with workflows, but can actions reference other actions?

Upvotes: 22

Views: 11707

Answers (3)

VonC
VonC

Reputation: 1328292

The answer seems to be: You can (now, Aug. 2021)

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.

So, as described in "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/[email protected]
   # 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

Upvotes: 24

The Mighty Chris
The Mighty Chris

Reputation: 1746

If both actions are nodejs actions you are serving over GitHub and you don't mind them reading the one set of input, this works pretty well:

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;
    }

    // ...
}

Upvotes: 3

The answer seems to be: You can't.

However, I ended up internally downloading the different actions from NPM, and then re-using them within my own action.

Probably not a good idea in general, but this particular action I am making is designed to run on my own projects without requiring too much configuration, so that makes it more okay.

Upvotes: 4

Related Questions