From fec78c12943f3e044509198f7143f0797b784ecb Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Tue, 24 Oct 2023 04:59:33 +0000 Subject: [PATCH 1/2] Make setup-node faster and more reliable We have occasionally seen that setup-node actions failing due to intermittent connection failures between us and nodejs.org. To alleviate the issue, we could cache our "most-widely-used" Node.js version inside the container image, enabling setup-node to skip access to nodejs.org. This tries to implements that, in a way so that it becomes resilient to nodejs and tool-cache changes. Just keep updating NODE_VERSION defined in the dockerfile using dependabot or renovate- the rest of the change will just work regardless of the version you want. --- Dockerfile.ubuntu20 | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Dockerfile.ubuntu20 b/Dockerfile.ubuntu20 index 9d7a6445..79968efb 100644 --- a/Dockerfile.ubuntu20 +++ b/Dockerfile.ubuntu20 @@ -65,9 +65,49 @@ 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 +# We pre-install nodejs to reduce time of setup-node and improve its reliability. +ENV NODE_VERSION 18.18.2 + +RUN if [ "${TARGETARCH}" = "amd64" ]; then export NODE_ARCH=x64 ; else export NODE_ARCH=${TARGETARCH} ; fi; \ + mkdir -p /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH} && \ + curl -s -L https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.gz \ + | tar xvzf - -C /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH} \ + && touch /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH}.complete \ + && chown -R runner:docker /opt/hostedtoolcache/node && \ + ${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}/node-v${NODE_VERSION}-linux-${NODE_ARCH}/bin/node --version + +RUN export PATH=$PATH:/home/runner/externals/node20/bin ; export NODE_PATH=/home/runner/externals/node20/lib/node_modules ; \ + npm install -g @actions/tool-cache && node < Date: Thu, 26 Oct 2023 09:03:46 +0900 Subject: [PATCH 2/2] Refactor (#162) * Refactor * Fix * Fix * Fix * Improve logs * Refactor * Fix script * Set --strip-components=1 --- .github/workflows/reusable--e2e-test.yaml | 21 +++++++++++ Dockerfile | 10 ++++++ Dockerfile.ubuntu20 | 43 +++-------------------- hostedtoolcache/.node-version | 1 + hostedtoolcache/actions-setup-node.sh | 34 ++++++++++++++++++ 5 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 hostedtoolcache/.node-version create mode 100644 hostedtoolcache/actions-setup-node.sh diff --git a/.github/workflows/reusable--e2e-test.yaml b/.github/workflows/reusable--e2e-test.yaml index 45a4e2f6..6d6c7ae8 100644 --- a/.github/workflows/reusable--e2e-test.yaml +++ b/.github/workflows/reusable--e2e-test.yaml @@ -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 diff --git a/Dockerfile b/Dockerfile index 4e920bd7..1221fd6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,9 @@ ARG RUNNER_VERSION=2.310.2 # 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 @@ -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 diff --git a/Dockerfile.ubuntu20 b/Dockerfile.ubuntu20 index 79968efb..125dad23 100644 --- a/Dockerfile.ubuntu20 +++ b/Dockerfile.ubuntu20 @@ -66,47 +66,14 @@ RUN export RUNNER_ARCH=${TARGETARCH} \ # some setup actions store cache into /opt/hostedtoolcache ENV RUNNER_TOOL_CACHE /opt/hostedtoolcache - RUN mkdir /opt/hostedtoolcache \ && chown runner:docker /opt/hostedtoolcache -# We pre-install nodejs to reduce time of setup-node and improve its reliability. -ENV NODE_VERSION 18.18.2 - -RUN if [ "${TARGETARCH}" = "amd64" ]; then export NODE_ARCH=x64 ; else export NODE_ARCH=${TARGETARCH} ; fi; \ - mkdir -p /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH} && \ - curl -s -L https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODE_ARCH}.tar.gz \ - | tar xvzf - -C /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH} \ - && touch /opt/hostedtoolcache/node/${NODE_VERSION}/${NODE_ARCH}.complete \ - && chown -R runner:docker /opt/hostedtoolcache/node && \ - ${RUNNER_TOOL_CACHE}/node/${NODE_VERSION}/${NODE_ARCH}/node-v${NODE_VERSION}-linux-${NODE_ARCH}/bin/node --version - -RUN export PATH=$PATH:/home/runner/externals/node20/bin ; export NODE_PATH=/home/runner/externals/node20/lib/node_modules ; \ - npm install -g @actions/tool-cache && node <