From 4f6039a0dcfbe1630b960970272a3c3300e7f9a4 Mon Sep 17 00:00:00 2001 From: Nikolaj Dam Larsen Date: Wed, 22 Nov 2023 22:29:34 +0100 Subject: [PATCH] Refactor the library substantially --- .github/workflows/docker-image.yml | 17 +++------------- Dockerfile | 32 ++++++++++++++++++++++++++++++ node-16-alpine/Dockerfile | 4 ---- node-16/Dockerfile | 4 ---- node-18-alpine/Dockerfile | 4 ---- node-18/Dockerfile | 4 ---- node-20-alpine/Dockerfile | 4 ---- node-20/Dockerfile | 4 ---- node-alpine/Dockerfile | 4 ---- node-latest/Dockerfile | 4 ---- node-lts/Dockerfile | 4 ---- node_version.sh | 25 +++++++++++++++++++++++ 12 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 Dockerfile delete mode 100644 node-16-alpine/Dockerfile delete mode 100644 node-16/Dockerfile delete mode 100644 node-18-alpine/Dockerfile delete mode 100644 node-18/Dockerfile delete mode 100644 node-20-alpine/Dockerfile delete mode 100644 node-20/Dockerfile delete mode 100644 node-alpine/Dockerfile delete mode 100644 node-latest/Dockerfile delete mode 100644 node-lts/Dockerfile create mode 100644 node_version.sh diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 74f326d..3952ad4 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -9,18 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node_version: - [ - "16", - "16-alpine", - "18", - "18-alpine", - "20", - "20-alpine", - "alpine", - "lts", - "latest", - ] + node_version: ["16", "18", "20", "lts", "latest"] steps: - name: Checkout @@ -36,7 +25,7 @@ jobs: uses: docker/build-push-action@v5 with: context: . - file: node-${{ matrix.node_version }}/Dockerfile - no-cache: true + file: Dockerfile push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/gcloud-sdk-nodejs:${{ matrix.node_version }} + build-args: NODE_MAJOR=${{ matrix.node_version }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bf6f8d5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +# Use the Google Cloud SDK slim image as the base +FROM google/cloud-sdk:slim + +# Specify the Node.js version as an argument +ARG NODE_MAJOR + +# Update the package manager +RUN apt-get update + +# Install necessary dependencies +RUN apt-get install -y ca-certificates curl gnupg jq + +# Copy the script to determine the Node.js version and make it executable +COPY node_version.sh . +RUN chmod +x node_version.sh + +# Run the script to determine the Node.js version and store the result in a file +RUN . ./node_version.sh > /tmp/result.txt + +# Display the determined Node.js version +RUN echo "Node version: " $(cat /tmp/result.txt) + +# Configure the source of the specific Node.js version +RUN mkdir -p /etc/apt/keyrings +RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \ + | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg +RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$(cat /tmp/result.txt).x nodistro main" \ + | tee /etc/apt/sources.list.d/nodesource.list + +# Update the package manager and install the specific Node.js version +RUN apt-get update +RUN apt-get install nodejs -y \ No newline at end of file diff --git a/node-16-alpine/Dockerfile b/node-16-alpine/Dockerfile deleted file mode 100644 index 52465d1..0000000 --- a/node-16-alpine/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:16-alpine as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-16/Dockerfile b/node-16/Dockerfile deleted file mode 100644 index 170fe3f..0000000 --- a/node-16/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:16 as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-18-alpine/Dockerfile b/node-18-alpine/Dockerfile deleted file mode 100644 index 176930d..0000000 --- a/node-18-alpine/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:18-alpine as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-18/Dockerfile b/node-18/Dockerfile deleted file mode 100644 index c8a597c..0000000 --- a/node-18/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:18 as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-20-alpine/Dockerfile b/node-20-alpine/Dockerfile deleted file mode 100644 index a169eda..0000000 --- a/node-20-alpine/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:20-alpine as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-20/Dockerfile b/node-20/Dockerfile deleted file mode 100644 index a6dd7c7..0000000 --- a/node-20/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:20 as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-alpine/Dockerfile b/node-alpine/Dockerfile deleted file mode 100644 index 1a63dae..0000000 --- a/node-alpine/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:alpine as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-latest/Dockerfile b/node-latest/Dockerfile deleted file mode 100644 index a84df10..0000000 --- a/node-latest/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:latest as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node-lts/Dockerfile b/node-lts/Dockerfile deleted file mode 100644 index 7f74be6..0000000 --- a/node-lts/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -FROM node:lts as node - -FROM google/cloud-sdk:alpine -COPY --from=node . . diff --git a/node_version.sh b/node_version.sh new file mode 100644 index 0000000..4e0f065 --- /dev/null +++ b/node_version.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Set the default Node.js version to the specified NODE_MAJOR or use LTS/Latest based on the condition +NODE_VERSION=${NODE_MAJOR} + +if [ "${NODE_MAJOR}" = "lts" ]; then + NODE_VERSION=$( + curl -sL https://nodejs.org/dist/index.json \ + | jq -r '.[] | select(.lts) | .version' \ + | head -n 1 \ + | cut -d '.' -f 1 \ + | sed 's/^v//' + ) +elif [ "${NODE_MAJOR}" = "latest" ]; then + NODE_VERSION=$( + curl -sL https://nodejs.org/dist/index.json \ + | jq -r '.[0].version' \ + | head -n 1 \ + | cut -d '.' -f 1 \ + | sed 's/^v//' + ) +fi + +# Print the determined Node.js version +echo $NODE_VERSION \ No newline at end of file