From 1653dfbd1dbba2191eab3d9e4c8aaa336280730d Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:57:42 +0530 Subject: [PATCH 1/4] Gitlab runner registration --- home/.chezmoi.yaml.tmpl | 5 ++ ...un_onchange_after_46-gitlab-runner.sh.tmpl | 72 +++++++++++++++++++ home/dot_config/shell/private_private.sh.tmpl | 1 + 3 files changed, 78 insertions(+) create mode 100644 home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl diff --git a/home/.chezmoi.yaml.tmpl b/home/.chezmoi.yaml.tmpl index 4fde999d..4da45932 100644 --- a/home/.chezmoi.yaml.tmpl +++ b/home/.chezmoi.yaml.tmpl @@ -140,6 +140,11 @@ data: docker: doRegion: nyc1 domain: "{{ $domain }}" + gitlab: + glurl: "https://gitlab.com/" + runnerDescription: "Docker executor" + runnerImage: "alpine:latest" + runnerTags: "" headless: {{ $headless }} home: "{{ .chezmoi.homeDir }}" homeParentFolder: "{{ if eq .chezmoi.os "linux" }}/home{{ else if eq .chezmoi.os "darwin" }}/Users{{ else }}C:\Users{{ end }}" diff --git a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl new file mode 100644 index 00000000..2afac503 --- /dev/null +++ b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl @@ -0,0 +1,72 @@ +{{- if eq .host.distro.family "linux" -}} +#!/usr/bin/env bash +# @file GitLab Runner Configuration +# @brief Registers GitLab Runner with the given GitLab instance +# @description +# This script registers the machine where the script is run as a runner with the given GitLab instance or with the SaaS GitLab +# if no instance information is provided. +# The script configures the runner to use Docker Executor. Refer [this page](https://docs.gitlab.com/runner/executors/docker.html) for more details. +# +# ## Secrets +# +# The following chart details the secret(s) that are needed to configure the runner: +# +# | Secret | Description | +# |------------------------|------------------------------------------------------------| +# | `GITLAB_RUNNER_TOKEN` | The token generated when the runner was created in GitLab | +# +# For more information about storing secrets like SSH keys and API keys, refer to our Secrets documentation provided below +# +# ## Configuration Variables +# +# The following chart details the input variable(s) that are used to determine the configuration of the runner: +# +# | Variable | Description | +# |---------------------|-------------------------------------------------------------| +# | `glurl` | The URL of the Gitlab instance to associate the Runner with | +# | `runnerImage` | Provide an image to use to configure the runner | +# | `runnerDescription` | Description of this runner | +# | `runnerTags` | Comma separated list of tags for this runner | +# +# ## Links +# +# * [Secrets / Environment variables documentation](https://install.doctor/docs/customization/secrets) + +{{ includeTemplate "universal/profile" }} +{{ includeTemplate "universal/logg" }} + +### Run logic if gitlab-runner is installed +if command -v gitlab-runner > /dev/null; then + ### Check if Runner Token value is present + if [ -v $GITLAB_RUNNER_TOKEN ]; then + if [ -n {{ .host.gitlab.runnerTags }} ]; then + ### Registering runner + logg info 'Registering GitLab Runner with the provided tags' + gitlab-runner register \ + --non-interactive \ + --url {{ .host.gitlab.glurl }} \ + --token $GITLAB_RUNNER_TOKEN \ + --executor "docker" \ + --docker-image {{ .host.gitlab.runnerImage }} \ + --description {{ .host.gitlab.runnerDescription }} \ + --tag-list {{ .host.gitlab.runnerTags }} + else + ### Registering runner + logg info 'Registering GitLab Runner to run untagged builds' + gitlab-runner register \ + --non-interactive \ + --url {{ .host.gitlab.glurl }} \ + --token $GITLAB_RUNNER_TOKEN \ + --executor "docker" \ + --docker-image {{ .host.gitlab.runnerImage }} \ + --description {{ .host.gitlab.runnerDescription }} \ + --run-untagged + fi + else + logg warn 'GITLAB_RUNNER_TOKEN is not set. Not registering the runner' + fi +else + logg warn 'gitlab-runner is not installed or is not available in PATH' +fi + +{{ end -}} diff --git a/home/dot_config/shell/private_private.sh.tmpl b/home/dot_config/shell/private_private.sh.tmpl index 18198d48..e0de5e72 100644 --- a/home/dot_config/shell/private_private.sh.tmpl +++ b/home/dot_config/shell/private_private.sh.tmpl @@ -31,6 +31,7 @@ export GITHUB_TOKEN="$GH_TOKEN" ### GitLab export GL_TOKEN="{{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "GITLAB_TOKEN")) }}{{ includeTemplate "secrets/GITLAB_TOKEN" | decrypt }}{{ else }}{{ env "GITLAB_TOKEN" }}{{ end }}" export GITLAB_TOKEN="$GL_TOKEN" +export GITLAB_RUNNER_TOKEN="{{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "GITLAB_RUNNER_TOKEN")) }}{{ includeTemplate "secrets/GITLAB_RUNNER_TOKEN" | decrypt }}{{ else }}{{ env "GITLAB_RUNNER_TOKEN" }}{{ end }}" ### Heroku export HEROKU_API_KEY="{{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "HEROKU_API_KEY")) }}{{ includeTemplate "secrets/HEROKU_API_KEY" | decrypt }}{{ else }}{{ env "HEROKU_API_KEY" }}{{ end }}" From f05b908f35d171a4f0095271da3648bac4989a1f Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Fri, 16 Jun 2023 15:50:24 +0530 Subject: [PATCH 2/4] Address comments --- home/.chezmoi.yaml.tmpl | 2 +- ...un_onchange_after_46-gitlab-runner.sh.tmpl | 34 +++++++------------ software.yml | 9 ++--- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/home/.chezmoi.yaml.tmpl b/home/.chezmoi.yaml.tmpl index 4da45932..a22743ab 100644 --- a/home/.chezmoi.yaml.tmpl +++ b/home/.chezmoi.yaml.tmpl @@ -140,7 +140,7 @@ data: docker: doRegion: nyc1 domain: "{{ $domain }}" - gitlab: + gitlabRunners: glurl: "https://gitlab.com/" runnerDescription: "Docker executor" runnerImage: "alpine:latest" diff --git a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl index 2afac503..b85bfe57 100644 --- a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl @@ -3,9 +3,11 @@ # @file GitLab Runner Configuration # @brief Registers GitLab Runner with the given GitLab instance # @description -# This script registers the machine where the script is run as a runner with the given GitLab instance or with the SaaS GitLab -# if no instance information is provided. -# The script configures the runner to use Docker Executor. Refer [this page](https://docs.gitlab.com/runner/executors/docker.html) for more details. +# This script registers the runners with the given GitLab instance. SaaS GitLab can also be provided as the GitLab instance to register +# the runners with. The script configures the runners to use Docker Executor. Refer to +# [this page](https://docs.gitlab.com/runner/executors/docker.html) for details about the available configuration settings. +# +# Configuring other executors is not supported by this script. # # ## Secrets # @@ -24,7 +26,7 @@ # | Variable | Description | # |---------------------|-------------------------------------------------------------| # | `glurl` | The URL of the Gitlab instance to associate the Runner with | -# | `runnerImage` | Provide an image to use to configure the runner | +# | `runnerImage` | Docker image to use to configure the runner | # | `runnerDescription` | Description of this runner | # | `runnerTags` | Comma separated list of tags for this runner | # @@ -39,29 +41,19 @@ if command -v gitlab-runner > /dev/null; then ### Check if Runner Token value is present if [ -v $GITLAB_RUNNER_TOKEN ]; then - if [ -n {{ .host.gitlab.runnerTags }} ]; then ### Registering runner logg info 'Registering GitLab Runner with the provided tags' + {{range .host.gitlabRunners -}} gitlab-runner register \ --non-interactive \ - --url {{ .host.gitlab.glurl }} \ + --url {{ .glurl }} \ --token $GITLAB_RUNNER_TOKEN \ --executor "docker" \ - --docker-image {{ .host.gitlab.runnerImage }} \ - --description {{ .host.gitlab.runnerDescription }} \ - --tag-list {{ .host.gitlab.runnerTags }} - else - ### Registering runner - logg info 'Registering GitLab Runner to run untagged builds' - gitlab-runner register \ - --non-interactive \ - --url {{ .host.gitlab.glurl }} \ - --token $GITLAB_RUNNER_TOKEN \ - --executor "docker" \ - --docker-image {{ .host.gitlab.runnerImage }} \ - --description {{ .host.gitlab.runnerDescription }} \ - --run-untagged - fi + --docker-image {{ .runnerImage }} \ + --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ + {{if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" + {{ else }}--run-untagged{{ end }} + {{ end }} else logg warn 'GITLAB_RUNNER_TOKEN is not set. Not registering the runner' fi diff --git a/software.yml b/software.yml index 35de6f4f..01964023 100644 --- a/software.yml +++ b/software.yml @@ -3323,6 +3323,7 @@ softwarePackages: pacman: gitlab-runner port: gitlab-runner scoop: gitlab-runner + _service: gitlab-runner _type: cli gitleaks: _bin: gitleaks @@ -8941,10 +8942,10 @@ softwarePackages: yay: winrm-cli-git browserosaurus: _bin: null - _desc: - _docs: - _github: - _home: + _desc: + _docs: + _github: + _home: _name: Browserosaurus _when:cask: '! test -d /Applications/Browserosaurus.app' cask: browserosaurus From 8302304078f3824fa420289e0515b77a79f3c51e Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Mon, 19 Jun 2023 17:19:00 +0530 Subject: [PATCH 3/4] Add VBox executor --- home/.chezmoi.yaml.tmpl | 12 +- ...un_onchange_after_46-gitlab-runner.sh.tmpl | 104 ++++++++++++------ 2 files changed, 81 insertions(+), 35 deletions(-) diff --git a/home/.chezmoi.yaml.tmpl b/home/.chezmoi.yaml.tmpl index 46185f34..20d7d88b 100644 --- a/home/.chezmoi.yaml.tmpl +++ b/home/.chezmoi.yaml.tmpl @@ -141,10 +141,14 @@ data: doRegion: nyc1 domain: "{{ $domain }}" gitlabRunners: - glurl: "https://gitlab.com/" - runnerDescription: "Docker executor" - runnerImage: "alpine:latest" - runnerTags: "" + - glurl: "https://gitlab.com/" + runnerDescription: "Docker executor" + runnerImage: "alpine:latest" + runnerTags: "" + - glurl: "https://gitlab.com/" + baseVM: "debian" + runnerDescription: "VirtualBox executor - Debian, OpenJDK 20" + runnerTags: "bash,openjdk20,linux" headless: {{ $headless }} home: "{{ .chezmoi.homeDir }}" homeParentFolder: "{{ if eq .chezmoi.os "linux" }}/home{{ else if eq .chezmoi.os "darwin" }}/Users{{ else }}C:\Users{{ end }}" diff --git a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl index b85bfe57..d177b673 100644 --- a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl @@ -1,11 +1,12 @@ {{- if eq .host.distro.family "linux" -}} #!/usr/bin/env bash # @file GitLab Runner Configuration -# @brief Registers GitLab Runner with the given GitLab instance +# @brief Registers GitLab Runner(s) with the given GitLab instance # @description -# This script registers the runners with the given GitLab instance. SaaS GitLab can also be provided as the GitLab instance to register -# the runners with. The script configures the runners to use Docker Executor. Refer to -# [this page](https://docs.gitlab.com/runner/executors/docker.html) for details about the available configuration settings. +# This script registers the runner(s) with the given GitLab instance. SaaS GitLab can also be provided as the GitLab instance to register +# the runners with. The script configures the runners to use either Docker or VirtualBox Executor. Refer to +# [this page](https://docs.gitlab.com/runner/executors/docker.html) and [this page](https://docs.gitlab.com/runner/executors/virtualbox.html) +# for details about the available configuration settings. # # Configuring other executors is not supported by this script. # @@ -23,12 +24,13 @@ # # The following chart details the input variable(s) that are used to determine the configuration of the runner: # -# | Variable | Description | -# |---------------------|-------------------------------------------------------------| -# | `glurl` | The URL of the Gitlab instance to associate the Runner with | -# | `runnerImage` | Docker image to use to configure the runner | -# | `runnerDescription` | Description of this runner | -# | `runnerTags` | Comma separated list of tags for this runner | +# | Variable | Description | +# |---------------------|----------------------------------------------------------------------------------------------------------| +# | `glurl` | The URL of the Gitlab instance to associate the Runner with | +# | `runnerImage` | Docker image to use to configure the runner. Needed only when configuring `Docker` executor | +# | `runnerDescription` | Description of this runner | +# | `runnerTags` | Comma separated list of tags for this runner | +# | `baseVM` | Name of the VirtualBox VM to use for creating runner. Needed only when configuring `VirtualBox` executor | # # ## Links # @@ -37,28 +39,68 @@ {{ includeTemplate "universal/profile" }} {{ includeTemplate "universal/logg" }} -### Run logic if gitlab-runner is installed -if command -v gitlab-runner > /dev/null; then - ### Check if Runner Token value is present - if [ -v $GITLAB_RUNNER_TOKEN ]; then - ### Registering runner - logg info 'Registering GitLab Runner with the provided tags' - {{range .host.gitlabRunners -}} - gitlab-runner register \ - --non-interactive \ - --url {{ .glurl }} \ - --token $GITLAB_RUNNER_TOKEN \ - --executor "docker" \ - --docker-image {{ .runnerImage }} \ - --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ - {{if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" - {{ else }}--run-untagged{{ end }} - {{ end }} - else - logg warn 'GITLAB_RUNNER_TOKEN is not set. Not registering the runner' - fi +### Check if Docker is installed and operational so Docker executor(s) can be registered +if command -v docker > /dev/null && docker run --rm hello-world > /dev/null; then + HAS_DOCKER=1 else - logg warn 'gitlab-runner is not installed or is not available in PATH' + logg warn 'Docker is not installed or it is not operational' +fi + +### Check if VirtualBox is installed and operational so VirtualBox executor(s) can be registered +if command -v VirtualBox > /dev/null; then + HAS_VIRTUALBOX=1 +else + logg warn 'VirtualBox is not installed' +fi + +### Configure runners if Docker or VirtualBox is installed +if [ $HAS_DOCKER -eq 0 ] && [ $HAS_VIRTUALBOX -eq 0 ]; then + logg warn 'Docker and VirtualBox are not installed. Not registering runner(s).' +else + ### Run logic if gitlab-runner is installed + if command -v gitlab-runner > /dev/null; then + ### Check if Runner Token value is present before attempting to register runner(s) + if [ -v $GITLAB_RUNNER_TOKEN ]; then + ### Registering runners + {{ $cmd := `gitlab-runner register \ + --non-interactive \ + --token $GITLAB_RUNNER_TOKEN \` }} + ### Register Docker based runners if Docker is installed and operational + if [ $HAS_DOCKER -eq 1 ]; then + logg info 'Registering GitLab Runner(s) that use Docker executor' + {{- range .host.gitlabRunners }} + {{- if .runnerImage }} + {{- $cmd }} + --url {{ .glurl }} \ + --executor "docker" \ + --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ + --docker-image {{ .runnerImage }} \ + {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" + {{ else }}--run-untagged{{ end }} || echo 'Runner registration failed" + {{ end -}} + {{ end }} + fi + ### Register VirtualBox based runners if VirtualBox is installed + if [ $HAS_VIRTUALBOX -eq 1 ]; then + logg info 'Registering GitLab Runner(s) that use VirtualBox executor' + {{- range .host.gitlabRunners }} + {{- if .baseVM }} + {{- $cmd }} + --url {{ .glurl }} \ + --executor "virtualbox" \ + --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ + --virtualbox-base-name "{{ .baseVM }}" \ + {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" + {{ else }}--run-untagged{{ end }} || echo 'Runner registration failed" + {{ end -}} + {{ end }} + fi + else + logg warn 'GITLAB_RUNNER_TOKEN is not set. Not registering runner(s)' + fi + else + logg warn 'gitlab-runner is not installed or is not available in PATH' + fi fi {{ end -}} From 1c5fb0a3f1627e9d333cc78b4d2c6b6db7609e03 Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Thu, 22 Jun 2023 15:06:48 +0530 Subject: [PATCH 4/4] Address review comments about tags --- .../run_onchange_after_46-gitlab-runner.sh.tmpl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl index d177b673..1aa98f78 100644 --- a/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl +++ b/home/.chezmoiscripts/universal/run_onchange_after_46-gitlab-runner.sh.tmpl @@ -8,7 +8,11 @@ # [this page](https://docs.gitlab.com/runner/executors/docker.html) and [this page](https://docs.gitlab.com/runner/executors/virtualbox.html) # for details about the available configuration settings. # -# Configuring other executors is not supported by this script. +# Runners are always tagged with these 2 values: `hostname` and `docker`/`virtualbox` depending on the type of executor. If a list of tags is provided, +# the runner is tagged with these values in addition to the above mentioned values. If the list of tags is empty, no additonal tags are added and the +# runner is configured to pickup `untagged` jobs. +# +# Configuring other type of executors is not supported by this script. # # ## Secrets # @@ -29,7 +33,7 @@ # | `glurl` | The URL of the Gitlab instance to associate the Runner with | # | `runnerImage` | Docker image to use to configure the runner. Needed only when configuring `Docker` executor | # | `runnerDescription` | Description of this runner | -# | `runnerTags` | Comma separated list of tags for this runner | +# | `runnerTags` | Comma separated list of tags for this runner. See details in the description for more info | # | `baseVM` | Name of the VirtualBox VM to use for creating runner. Needed only when configuring `VirtualBox` executor | # # ## Links @@ -75,8 +79,8 @@ else --executor "docker" \ --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ --docker-image {{ .runnerImage }} \ - {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" - {{ else }}--run-untagged{{ end }} || echo 'Runner registration failed" + {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }},{{ .chezmoi.hostname }},docker" + {{ else }}--tag-list "{{ .chezmoi.hostname }},docker" --run-untagged{{ end }} || echo 'Runner registration failed" {{ end -}} {{ end }} fi @@ -90,8 +94,8 @@ else --executor "virtualbox" \ --description "{{ .runnerDescription }} - on {{ .chezmoi.hostname }}" \ --virtualbox-base-name "{{ .baseVM }}" \ - {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }}" - {{ else }}--run-untagged{{ end }} || echo 'Runner registration failed" + {{ if and .runnerTags (gt (len .runnerTags) 0) }}--tag-list "{{ .runnerTags }},{{ .chezmoi.hostname }},virtualbox" + {{ else }}--tag-list "{{ .chezmoi.hostname }},virtualbox" --run-untagged{{ end }} || echo 'Runner registration failed" {{ end -}} {{ end }} fi