Skip to content

Commit d3bc251

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

File tree

2 files changed

+113
-7
lines changed

2 files changed

+113
-7
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
id: authorize-job
40+
steps:
41+
- name: Authorization confirmation
42+
run: echo "Authorized the job to run" # This step will only execute if
43+
# the pipeline has necessary approvals to run
44+
45+
# We need workflow resolution to run on non-private runner due to required dependencies
46+
# missing in private runner. Additionally for clarity it is separated into separate job
47+
resolve-workflow-ref:
48+
needs: authorize
49+
runs-on: ubuntu-latest
50+
steps:
51+
- name: Get Workflow Version
52+
uses: canonical/get-workflow-version-action@a5d53b08d254a157ea441c9819ea5002ffc12edc
53+
id: workflow-ref
54+
with:
55+
repository-name: NordSecurity/trigger-gitlab-pipeline
56+
file-name: .github/workflows/trigger-gitlab-pipeline.yml
57+
outputs:
58+
workflow-ref: ${{ steps.workflow-ref.output.sha }}
59+
60+
trigger-gitlab-pipeline:
61+
# In the current form, "authorize" job is implicitly required by trigger-gitlab-pipeline job
62+
# To make this dependency super explicit and a bit more future-proof against modifications of
63+
# this workflow - it is stated explicitly in the list of dependencies, even though it is
64+
# redundant
65+
needs: [resolve-workflow-ref, authorize]
66+
runs-on: [self-hosted, gitlab]
67+
steps:
68+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
69+
with:
70+
repository: NordSecurity/trigger-gitlab-pipeline
71+
ref: ${{ needs.resolve-workflow-ref.outputs.workflow-ref }}
72+
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
73+
with:
74+
node-version: 20
75+
- name: Dependencies install
76+
run: npm install
77+
- name: Run triggering script
78+
run: node index.js # It will not be accessible as of now, but it is enough for testing.
79+
env:
80+
TRIGGERED_REF: ${{ inputs.triggered-ref }}
81+
SCHEDULE: ${{ inputs.schedule }}
82+
CANCEL_OUTDATED_PIPELINES: ${{ inputs.cancel-outdated-pipelines }}
83+
CI_API_V4_URL: ${{ secrets.ci-api-v4-url }}
84+
ACCESS_TOKEN: ${{ secrets.access-token }}
85+
TRIGGER_TOKEN: ${{ secrets.trigger-token }}
86+
PROJECT_ID: ${{ secrets.project-id }}
87+
88+
89+

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)