Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9338722
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Subscribers
None
View Options
diff --git a/jobs/tools/setup-gitlab-webhooks.groovy.j2 b/jobs/tools/setup-gitlab-webhooks.groovy.j2
new file mode 100644
index 0000000..03db927
--- /dev/null
+++ b/jobs/tools/setup-gitlab-webhooks.groovy.j2
@@ -0,0 +1,84 @@
+pipeline {
+ agent any
+
+ environment {
+ GITLAB_TOKEN = credentials("jenkins-gitlab-token")
+ }
+
+ stages {
+
+ stage('Checkout Repository') {
+ steps {
+ checkout([
+ $class: 'GitSCM',
+ branches: [[name: 'gitlab-integration']],
+ userRemoteConfigs: [[
+ url: "http://forge.softwareheritage.org/source/swh-jenkins-jobs.git",
+ ]],
+ ])
+ }
+ }
+
+ stage('Setup gitlab integration') {
+ steps {
+ script {
+ setupGitlabWebhook("swh/infra/ci-cd/swh-jenkins-jobs",
+ "jenkins-tools/swh-jenkins-jobs-builder",
+ true, true, false)
+
+ projects = readYaml(file: 'jobs/swh-packages.yaml')
+ for (project in projects) {
+ if (project.containsKey("project")) {
+ def jenkinsFolder = project.get('project').get('name')
+ def repoName= project.get('project').get('repo_name')
+ def gitlabProjectName = "swh/devel/${repoName}"
+ setupGitlabWebhook(gitlabProjectName, "${jenkinsFolder}/gitlab-tests")
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+void setupGitlabWebhook(gitlabProjectName, jenkinsProjectName, pushEvents = true,
+ mergeRequestEvents = true, tagPushEvents = false) {
+
+ def webhookUrl = "${jenkins_url}/project/${jenkinsProjectName}"
+ def gitlabProjectEncoded = java.net.URLEncoder.encode(gitlabProjectName, "UTF-8")
+ def payload = """
+ {
+ "id": "${gitlabProjectEncoded}",
+ "url": "${webhookUrl}",
+ "push_events": "${pushEvents}",
+ "merge_requests_events": "${mergeRequestEvents}",
+ "tag_push_events": "${tagPushEvents}",
+ "token": "{{jenkins_token}}"
+ }
+ """
+
+ def url = "${gitlab_url}/api/v4/projects/${gitlabProjectEncoded}/hooks"
+
+ // get current webhooks of the gitlab project
+ def response = httpRequest httpMode: 'GET', url: url,
+ contentType: 'APPLICATION_JSON',
+ customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]]
+
+ def hooks = readJSON text: response.content
+ for (hook in hooks) {
+ if (hook.url == webhookUrl) {
+ // update previously set webhook for jenkins job
+ httpRequest httpMode: 'PUT', url: "${url}/${hook.id}",
+ contentType: 'APPLICATION_JSON', requestBody: payload,
+ customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]]
+ return
+ }
+ }
+
+ // add webhook for jenkins job
+ httpRequest httpMode: 'POST', url: url,
+ contentType: 'APPLICATION_JSON', requestBody: payload,
+ customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]]
+
+}
+
diff --git a/jobs/tools/setup-gitlab-webhooks.yaml b/jobs/tools/setup-gitlab-webhooks.yaml
new file mode 100644
index 0000000..f5eba6c
--- /dev/null
+++ b/jobs/tools/setup-gitlab-webhooks.yaml
@@ -0,0 +1,25 @@
+- job-template:
+ name: jenkins-tools/{name}
+ project-type: pipeline
+ description: Setup Jenkins integration for a GitLab repository
+ node: built-in
+ # secret jenkins token is generated when executing tox
+ jenkins_token: !include-raw: jobs/templates/jenkins-token
+ parameters:
+ - string:
+ name: gitlab_url
+ description: URL of GitLab instance
+ default: https://gitlab-staging.swh.network
+ - string:
+ name: jenkins_url
+ description: URL of Jenkins instance
+ default: https://jenkins.softwareheritage.org
+
+ dsl: !include-jinja2: setup-gitlab-webhooks.groovy.j2
+
+# we use a project and a job template here as we need jinja2 processing
+# to inline the generated jenkins token into the pipeline groovy script
+- project:
+ name: setup-gitlab-webhooks
+ jobs:
+ - "jenkins-tools/{name}"
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Jul 4 2025, 9:04 AM (6 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3247183
Attached To
rCJSWH Jenkins jobs
Event Timeline
Log In to Comment