tl;dr: Define a GitHub Action using the workflow syntax, allowing reuse of other actions across repositories without creating own JS based wrapper actions.
Presently, there are two ways to define GitHub actions - either using JavaScript or Docker. If we want to reuse some action, we need to clone/install it and then make our own wrapper around it. It becomes more difficult when the action doesn’t provide a programmatic API. The workflow syntax already allows (re)use of various actions, but then one needs to copy same workflow across multiple projects. It would be great if we can define new GitHub actions by using the workflow syntax.
In JavaScript, we may write an action as:
// Skipping some syntax for brevity.
// ASSUMING all actions provide a programmatic API.
// All action libraries are in node_modules, checked-in to the git repository (or packaged using some other tool).
import core from "@actions/core";
import checkout from "@actions/checkout";
import exec from "some-exec-lib";
import deployToGhPages from "some-deploy-lib";
import postComment from "some-comment-lib";
async function main() {
await checkout();
await exec(`npm run build -- --profile ${core.getInput("some-input")}`);
const outputs = await deployToGhPages({ token: core.getInput("github_token") });
await postComment({ files: outputs.changed_files });
// above gets out of hands quick.
}
main().catch((error) => {
console.error(error);
process.exit(1);
}
vs an action.yml
in my-user/declarative-action
may look like:
name: 'Build, deploy and add comment'
inputs:
SOME_INPUT:
description: 'input description'
required: true
steps:
- uses: actions/checkout@v2
- run: npm run build
with:
profile: ${{ inputs.SOME_INPUT }}
- id: deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: comment
uses: ...
with:
files: ${{ outputs.deploy.changed_files }}
Now copying (and updating) above in a workflow across repositories will be cumbersome. So, users can use above action in a workflow as:
jobs:
some_job:
runs-on: ubuntu-latest
steps:
- uses: user/my-declarative-action@v1
with:
SOME_INPUT: my_input_value