diff --git a/jobs/tools/gitlab-jenkins-integration.yaml b/jobs/tools/gitlab-jenkins-integration.yaml new file mode 100644 index 0000000..8ba0af6 --- /dev/null +++ b/jobs/tools/gitlab-jenkins-integration.yaml @@ -0,0 +1,96 @@ +- job: + name: jenkins-tools/gitlab-jenkins-integration + project-type: pipeline + description: Setup Jenkins integration for a GitLab repository + node: built-in + parameters: + - credentials: + name: gitlab_token + type: secrettext + default: jenkins-gitlab-token + description: GitLab token used by Jenkins to query GitLab API + - credentials: + name: jenkins_token + type: secrettext + default: gitlab-jenkins-token + description: Jenkins token used by GitLab to trigger jobs + - string: + name: gitlab_url + description: URL of GitLab instance + default: https://gitlab-staging.swh.network + - string: + name: gitlab_project + description: Name of the project in the GitLab instance to trigger builds on Jenkins + - string: + name: jenkins_job_name + description: Name of the Jenkins job to be triggered by GitLab + - string: + name: jenkins_url + description: URL of Jenkins instance + default: https://jenkins.softwareheritage.org + - bool: + name: push_events + description: Wether to trigger builds when pushing commits + default: true + - bool: + name: merge_request_events + description: Wether to trigger builds on merge requests + default: true + - bool: + name: tag_push_events + description: Wether to trigger builds when pushing tags + default: false + + dsl: | + pipeline { + agent any + + environment { + GITLAB_TOKEN = credentials("${gitlab_token}") + JENKINS_TOKEN = credentials("${jenkins_token}") + } + + stages { + stage('Setup gitlab integration') { + steps { + script { + def gitlabProjectEncoded = java.net.URLEncoder.encode("${gitlab_project}", "UTF-8") + def webhookUrl = "${jenkins_url}/project/${jenkins_job_name}" + + def payload = """ + { + "id": "${gitlabProjectEncoded}", + "url": "${webhookUrl}", + "push_events": "${push_events}", + "merge_requests_events": "${merge_request_events}", + "tag_push_events": "${tag_push_events}", + "token": "$JENKINS_TOKEN" + } + """ + + def url = "${gitlab_url}/api/v4/projects/${gitlabProjectEncoded}/hooks" + + // get current webhooks of the gitlab project + def response = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', + httpMode: 'GET', url: url, + customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]] + + def hooks = readJSON text: response.content + for (hook in hooks) { + if (hook.url == webhookUrl) { + // remove previously set webhook for jenkins job + httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', + httpMode: 'DELETE', url: "${url}/${hook.id}", + customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]] + } + } + + // add webhook for jenkins job + httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', + httpMode: 'POST', url: url, requestBody: payload, + customHeaders: [[name: 'Authorization', value: "Bearer $GITLAB_TOKEN"]] + } + } + } + } + }