Skip to content
Snippets Groups Projects
Commit b2c09ea7 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

jobs/tools: Add setup-gitlab-webhooks job

This job is responsible to setup the webhooks on swh GitLab repositories
enabling to trigger Jenkins builds when pushing changes or creating merge
requests.

It parses Jenkins jobs builder YAML configuration to get the name of the
repos with CI setup and set webhook URLs in GitLab projects through HTTP
requests to the GitLab REST API.

It is triggered at the end of the swh-jenkins-jobs-builder job after jobs
have been reconfigured.
parent 6737a1e5
No related branches found
No related tags found
1 merge request!146jobs/tools: Add setup-gitlab-webhooks job
......@@ -37,6 +37,9 @@ pipeline {
steps {
script {
sh('tox -- update --delete-old')
build(
job: '/jenkins-tools/setup-gitlab-webhooks',
)
}
}
}
......
pipeline {
agent any
environment {
GITLAB_TOKEN = credentials("jenkins-gitlab-token")
}
stages {
stage('Checkout Repository') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'master']],
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"]]
}
- 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}"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment