From cfbc9d7950c7651f2cc0a551dfe52d623e19afb0 Mon Sep 17 00:00:00 2001
From: Timo Furrer <tfurrer@gitlab.com>
Date: Tue, 12 Nov 2024 11:12:13 +0100
Subject: [PATCH] Support custom runner tag for image build job

Closes https://gitlab.com/components/opentofu/-/issues/73

Changelog: development
---
 .gitlab-ci.yml             | 43 ++++++++++++++++++++++++++++----------
 .gitlab/README.md.template |  3 +++
 README.md                  |  3 +++
 3 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b66a8ad..4ee2f66 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -116,7 +116,18 @@ check-semantic-version:
   script:
     - echo -n "$CI_COMMIT_TAG" | ./.gitlab/scripts/check-semantic-version.sh
 
-gitlab-opentofu-image:build:
+.gitlab-opentofu-image:build:base:rules: &gitlab-opentofu-image-build-base-rules
+  - if: $CI_COMMIT_TAG
+  - changes:
+      - Dockerfile.*
+      - .dockerignore
+      - opentofu_versions.yaml
+      - .gitlab-ci.yml
+      - src/**/*
+      - templates/**/*
+      - tests/**/*
+
+.gitlab-opentofu-image:build:base:
   extends: '.image-matrix:build'
   stage: build
   image: quay.io/containers/buildah:v1.37.5
@@ -149,16 +160,22 @@ gitlab-opentofu-image:build:
       --manifest "$GITLAB_OPENTOFU_IMAGE_NAME"
       .
     - buildah manifest push --all "$GITLAB_OPENTOFU_IMAGE_NAME"
+
+gitlab-opentofu-image:build:
+  extends: '.gitlab-opentofu-image:build:base'
   rules:
-    - if: $CI_COMMIT_TAG
-    - changes:
-        - Dockerfile.*
-        - .dockerignore
-        - opentofu_versions.yaml
-        - .gitlab-ci.yml
-        - src/**/*
-        - templates/**/*
-        - tests/**/*
+    - if: $OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG
+      when: never
+    - *gitlab-opentofu-image-build-base-rules
+
+gitlab-opentofu-image:build:custom-runner:
+  extends: '.gitlab-opentofu-image:build:base'
+  tags:
+    - $OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG
+  rules:
+    - if: '$OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG == null || $OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG == ""'
+      when: never
+    - *gitlab-opentofu-image-build-base-rules
 
 check-versions:
   stage: test
@@ -237,7 +254,11 @@ gitlab-opentofu-image:deploy:
 
 gitlab-opentofu-image:deploy:dry-run:
   extends: ['.gitlab-opentofu-image:deploy:base']
-  needs: ['gitlab-opentofu-image:build']
+  needs:
+    - job: 'gitlab-opentofu-image:build'
+      optional: true
+    - job: 'gitlab-opentofu-image:build:custom-runner'
+      optional: true
   script:
     - echo "dry run"
   rules:
diff --git a/.gitlab/README.md.template b/.gitlab/README.md.template
index 5665ef3..28a0c49 100644
--- a/.gitlab/README.md.template
+++ b/.gitlab/README.md.template
@@ -457,6 +457,9 @@ See also the official GitLab documentation for it
 If you want to save runner resources you may disable the unit and integration tests
 by setting the `SKIP_TESTS` CI/CD variable to `true`.
 
+You can set the `OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG` CI/CD variable to a custom runner tag
+to use for the image build job. This may be useful if you require a dedicated privileged runner.
+
 The pipeline of this component respects the
 [GitLab Dependency Proxy](https://docs.gitlab.com/ee/user/packages/dependency_proxy/) configuration
 by detecting the `CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX` environment variable
diff --git a/README.md b/README.md
index 4c6cdf7..06023aa 100644
--- a/README.md
+++ b/README.md
@@ -472,6 +472,9 @@ See also the official GitLab documentation for it
 If you want to save runner resources you may disable the unit and integration tests
 by setting the `SKIP_TESTS` CI/CD variable to `true`.
 
+You can set the `OPENTOFU_COMPONENT_IMAGE_BUILD_RUNNER_TAG` CI/CD variable to a custom runner tag
+to use for the image build job. This may be useful if you require a dedicated privileged runner.
+
 The pipeline of this component respects the
 [GitLab Dependency Proxy](https://docs.gitlab.com/ee/user/packages/dependency_proxy/) configuration
 by detecting the `CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX` environment variable
-- 
GitLab