Skip to content

Commit 74bd8d6

Browse files
feat: runner refactoring (#6)
Co-authored-by: Michele De Simone <106953981+micdes-pagopa@users.noreply.github.com>
1 parent 1d12975 commit 74bd8d6

File tree

6 files changed

+136
-99
lines changed

6 files changed

+136
-99
lines changed

.github/workflows/release.yml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,34 @@ jobs:
4646
username: ${{ github.actor }}
4747
password: ${{ secrets.GITHUB_TOKEN }}
4848

49-
- name: Build and push Docker image
50-
id: docker_build_push
49+
- name: Build and push Docker image (minimal)
50+
id: docker_build_push_minimal
5151
if: steps.release.outputs.new_release_published == 'true'
5252
# from https://github.com/docker/build-push-action/commits/master
5353
uses: docker/build-push-action@c56af957549030174b10d6867f20e78cfd7debc5
5454
with:
5555
context: .
56+
file: Dockerfile.minimal
5657
push: true
5758
tags: |
5859
ghcr.io/${{ github.repository }}:latest
59-
ghcr.io/${{ github.repository }}:v${{ steps.release.outputs.new_release_version }}
60+
ghcr.io/${{ github.repository }}:v${{ steps.release.outputs.new_release_version }}-minimal
61+
labels: |
62+
maintainer=https://pagopa.it
63+
org.opencontainers.image.source=https://github.com/${{ github.repository }}
64+
65+
- name: Build and push Docker image (full)
66+
id: docker_build_push_full
67+
if: steps.release.outputs.new_release_published == 'true'
68+
# from https://github.com/docker/build-push-action/commits/master
69+
uses: docker/build-push-action@c56af957549030174b10d6867f20e78cfd7debc5
70+
with:
71+
context: .
72+
file: Dockerfile.full
73+
push: true
74+
tags: |
75+
ghcr.io/${{ github.repository }}:latest-full
76+
ghcr.io/${{ github.repository }}:v${{ steps.release.outputs.new_release_version }}-full
6077
labels: |
6178
maintainer=https://pagopa.it
6279
org.opencontainers.image.source=https://github.com/${{ github.repository }}

Dockerfile renamed to Dockerfile.full

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
FROM ubuntu:22.04@sha256:a8fe6fd30333dc60fc5306982a7c51385c2091af1e0ee887166b40a905691fd0
1+
# runner v2.323.0: https://github.com/actions/runner/pkgs/container/actions-runner
2+
FROM ghcr.io/actions/actions-runner@sha256:831a2607a2618e4b79d9323b4c72330f3861768a061c2b92a845e9d214d80e5b
23

34
ARG KUBECTL_VERSION=1.25.16
45

5-
RUN apt-get update && apt-get install -y curl zip unzip jq ca-certificates curl wget apt-transport-https lsb-release gnupg git gettext-base
6+
USER root
67

7-
# Create a folder
8-
RUN mkdir actions-runner
9-
WORKDIR /actions-runner
8+
# Already installed in base image: curl, jq1.6, git 2.49.0
9+
RUN apt-get update && apt-get install -y zip unzip ca-certificates wget apt-transport-https lsb-release gnupg gettext-base
1010

11-
RUN GITHUB_RUNNER_VERSION="2.323.0" && \
12-
GITHUB_RUNNER_VERSION_SHA="0dbc9bf5a58620fc52cb6cc0448abcca964a8d74b5f39773b7afcad9ab691e19" && \
13-
curl -o actions-runner-linux-x64-${GITHUB_RUNNER_VERSION}.tar.gz -L https://github.com/actions/runner/releases/download/v${GITHUB_RUNNER_VERSION}/actions-runner-linux-x64-${GITHUB_RUNNER_VERSION}.tar.gz && \
14-
echo "${GITHUB_RUNNER_VERSION_SHA} actions-runner-linux-x64-${GITHUB_RUNNER_VERSION}.tar.gz" | sha256sum -c && \
15-
tar xzf ./actions-runner-linux-x64-${GITHUB_RUNNER_VERSION}.tar.gz && \
16-
rm actions-runner-linux-x64-${GITHUB_RUNNER_VERSION}.tar.gz
17-
18-
RUN bash bin/installdependencies.sh
1911

2012
# install AWS cli from https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
2113
WORKDIR /tmp
@@ -59,26 +51,27 @@ RUN gpg --verify awscliv2.sig awscliv2.zip
5951

6052
RUN unzip -q awscliv2.zip && ./aws/install
6153
RUN rm -rf "aws*"
54+
RUN aws --version
6255

6356
# install kubectl from https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/#install-kubectl-on-linux
64-
6557
RUN curl -LO https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl
6658
RUN curl -LO https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl.sha256
6759
RUN echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
6860
RUN mv kubectl /usr/local/bin/ && chmod +x /usr/local/bin/kubectl
61+
RUN kubectl version --output=yaml --client
6962

7063
# install helm from https://helm.sh/docs/intro/install/#from-apt-debianubuntu
71-
7264
RUN curl https://baltocdn.com/helm/signing.asc | apt-key add - && \
7365
echo "deb https://baltocdn.com/helm/stable/debian/ all main" | tee /etc/apt/sources.list.d/helm-stable-debian.list
7466

75-
RUN apt-get update && apt-get -y install helm
67+
RUN apt-get update && apt list helm && apt-get -y install helm=3.17.2-1
68+
RUN helm version
7669

7770
# install mongosh from https://www.mongodb.com/try/download/shell
78-
7971
RUN curl -O https://downloads.mongodb.com/compass/mongodb-mongosh_1.6.1_amd64.deb
8072
RUN apt-get install -y ./mongodb-mongosh_1.6.1_amd64.deb
8173
RUN rm ./mongodb-mongosh_1.6.1_amd64.deb
74+
RUN mongosh --version
8275

8376
# install NodeJS 18-x
8477
RUN mkdir -p /etc/apt/keyrings
@@ -90,22 +83,16 @@ RUN node -v
9083

9184
# Install yq
9285
RUN curl -L https://github.com/mikefarah/yq/releases/download/v4.35.2/yq_linux_amd64 -o /usr/local/bin/yq && chmod +x /usr/local/bin/yq
93-
# Verify yq installation
9486
RUN yq --version
9587

96-
RUN useradd github && \
97-
mkdir -p /home/github && \
98-
chown -R github:github /home/github && \
99-
chown -R github:github /actions-runner
100-
101-
WORKDIR /home/github
88+
# container home is /home/runner
89+
# "runner" user is created in base image, has permissions on container home
90+
# "docker" group is created in base image
91+
WORKDIR /home/runner
10292

103-
COPY killProcess.sh ./killProcess.sh
104-
RUN chmod +x ./killProcess.sh
93+
USER runner
10594

106-
COPY entrypoint.sh ./entrypoint.sh
95+
COPY --chown=runner: ./scripts/entrypoint.sh ./entrypoint.sh
10796
RUN chmod +x ./entrypoint.sh
10897

109-
USER github
110-
111-
ENTRYPOINT ["/home/github/entrypoint.sh"]
98+
ENTRYPOINT ["/home/runner/entrypoint.sh"]

Dockerfile.minimal

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# runner v2.323.0: https://github.com/actions/runner/pkgs/container/actions-runner
2+
FROM ghcr.io/actions/actions-runner@sha256:831a2607a2618e4b79d9323b4c72330f3861768a061c2b92a845e9d214d80e5b
3+
4+
USER root
5+
6+
# Already installed in base image: curl, jq1.6, git 2.49.0
7+
RUN apt-get update && apt-get install -y zip unzip ca-certificates wget apt-transport-https lsb-release gnupg gettext-base
8+
9+
# container home is /home/runner
10+
# "runner" user is created in base image, has permissions on container home
11+
# "docker" group is created in base image
12+
WORKDIR /home/runner
13+
14+
USER runner
15+
16+
COPY --chown=runner: ./scripts/entrypoint.sh ./entrypoint.sh
17+
RUN chmod +x ./entrypoint.sh
18+
19+
ENTRYPOINT ["/home/runner/entrypoint.sh"]

entrypoint.sh

Lines changed: 0 additions & 66 deletions
This file was deleted.

scripts/entrypoint.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
3+
INTERACTIVE="FALSE"
4+
if [ "$(echo $INTERACTIVE_MODE | tr '[:upper:]' '[:lower:]')" == "true" ]; then
5+
INTERACTIVE="TRUE"
6+
fi
7+
8+
# Verify some Repo URL and token have been given, otherwise we must be interactive mode.
9+
if [ -z "$GITHUB_REPOSITORY_URL" ] || [ -z "$GITHUB_PAT" ] || [ -z "$GITHUB_REPOSITORY_NAME" ] || [ -z "$RUNNER_NAME" ]; then
10+
if [ "$INTERACTIVE" == "FALSE" ]; then
11+
echo "GITHUB_REPOSITORY_URL, GITHUB_PAT, GITHUB_REPOSITORY_NAME and RUNNER_NAME cannot be empty"
12+
exit 1
13+
fi
14+
fi
15+
16+
ADDITIONAL_ARGS=""
17+
18+
if [[ -n "${RUNNER_LABELS:-}" ]]; then
19+
# Must not start or end with a comma
20+
if [[ "$RUNNER_LABELS" == ,* || "$RUNNER_LABELS" == *, ]]; then
21+
echo "Error: RUNNER_LABELS must not start or end with a comma"
22+
exit 1
23+
fi
24+
25+
# Must not contain any whitespace
26+
if [[ "$RUNNER_LABELS" =~ [[:space:]] ]]; then
27+
echo "Error: RUNNER_LABELS must not contain spaces"
28+
exit 1
29+
fi
30+
31+
ADDITIONAL_ARGS="$ADDITIONAL_ARGS --no-default-labels --labels ${RUNNER_LABELS} "
32+
fi
33+
34+
if [[ -n "${REPLACE_EXISTING_RUNNER_NAME:-}" ]]; then
35+
case "$REPLACE_EXISTING_RUNNER_NAME" in
36+
true)
37+
ADDITIONAL_ARGS="$ADDITIONAL_ARGS --replace"
38+
;;
39+
*)
40+
echo "Error: REPLACE_EXISTING_RUNNER_NAME must be 'true' or 'false' if set, got: '$REPLACE_EXISTING_RUNNER_NAME'"
41+
exit 1
42+
;;
43+
esac
44+
fi
45+
46+
if [[ -n "${WORK_DIR:-}" ]]; then
47+
ADDITIONAL_ARGS="$ADDITIONAL_ARGS --work $WORK_DIR"
48+
fi
49+
50+
# Calculate default configuration values.
51+
GITHUB_REPOSITORY_BANNER="$GITHUB_REPOSITORY_URL"
52+
if [ -z "$GITHUB_REPOSITORY_BANNER" ]; then
53+
export GITHUB_REPOSITORY_BANNER="<empty repository url>"
54+
fi
55+
56+
57+
echo "Requesting registration token..."
58+
59+
REGISTRATION_TOKEN=$(curl -s \
60+
--http1.1 \
61+
-X POST \
62+
-H "Accept: application/vnd.github+json" \
63+
-H "Authorization: Bearer ${GITHUB_PAT}" \
64+
https://api.github.com/repos/${GITHUB_REPOSITORY_NAME}/actions/runners/registration-token | jq ".token" -r)
65+
66+
67+
printf "Configuring GitHub Runner for $GITHUB_REPOSITORY_BANNER\n"
68+
printf "\tRunner Name: $RUNNER_NAME\n\tAdditional args: $ADDITIONAL_ARGS\n"
69+
70+
if [ "$INTERACTIVE" == "FALSE" ]; then
71+
printf "Running in non-interactive mode\n"
72+
. $HOME/config.sh --name $RUNNER_NAME --url $GITHUB_REPOSITORY_URL --token $REGISTRATION_TOKEN $ADDITIONAL_ARGS --unattended
73+
else
74+
. $HOME/config.sh --name $RUNNER_NAME --url $GITHUB_REPOSITORY_URL --token $REGISTRATION_TOKEN $ADDITIONAL_ARGS
75+
fi
76+
77+
# Start the runner.
78+
printf "Executing GitHub Runner for $GITHUB_REPOSITORY_NAME\n"
79+
80+
bash $HOME/run.sh
File renamed without changes.

0 commit comments

Comments
 (0)