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

Make setup-node faster and more reliable #160

Merged
merged 3 commits into from
Oct 30, 2023
Merged
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
21 changes: 21 additions & 0 deletions .github/workflows/reusable--e2e-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,24 @@ jobs:

# Test service container
- run: curl -sf http://localhost:8080

# Test the hosted tool cache
- run: |
export PATH="$PATH:/home/runner/externals/node20/bin"
npm install @actions/tool-cache
- uses: actions/github-script@v6
with:
script: |
const assert = require('assert')
const fs = require('fs')
const tc = require('@actions/tool-cache')
const expectedVersion = fs.readFileSync('hostedtoolcache/.node-version').toString().trim()
core.info(`expectedVersion = ${JSON.stringify(expectedVersion)}`)
const versions = tc.findAllVersions('node')
core.info(`versions = ${JSON.stringify(versions)}`)
assert.strictEqual(versions.length, 1)
assert.strictEqual(versions[0], expectedVersion)
- uses: actions/setup-node@v3
with:
node-version-file: hostedtoolcache/.node-version
- run: node --version
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ ARG RUNNER_VERSION=2.311.0
# extends https://github.com/actions/runner/blob/main/images/Dockerfile
FROM ghcr.io/actions/actions-runner:${RUNNER_VERSION}

ARG TARGETOS
ARG TARGETARCH

RUN sudo apt-get update -y \
&& sudo apt-get install -y --no-install-recommends \
# packages in actions-runner-controller/runner-22.04
Expand Down Expand Up @@ -31,9 +34,16 @@ RUN sudo apt-get update -y \
# keep /var/lib/apt/lists to reduce time of apt-get update in a job

# some setup actions store cache into /opt/hostedtoolcache
ENV RUNNER_TOOL_CACHE /opt/hostedtoolcache
RUN sudo mkdir /opt/hostedtoolcache \
&& sudo chown runner:docker /opt/hostedtoolcache

# Pre-install Node.js for actions/setup-node
COPY hostedtoolcache/ /tmp/hostedtoolcache/
RUN cd /tmp/hostedtoolcache \
&& TARGETARCH="${TARGETARCH}" TARGETOS="${TARGETOS}" bash actions-setup-node.sh \
&& sudo rm -fr /tmp/hostedtoolcache

COPY entrypoint.sh /

VOLUME /var/lib/docker
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile.ubuntu20
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ RUN export RUNNER_ARCH=${TARGETARCH} \
&& rm -rf docker

# some setup actions store cache into /opt/hostedtoolcache
ENV RUNNER_TOOL_CACHE /opt/hostedtoolcache
RUN mkdir /opt/hostedtoolcache \
&& chown runner:docker /opt/hostedtoolcache

# Pre-install Node.js for actions/setup-node
COPY hostedtoolcache/ /tmp/hostedtoolcache/
RUN cd /tmp/hostedtoolcache \
&& TARGETARCH="${TARGETARCH}" TARGETOS="${TARGETOS}" bash actions-setup-node.sh \
&& rm -fr /tmp/hostedtoolcache

COPY entrypoint.sh /

VOLUME /var/lib/docker
Expand Down
1 change: 1 addition & 0 deletions hostedtoolcache/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.18.2
34 changes: 34 additions & 0 deletions hostedtoolcache/actions-setup-node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# This script downloads the specific version of Node.js into RUNNER_TOOL_CACHE directory.
# It is designed to work with https://github.com/actions/setup-node
set -eux -o pipefail

: "${TARGETARCH}"
: "${TARGETOS}"
: "${RUNNER_TOOL_CACHE}"

NODE_ARCH="${TARGETARCH}"
if [ "${TARGETARCH}" = "amd64" ]; then
NODE_ARCH=x64
fi

NODE_VERSION="$(cat .node-version)"
test "${NODE_VERSION}"

function download_nodejs () {
mkdir -p "${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}"
# For example, download https://nodejs.org/dist/v18.18.2/node-v18.18.2-linux-x64.tar.gz and
# extract to /opt/hostedtoolcache/node/18.18.2/x64
curl -sf -L "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-${TARGETOS}-${NODE_ARCH}.tar.gz" |
tar -xzf - --strip-components=1 -C "${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}"
touch "${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}.complete"
chown -R runner:docker "${RUNNER_TOOL_CACHE}/node"
}

function verify_nodejs () {
# e.g., /opt/hostedtoolcache/node/18.18.2/x64/bin/node
"${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}/bin/node" --version
}

download_nodejs
verify_nodejs