Просмотр исходного кода

Merge pull request #743 from sharelatex/hof-github-jenkins

Update github commit status in Jenkins pipeline
Hayden Faulds 8 лет назад
Родитель
Сommit
71351476f7
1 измененных файлов с 56 добавлено и 17 удалено
  1. 56 17
      services/web/Jenkinsfile

+ 56 - 17
services/web/Jenkinsfile

@@ -1,19 +1,37 @@
 String cron_string = BRANCH_NAME == "master" ? "@daily" : ""
 String cron_string = BRANCH_NAME == "master" ? "@daily" : ""
 
 
 pipeline {
 pipeline {
-  
+
   agent any
   agent any
-  
+
   environment  {
   environment  {
       HOME = "/tmp"
       HOME = "/tmp"
+      GIT_PROJECT = "web-sharelatex-internal"
+      JENKINS_WORKFLOW = "web-sharelatex-internal"
+      TARGET_URL = "${env.JENKINS_URL}blue/organizations/jenkins/${JENKINS_WORKFLOW}/detail/$BRANCH_NAME/$BUILD_NUMBER/pipeline"
+      GIT_API_URL = "https://api.github.com/repos/sharelatex/${GIT_PROJECT}/statuses/$GIT_COMMIT"
   }
   }
-  
+
   triggers {
   triggers {
     pollSCM('* * * * *')
     pollSCM('* * * * *')
     cron(cron_string)
     cron(cron_string)
   }
   }
-  
+
   stages {
   stages {
+    stage('Pre') {
+      steps {
+        withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
+          sh "curl $GIT_API_URL \
+            --data '{ \
+            \"state\" : \"pending\", \
+            \"target_url\": \"$TARGET_URL\", \
+            \"description\": \"Your build is underway\", \
+            \"context\": \"ci/jenkins\" }' \
+            -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
+        }
+      }
+    }
+
     stage('Install modules') {
     stage('Install modules') {
       steps {
       steps {
         sshagent (credentials: ['GIT_DEPLOY_KEY']) {
         sshagent (credentials: ['GIT_DEPLOY_KEY']) {
@@ -21,7 +39,7 @@ pipeline {
         }
         }
       }
       }
     }
     }
-    
+
     stage('Install') {
     stage('Install') {
       agent {
       agent {
         docker {
         docker {
@@ -66,7 +84,7 @@ pipeline {
         sh 'make --no-print-directory lint'
         sh 'make --no-print-directory lint'
       }
       }
     }
     }
-    
+
     stage('Test and Minify') {
     stage('Test and Minify') {
       parallel {
       parallel {
         stage('Unit Test') {
         stage('Unit Test') {
@@ -80,7 +98,7 @@ pipeline {
             sh 'make --no-print-directory test_unit MOCHA_ARGS="--reporter tap"'
             sh 'make --no-print-directory test_unit MOCHA_ARGS="--reporter tap"'
           }
           }
         }
         }
-        
+
         stage('Acceptance Test') {
         stage('Acceptance Test') {
           steps {
           steps {
             // Spawns its own docker containers
             // Spawns its own docker containers
@@ -108,7 +126,7 @@ pipeline {
         sh 'make --no-print-directory test_frontend'
         sh 'make --no-print-directory test_frontend'
       }
       }
     }
     }
-    
+
     stage('Package') {
     stage('Package') {
       steps {
       steps {
         sh 'rm -rf ./node_modules/grunt*'
         sh 'rm -rf ./node_modules/grunt*'
@@ -117,7 +135,7 @@ pipeline {
         sh 'tar -czf build.tar.gz --exclude=build.tar.gz --exclude-vcs .'
         sh 'tar -czf build.tar.gz --exclude=build.tar.gz --exclude-vcs .'
       }
       }
     }
     }
-    
+
     stage('Publish') {
     stage('Publish') {
       steps {
       steps {
         withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
         withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
@@ -127,8 +145,8 @@ pipeline {
         }
         }
       }
       }
     }
     }
-    
-    
+
+
     stage('Sync OSS') {
     stage('Sync OSS') {
       when {
       when {
         branch 'master'
         branch 'master'
@@ -140,29 +158,50 @@ pipeline {
       }
       }
     }
     }
   }
   }
-  
+
   post {
   post {
     always {
     always {
       sh 'make clean_ci'
       sh 'make clean_ci'
     }
     }
 
 
+    success {
+      withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
+        sh "curl $GIT_API_URL \
+          --data '{ \
+          \"state\" : \"success\", \
+          \"target_url\": \"$TARGET_URL\", \
+          \"description\": \"Your build succeeded!\", \
+          \"context\": \"ci/jenkins\" }' \
+          -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
+      }
+    }
+
     failure {
     failure {
-      mail(from: "${EMAIL_ALERT_FROM}", 
-           to: "${EMAIL_ALERT_TO}", 
+      mail(from: "${EMAIL_ALERT_FROM}",
+           to: "${EMAIL_ALERT_TO}",
            subject: "Jenkins build failed: ${JOB_NAME}:${BUILD_NUMBER}",
            subject: "Jenkins build failed: ${JOB_NAME}:${BUILD_NUMBER}",
            body: "Build: ${BUILD_URL}")
            body: "Build: ${BUILD_URL}")
+      withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
+        sh "curl $GIT_API_URL \
+          --data '{ \
+          \"state\" : \"failure\", \
+          \"target_url\": \"$TARGET_URL\", \
+          \"description\": \"Your build failed\", \
+          \"context\": \"ci/jenkins\" }' \
+          -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
+      }
     }
     }
   }
   }
-  
+
 
 
   // The options directive is for configuration that applies to the whole job.
   // The options directive is for configuration that applies to the whole job.
   options {
   options {
     // Only build one at a time
     // Only build one at a time
     disableConcurrentBuilds()
     disableConcurrentBuilds()
-    
+
     // we'd like to make sure remove old builds, so we don't fill up our storage!
     // we'd like to make sure remove old builds, so we don't fill up our storage!
     buildDiscarder(logRotator(numToKeepStr:'50'))
     buildDiscarder(logRotator(numToKeepStr:'50'))
-    
+
     // And we'd really like to be sure that this build doesn't hang forever, so let's time it out after:
     // And we'd really like to be sure that this build doesn't hang forever, so let's time it out after:
     timeout(time: 30, unit: 'MINUTES')
     timeout(time: 30, unit: 'MINUTES')
   }
   }