diff --git a/jobs/tools/dockerfiles.yaml b/jobs/tools/dockerfiles.yaml index f515625..8a64772 100644 --- a/jobs/tools/dockerfiles.yaml +++ b/jobs/tools/dockerfiles.yaml @@ -1,20 +1,49 @@ -- job: +- job: &swh_jenkins_dockerfiles name: jenkins-tools/swh-jenkins-dockerfiles node: built-in scm: - git: url: http://forge.softwareheritage.org/source/swh-jenkins-dockerfiles.git wipe-workspace: false triggers: - pollscm: cron: "H/30 * * * *" - - timed: '@daily' - auth-token: 'ph4br1cat0r' + - timed: "@daily" + auth-token: "ph4br1cat0r" properties: - build-discarder: days-to-keep: 7 wrappers: - timestamps - ansicolor builders: - shell: make checkrebuild all + +# we need a job-template to substitute gitlab_* variables +- job-template: + name: jenkins-tools/{dockerfiles_job_name} + scm: + - git: + url: "{gitlab_url}/swh/infra/ci-cd/swh-jenkins-dockerfiles.git" + wipe-workspace: false + auth-token: + properties: + - gitlab: + connection: "{gitlab_connection_name}" + triggers: + - gitlab: + trigger-push: true + trigger-merge-request: true + add-ci-message: true + cancel-pending-builds-on-update: true + # secret jenkins token is generated when executing tox + secret-token: !include-raw: jobs/templates/jenkins-token + publishers: + - gitlab-notifier + <<: *swh_jenkins_dockerfiles + +- project: + name: gitlab-swh-jenkins-dockerfiles + dockerfiles_job_name: "{name}" + jobs: + - "jenkins-tools/{dockerfiles_job_name}" diff --git a/jobs/tools/setup-gitlab-webhooks.groovy.j2 b/jobs/tools/setup-gitlab-webhooks.groovy.j2 index 467f8cc..3cbdc61 100644 --- a/jobs/tools/setup-gitlab-webhooks.groovy.j2 +++ b/jobs/tools/setup-gitlab-webhooks.groovy.j2 @@ -1,88 +1,91 @@ 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) + setupGitlabWebhook("swh/infra/ci-cd/swh-jenkins-dockerfiles", + "jenkins-tools/gitlab-swh-jenkins-dockerfiles", + true, true, false) setupGitlabWebhook("swh/devel/swh-docs", "DDOC/gitlab-builds", 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") setupGitlabWebhook(gitlabProjectName, "${jenkinsFolder}/gitlab-incoming-tag", false, false, true) } } } } } } } 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"]] }