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

Refactor #162

Merged
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.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
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
43 changes: 5 additions & 38 deletions Dockerfile.ubuntu20
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF && npm uninstall -g @actions/tool-cache
const tc = require('@actions/tool-cache');
const allNodeVersions = tc.findAllVersions('node');
const expected = ['${NODE_VERSION}'];
if (expected[0] == '') {
console.log('Invalid NODE_VERSION: ' + expected[0]);
process.on("exit", function() {
process.exit(1);
});
} else if (allNodeVersions.length != expected.length) {
console.log('Expected versions of node available: ' + expected);
console.log('Actual versions of node available: ' + allNodeVersions);
process.on("exit", function() {
process.exit(1);
});
} else if (allNodeVersions[0] != expected[0]) {
console.log('Expected versions of node available: ' + expected);
console.log('Actual versions of node available: ' + allNodeVersions);
process.on("exit", function() {
process.exit(1);
});
} else {
console.log('Versions of node available: ' + allNodeVersions);
}
EOF
# 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 /

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