From 070a5a61e4b73abab3f68022133d29f015560c7d Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Sat, 18 Nov 2023 18:04:40 +0100 Subject: [PATCH] Add support for passing variables to the GitLab pipeline --- .github/workflows/test.yml | 2 ++ lib/action/context.rb | 2 +- lib/action/step/prepare_context.rb | 2 ++ lib/action/step/trigger_pipeline.rb | 3 ++- test/.gitlab-ci.yml | 1 + 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6f57dc9..535261f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,6 +27,8 @@ jobs: GL_PROJECT_ID: '1000' GL_RUNNER_TOKEN: some_long_runner_token GL_API_TOKEN: TEST1234567890123456 + env: + GLPA_SOME_VARIABLE: some value for the variable - run: 'curl --silent --header "Private-Token: TEST1234567890123456" "http://127.17.0.1:8080/api/v4/projects/1000/jobs/1/trace"' if: always() - run: docker compose down diff --git a/lib/action/context.rb b/lib/action/context.rb index 9e34d64..1014997 100644 --- a/lib/action/context.rb +++ b/lib/action/context.rb @@ -4,7 +4,7 @@ module GitlabPipelineAction class Context attr_accessor :gh_project, :gh_sha, :gh_ref, :gh_server_url, :gl_server_url, :gl_project_id, :gl_project_path, :gl_runner_token, - :gl_api_token, :gl_pipeline, :gl_branch_name, + :gl_api_token, :gl_pipeline, :gl_branch_name, :gl_pipeline_variables, :git_repository, :git_path, :gitlab_client, :docker_runner_container diff --git a/lib/action/step/prepare_context.rb b/lib/action/step/prepare_context.rb index ee8307a..95e73d2 100644 --- a/lib/action/step/prepare_context.rb +++ b/lib/action/step/prepare_context.rb @@ -15,6 +15,8 @@ def execute context.gl_project_id = ENV.fetch('INPUT_GL_PROJECT_ID', nil) context.gl_runner_token = ENV.fetch('INPUT_GL_RUNNER_TOKEN', nil) context.gl_api_token = ENV.fetch('INPUT_GL_API_TOKEN', nil) + context.gl_pipeline_variables = ENV.select { |key| key.start_with?('GLPA_') } + .transform_keys { |key| key.delete_prefix('GLPA_') } context.git_path = "/tmp/repo/#{SecureRandom.hex}" diff --git a/lib/action/step/trigger_pipeline.rb b/lib/action/step/trigger_pipeline.rb index db9d52e..2d91a66 100644 --- a/lib/action/step/trigger_pipeline.rb +++ b/lib/action/step/trigger_pipeline.rb @@ -6,7 +6,8 @@ class TriggerPipeline < Base def execute context.gl_pipeline = context.gitlab_client.create_pipeline( context.gl_project_id, - context.gl_branch_name + context.gl_branch_name, + context.gl_pipeline_variables ) puts "Triggered: #{context.gl_pipeline.web_url}" diff --git a/test/.gitlab-ci.yml b/test/.gitlab-ci.yml index e6f6b04..4e7a5dd 100644 --- a/test/.gitlab-ci.yml +++ b/test/.gitlab-ci.yml @@ -1,3 +1,4 @@ job: script: - echo $CI_COMMIT_SHA + - echo $SOME_VARIABLE