Extracting a composite GitHub Action for pnpm and Node
This lesson preview is part of the Bundling and Automation in Monorepos course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Bundling and Automation in Monorepos, plus 90+ \newline books, guides and courses with the \newline Pro subscription.

[00:02 - 00:20] The change that I want to do in this lesson is I want to extract these three steps into their own common action, because we're going to use them in other jobs throughout our monorepo. And it's a good way to see how shared code is done in GitHub actions.
[00:21 - 00:37] I'm going to create a file called ./.github/actions/pnpm/action.yaml. And I'm going to create the folder for it and then can write it okay.
[00:38 - 00:58] I'm going to name this "Setup pnpm and Node". It has a "runs" block that sets "using: composite", which means that this is an action that's going to run multiple steps.
[00:59 - 01:11] That's just the way that you denote that in GitHub actions. And our steps are going to be these things copied over with one change.
[01:12 - 01:20] The run block needs to also specify a shell for running. In this case we're going to specify "shell: bash".
[01:21 - 02:05] The reason why you need to specify a shell with run steps is because an action can run in any operating system, and you need to explicitly say what the shell should be. While here we don't need to specify a shell because the operating system is inferred by what we're running the job on, we're running on ubuntu, so run will automatically use the bash shell. To use this new action I need to say "uses" and then dot, which means this repository then ./.github/actions/pnpm Notice that here we're saying just the folder of the action.
[02:06 - 02:17] So ./.github/actions/pnpm, while here we're specifying in the folder, we're specifying a file called action.yaml. There are other types of actions you can create for example with Docker.
[02:18 - 02:33] But this is the most common one that you would see in GitHub, and the only one we're going to look at in this course. I'm going to first check out a branch called extract-pnpm-action.
[02:34 - 02:45] I'm going to add my changes and I'm going to git commit "Extract pnpm action". I'm going to push to origin/extract-pnpm-action branch.
[02:46 - 02:55] Then we can open a pull request for this. And if I didn't get anything wrong, this should now complete successfully.
[02:56 - 03:04] Let me open the workflow. And it seems like it will complete successfully.
[03:05 - 03:20] So it is running our action. It is correctly resolving Node, reading from .nvmrc to get Node version 22, and then it's going to run all the other commands in our CI check.
[03:21 - 03:26] So this seems like it's fine just going to wait for it to finish. And then we can merge this pull request.
[03:27 - 03:36] And with that this lesson is done. See you in the next one.