Jenkinsfile 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // CI/CD for Gogs: push and pull requests targeting main.
  2. // Tests/package run in Docker (DinD). Release publishes the HPI to Gogs.
  3. pipeline {
  4. agent none
  5. options {
  6. timestamps()
  7. disableConcurrentBuilds()
  8. timeout(time: 45, unit: 'MINUTES')
  9. buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '10'))
  10. }
  11. stages {
  12. stage('Build') {
  13. agent {
  14. docker {
  15. image 'ura-ci-maven:21'
  16. label 'built-in'
  17. alwaysPull false
  18. args '-u root -v jenkins-m2-cache:/root/.m2 -e MAVEN_OPTS=-Xmx1024m'
  19. }
  20. }
  21. stages {
  22. stage('Checkout') {
  23. steps {
  24. script {
  25. def sha = env.pr_head_sha ?: env.after ?: 'main'
  26. if (!sha?.trim() || sha == 'null' || sha == '0000000000000000000000000000000000000000') {
  27. sha = 'main'
  28. }
  29. echo "Gogs event ref=${env.ref} action=${env.action} pr=#${env.pr_number} sha=${sha}"
  30. env.RELEASE_SHA = sha
  31. checkout([
  32. $class: 'GitSCM',
  33. branches: [[name: sha]],
  34. extensions: [[$class: 'CloneOption', shallow: false, noTags: false]],
  35. userRemoteConfigs: [[url: 'https://git.barcelos.dev/allan/jenkins-mfa-auth-plugin.git']]
  36. ])
  37. }
  38. }
  39. }
  40. stage('Test') {
  41. steps {
  42. sh 'mvn -B -ntp verify'
  43. }
  44. post {
  45. always {
  46. junit allowEmptyResults: true, testResults: 'target/surefire-reports/*.xml'
  47. }
  48. }
  49. }
  50. stage('Package') {
  51. steps {
  52. sh 'mvn -B -ntp -DskipTests -Dset.changelist package'
  53. sh 'ls -la target/*.hpi'
  54. archiveArtifacts artifacts: 'target/*.hpi', fingerprint: true
  55. stash name: 'hpi', includes: 'target/*.hpi'
  56. }
  57. }
  58. }
  59. post {
  60. always {
  61. sh 'chmod -R a+rwX "$WORKSPACE" || true'
  62. }
  63. }
  64. }
  65. stage('Release') {
  66. when {
  67. allOf {
  68. expression { env.ref == 'refs/heads/main' }
  69. expression { !env.pr_number?.trim() || env.pr_number == 'null' }
  70. }
  71. }
  72. agent { label 'built-in' }
  73. steps {
  74. unstash 'hpi'
  75. withCredentials([string(credentialsId: 'gogs-release-token', variable: 'GOGS_RELEASE_TOKEN')]) {
  76. sh '''
  77. set -euo pipefail
  78. HPI=$(ls -1 target/*.hpi | head -1)
  79. SHA="${RELEASE_SHA:-${after:-}}"
  80. if [ -z "$SHA" ] || [ "$SHA" = "null" ]; then
  81. SHA=$(git rev-parse HEAD 2>/dev/null || echo "")
  82. fi
  83. TAG="1.0.${BUILD_NUMBER}"
  84. echo "Publishing ${HPI} as Gogs release ${TAG} sha=${SHA}"
  85. curl -fsS -X POST \
  86. -H "X-Release-Token: ${GOGS_RELEASE_TOKEN}" \
  87. -F "file=@${HPI}" \
  88. -F "tag=${TAG}" \
  89. -F "sha=${SHA}" \
  90. -F "title=mfa-totp ${TAG}" \
  91. -F "body=Jenkins build #${BUILD_NUMBER} (${SHA}). Artifact: $(basename "$HPI")" \
  92. http://host.docker.internal:9377/publish
  93. '''
  94. }
  95. }
  96. }
  97. }
  98. }