Jenkinsfile 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. String cron_string = BRANCH_NAME == "master" ? "@daily" : ""
  2. pipeline {
  3. agent any
  4. environment {
  5. GIT_PROJECT = "document-updater-sharelatex"
  6. JENKINS_WORKFLOW = "document-updater-sharelatex"
  7. TARGET_URL = "${env.JENKINS_URL}blue/organizations/jenkins/${JENKINS_WORKFLOW}/detail/$BRANCH_NAME/$BUILD_NUMBER/pipeline"
  8. GIT_API_URL = "https://api.github.com/repos/sharelatex/${GIT_PROJECT}/statuses/$GIT_COMMIT"
  9. }
  10. triggers {
  11. pollSCM('* * * * *')
  12. cron(cron_string)
  13. }
  14. stages {
  15. stage('Install') {
  16. agent {
  17. docker {
  18. image 'node:6.9.5'
  19. args "-v /var/lib/jenkins/.npm:/tmp/.npm -e HOME=/tmp"
  20. reuseNode true
  21. }
  22. }
  23. steps {
  24. withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
  25. sh "curl $GIT_API_URL \
  26. --data '{ \
  27. \"state\" : \"pending\", \
  28. \"target_url\": \"$TARGET_URL\", \
  29. \"description\": \"Your build is underway\", \
  30. \"context\": \"ci/jenkins\" }' \
  31. -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
  32. }
  33. // we need to disable logallrefupdates, else git clones
  34. // during the npm install will require git to lookup the
  35. // user id which does not exist in the container's
  36. // /etc/passwd file, causing the clone to fail.
  37. sh 'git config --global core.logallrefupdates false'
  38. sh 'rm -rf node_modules'
  39. sh 'npm install && npm rebuild'
  40. }
  41. }
  42. stage('Compile') {
  43. agent {
  44. docker {
  45. image 'node:6.9.5'
  46. reuseNode true
  47. }
  48. }
  49. steps {
  50. sh 'npm run compile:all'
  51. }
  52. }
  53. stage('Unit Tests') {
  54. steps {
  55. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_unit'
  56. }
  57. }
  58. stage('Acceptance Tests') {
  59. steps {
  60. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_acceptance'
  61. }
  62. }
  63. stage('Package and publish build') {
  64. steps {
  65. sh 'echo ${BUILD_NUMBER} > build_number.txt'
  66. sh 'touch build.tar.gz' // Avoid tar warning about files changing during read
  67. sh 'tar -czf build.tar.gz --exclude=build.tar.gz --exclude-vcs .'
  68. withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
  69. s3Upload(file:'build.tar.gz', bucket:"${S3_BUCKET_BUILD_ARTEFACTS}", path:"${JOB_NAME}/${BUILD_NUMBER}.tar.gz")
  70. }
  71. }
  72. }
  73. stage('Publish build number') {
  74. steps {
  75. sh 'echo ${BRANCH_NAME}-${BUILD_NUMBER} > build_number.txt'
  76. withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
  77. // The deployment process uses this file to figure out the latest build
  78. s3Upload(file:'build_number.txt', bucket:"${S3_BUCKET_BUILD_ARTEFACTS}", path:"${JOB_NAME}/latest")
  79. }
  80. }
  81. }
  82. }
  83. post {
  84. always {
  85. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_clean'
  86. sh 'make clean'
  87. }
  88. success {
  89. withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
  90. sh "curl $GIT_API_URL \
  91. --data '{ \
  92. \"state\" : \"success\", \
  93. \"target_url\": \"$TARGET_URL\", \
  94. \"description\": \"Your build succeeded!\", \
  95. \"context\": \"ci/jenkins\" }' \
  96. -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
  97. }
  98. }
  99. failure {
  100. mail(from: "${EMAIL_ALERT_FROM}",
  101. to: "${EMAIL_ALERT_TO}",
  102. subject: "Jenkins build failed: ${JOB_NAME}:${BUILD_NUMBER}",
  103. body: "Build: ${BUILD_URL}")
  104. withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
  105. sh "curl $GIT_API_URL \
  106. --data '{ \
  107. \"state\" : \"failure\", \
  108. \"target_url\": \"$TARGET_URL\", \
  109. \"description\": \"Your build failed\", \
  110. \"context\": \"ci/jenkins\" }' \
  111. -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
  112. }
  113. }
  114. }
  115. // The options directive is for configuration that applies to the whole job.
  116. options {
  117. // we'd like to make sure remove old builds, so we don't fill up our storage!
  118. buildDiscarder(logRotator(numToKeepStr:'50'))
  119. // And we'd really like to be sure that this build doesn't hang forever, so let's time it out after:
  120. timeout(time: 30, unit: 'MINUTES')
  121. }
  122. }