Skip to content

Commit fbaafd5

Browse files
committed
Initial version of reusable workflow for trigger-gitlab-pipeline
1 parent 940714d commit fbaafd5

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Trigger GitLab pipeline
2+
on:
3+
workflow_call:
4+
inputs:
5+
triggered-ref:
6+
description: 'GitLab project ref to trigger'
7+
required: true
8+
type: string
9+
schedule:
10+
description: 'Indication if it is a automatically scheduled request'
11+
required: false
12+
default: false
13+
type: boolean
14+
cancel-outdated-pipelines:
15+
description: 'If set to true, it will cancel previous pipelines that are running for the same github ref'
16+
required: false
17+
default: true
18+
type: boolean
19+
secrets:
20+
ci-api-v4-url:
21+
description: 'GitLab API v4 root URL'
22+
required: true
23+
access-token:
24+
description: 'GitLab API access token'
25+
required: true
26+
trigger-token:
27+
description: 'GitLab API trigger token'
28+
required: true
29+
project-id:
30+
description: 'GitLab project ID'
31+
required: true
32+
33+
jobs:
34+
authorize:
35+
environment: ${{ (github.event_name == 'pull_request_target' &&
36+
github.event.pull_request.head.repo.full_name != github.repository) &&
37+
'External' || 'Internal' }}
38+
runs-on: ubuntu-latest
39+
steps:
40+
- run: echo "Authorized the job to run" # This step will only execute if the pipeline has necessary approvals to run
41+
42+
trigger-gitlab-pipeline:
43+
needs: authorize
44+
runs-on: [self-hosted, gitlab]
45+
steps:
46+
# Note: actions/checkout will run in the context of the caller workflow
47+
# meaning, that we cannot use checkout defaults, and must specify
48+
# this repo explicitly, to get its contents
49+
#
50+
# There might be a better way to do that, but I would like to avoid
51+
# making this as inputs or secrets to have less manipulatable inputs
52+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
53+
with:
54+
repository: NordSecurity/trigger-gitlab-pipeline
55+
ref: LLT-5701_implement_reusable_workflow_to_enable_workflow_pinning_on_non_ephemeral_runners # Change to "main" after merge or figure out how to find out which reference was called
56+
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
57+
with:
58+
node-version: 20
59+
- name: Dependencies install
60+
run: npm install
61+
- name: Run triggering script
62+
run: node index.js # It will not be accessible as of now, but it is enough for testing.
63+
env:
64+
TRIGGERED_REF: ${{ inputs.triggered-ref }}
65+
SCHEDULE: ${{ inputs.schedule }}
66+
CANCEL_OUTDATED_PIPELINES: ${{ inputs.cancel-outdated-pipelines }}
67+
CI_API_V4_URL: ${{ secrets.ci-api-v4-url }}
68+
ACCESS_TOKEN: ${{ secrets.access-token }}
69+
TRIGGER_TOKEN: ${{ secrets.trigger-token }}
70+
PROJECT_ID: ${{ secrets.project-id }}
71+
72+
73+

index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,32 @@ async function execute (inputConfig, githubConfig) {
115115
return resp.id;
116116
}
117117

118+
function getEnv (name) {
119+
const val = process.env[name] || '';
120+
if (!val) {
121+
throw new Error(`Environment variable expected, but not supplied: ${name}`);
122+
}
123+
return val;
124+
}
125+
126+
function getBooleanEnv (name) {
127+
const trueValue = ['true', 'True', 'TRUE'];
128+
const falseValue = ['false', 'False', 'FALSE'];
129+
const val = getEnv(name);
130+
if (trueValue.includes(val)) return true;
131+
if (falseValue.includes(val)) return false;
132+
throw new TypeError(`Input could not be converted to boolean value: ${name}`);
133+
}
134+
118135
async function main () {
119136
const inputConfig = {
120-
apiUrl: core.getInput('ci-api-v4-url', { required: true }),
121-
accesToken: core.getInput('access-token', { required: true }),
122-
triggerToken: core.getInput('trigger-token', { required: true }),
123-
projectId: core.getInput('project-id', { required: true }),
124-
triggeredRef: core.getInput('triggered-ref', { required: true }),
125-
schedule: core.getBooleanInput('schedule', { required: false }),
126-
cancelOutdatedPipelines: core.getBooleanInput('cancel-outdated-pipelines', { required: false })
137+
apiUrl: getEnv('CI_API_V4_URL'),
138+
accesToken: getEnv('ACCESS_TOKEN'),
139+
triggerToken: getEnv('TRIGGER_TOKEN'),
140+
projectId: getEnv('PROJECT_ID'),
141+
triggeredRef: getEnv('TRIGGERED_REF'),
142+
schedule: getBooleanEnv('SCHEDULE'),
143+
cancelOutdatedPipelines: getBooleanEnv('CANCEL_OUTDATED_PIPELINES')
127144
};
128145

129146
const githubConfig = {

0 commit comments

Comments
 (0)