Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 261
Mathias Lykkegaard Lorenzen Avatar asked Oct 29 '19 16:10

Mathias Lykkegaard Lorenzen


People also ask

Can a GitHub Action call another action?

You can't package up a composite action that can call other actions, at least not yet. Pretty new: https://github.blog/changelog/2021-08-25-github-actions-redu...

How do I trigger a workflow from another workflow in GitHub Actions?

If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret.

Where are GitHub Actions located?

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named . github/workflows . You can create an example workflow in your repository that automatically triggers a series of commands whenever code is pushed.


3 Answers

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
like image 126
VonC Avatar answered Oct 21 '22 13:10

VonC


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.

like image 4
Mathias Lykkegaard Lorenzen Avatar answered Oct 21 '22 12:10

Mathias Lykkegaard Lorenzen


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

    // ...
}
like image 3
The Mighty Chris Avatar answered Oct 21 '22 13:10

The Mighty Chris