Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: now uses actions/setup-node #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ outputs:
The version of golang that has been installed and is ready for use.
node:
description:
The verison of Node.js that has been installed and is ready for use.
The version of Node.js that has been installed and is ready for use.
java:
description:
The version of Java that has been installed and is ready for use.
Expand All @@ -20,5 +20,42 @@ outputs:
description:
The version of Terraform that has been installed and is ready for use.
runs:
using: "node20"
main: "dist/main/index.js"
using: "composite"
steps:
- name: Read node version
id: get-node-version
run: |
# Check if an .nvmrc file exists and export the node version if it does
if [ -f .nvmrc ]; then
NODE_VERSION=$(cat .nvmrc)
echo "Node version from .nvmrc: $NODE_VERSION"
echo "node_version=$NODE_VERSION" >> $GITHUB_OUTPUT
fi
# Check if a .node-version file exists and export the node version if it does
if [ -f .node-version ]; then
NODE_VERSION=$(cat .node-version)
echo "Node version from .node-version: $NODE_VERSION"
echo "node_version=$NODE_VERSION" >> $GITHUB_OUTPUT
fi
shell: bash
- uses: actions/setup-node@v4
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this action we get cached versions out of the box and a more reliable infrastructure that doesn't require us to rebuild runners to cache new node versions: https://github.com/actions/setup-node

id: setup-node
if: steps.get-node-version.outputs.node_version
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this POC, we only install node if it is required, otherwise we fall back to the system node. We could as easily install node 20 as the default and run the node install step every time. We can make a decision here.

with:
node-version: ${{ steps.get-node-version.outputs.node_version }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can optionally provide a GH token if we hit rate limits (https://github.com/actions/setup-node?tab=readme-ov-file#using-setup-node-on-ghes)

We just need to apply this diff:

diff --git a/action.yaml b/action.yaml
index 6dcc903..8a78d82 100644
--- a/action.yaml
+++ b/action.yaml
@@ -3,6 +3,11 @@ description:
   GitHub Action that installs and provisions supported tools for workflow steps
   in self-hosted runners. This relies on the agent having supported tooling
   installed.
+inputs:
+  github-token:
+    required: true
+    description: GitHub token to overcome unauthenticated rate limits when installing node, for instance 'secrets.GITHUB_TOKEN'
+    default: ${{ github.token }}
 outputs:
   go:
     description:
@@ -44,6 +49,8 @@ runs:
       if: steps.get-node-version.outputs.node_version
       with:
         node-version: ${{ steps.get-node-version.outputs.node_version }}
+        # https://github.com/actions/setup-node?tab=readme-ov-file#using-setup-node-on-ghes
+        token: ${{ inputs.github-token }}
     - name: Export installed node version
       if: steps.get-node-version.outputs.node_version
       run: |

- name: Install yarn
if: steps.setup-node.outputs.node-version
run: |
npm install -g yarn
shell: bash
- name: Export installed node version
if: steps.get-node-version.outputs.node_version
run: |
echo "Installed Node version: ${{ steps.setup-node.outputs.node-version }}"
echo "node=${{ steps.setup-node.outputs.node-version }}" >> $GITHUB_OUTPUT
shell: bash
- name: Install Tools
id: install-tools
run: "node ${{ github.action_path }}/dist/main/index.js"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work if no node version was installed on the previous step?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work on the github runners, but on self hosted runners this would require having node installed there.

In both cases this would run using the default node verison in the runners, which could lead to failures (especially on our internal runners)

shell: bash
Loading
Loading