Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. String cron_string = BRANCH_NAME == "master" ? "@daily" : ""
  2. pipeline {
  3. agent any
  4. environment {
  5. GIT_PROJECT = "chat-sharelatex"
  6. JENKINS_WORKFLOW = "chat-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.14.1'
  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('Unit Tests') {
  43. steps {
  44. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_unit'
  45. }
  46. }
  47. stage('Acceptance Tests') {
  48. steps {
  49. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_acceptance'
  50. }
  51. }
  52. stage('Package and publish build') {
  53. steps {
  54. sh 'echo ${BUILD_NUMBER} > build_number.txt'
  55. sh 'touch build.tar.gz' // Avoid tar warning about files changing during read
  56. sh 'tar -czf build.tar.gz --exclude=build.tar.gz --exclude-vcs .'
  57. withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
  58. s3Upload(file:'build.tar.gz', bucket:"${S3_BUCKET_BUILD_ARTEFACTS}", path:"${JOB_NAME}/${BUILD_NUMBER}.tar.gz")
  59. }
  60. }
  61. }
  62. stage('Publish build number') {
  63. steps {
  64. sh 'echo ${BRANCH_NAME}-${BUILD_NUMBER} > build_number.txt'
  65. withAWS(credentials:'S3_CI_BUILDS_AWS_KEYS', region:"${S3_REGION_BUILD_ARTEFACTS}") {
  66. // The deployment process uses this file to figure out the latest build
  67. s3Upload(file:'build_number.txt', bucket:"${S3_BUCKET_BUILD_ARTEFACTS}", path:"${JOB_NAME}/latest")
  68. }
  69. }
  70. }
  71. }
  72. post {
  73. always {
  74. sh 'DOCKER_COMPOSE_FLAGS="-f docker-compose.ci.yml" make test_clean'
  75. sh 'make clean'
  76. }
  77. success {
  78. withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
  79. sh "curl $GIT_API_URL \
  80. --data '{ \
  81. \"state\" : \"success\", \
  82. \"target_url\": \"$TARGET_URL\", \
  83. \"description\": \"Your build succeeded!\", \
  84. \"context\": \"ci/jenkins\" }' \
  85. -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
  86. }
  87. }
  88. failure {
  89. mail(from: "${EMAIL_ALERT_FROM}",
  90. to: "${EMAIL_ALERT_TO}",
  91. subject: "Jenkins build failed: ${JOB_NAME}:${BUILD_NUMBER}",
  92. body: "Build: ${BUILD_URL}")
  93. withCredentials([usernamePassword(credentialsId: 'GITHUB_INTEGRATION', usernameVariable: 'GH_AUTH_USERNAME', passwordVariable: 'GH_AUTH_PASSWORD')]) {
  94. sh "curl $GIT_API_URL \
  95. --data '{ \
  96. \"state\" : \"failure\", \
  97. \"target_url\": \"$TARGET_URL\", \
  98. \"description\": \"Your build failed\", \
  99. \"context\": \"ci/jenkins\" }' \
  100. -u $GH_AUTH_USERNAME:$GH_AUTH_PASSWORD"
  101. }
  102. }
  103. }
  104. // The options directive is for configuration that applies to the whole job.
  105. options {
  106. // we'd like to make sure remove old builds, so we don't fill up our storage!
  107. buildDiscarder(logRotator(numToKeepStr:'50'))
  108. // And we'd really like to be sure that this build doesn't hang forever, so let's time it out after:
  109. timeout(time: 30, unit: 'MINUTES')
  110. }
  111. }