Jenkinsfile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. pipeline {
  2. agent any
  3. /**
  4. * Build file for EliasDB
  5. *
  6. * Each build happens with 2 commits. The first commit is the actual
  7. * feature or fix commit. The commit message should follow conventional
  8. * commit messages (https://www.conventionalcommits.org/en/v1.0.0-beta.4/).
  9. * In a second commit a program called standard version
  10. * (https://github.com/conventional-changelog/standard-version) calculates
  11. * a new product version. The versioning will be according to the rules
  12. * of “Semantic Versioning” (https://semver.org/).
  13. *
  14. * Building is done using simple make.
  15. *
  16. * Testing produces code coverage badges which can be embedded web pages.
  17. */
  18. stages {
  19. stage('Commit Analysis') {
  20. steps {
  21. // Read the commit message into a variable
  22. //
  23. script {
  24. commit_msg = sh(returnStdout: true, script: 'git log -1')
  25. }
  26. }
  27. }
  28. stage('Prepare Release Build') {
  29. // Check for a release build (a commit by standard-version)
  30. //
  31. when { expression { return commit_msg =~ /chore\(release\)\:/ } }
  32. steps {
  33. // Find out the tagged version
  34. //
  35. script {
  36. version = sh(returnStdout: true, script: 'git log -1 | grep chore | tr -d "\\n" | sed "s/.*chore(release): \\([0-9\\.]*\\)/\\1/"')
  37. }
  38. echo "Building version: ${version} ..."
  39. }
  40. }
  41. stage('Build') {
  42. when { expression { return commit_msg =~ /chore\(release\)\:/ } }
  43. steps {
  44. // Fetch all git tags and run goreleaser
  45. //
  46. checkout scm
  47. sshagent (credentials: ['Gogs']) {
  48. sh 'git fetch --tags'
  49. }
  50. sh 'mkdir -p .cache'
  51. sh '/opt/env-go/bin/env-go make dist'
  52. }
  53. }
  54. stage('Test') {
  55. // The tests are run in both stages - no release commit is made if the tests fail.
  56. // The output is the coverage data and the badge.
  57. //
  58. steps {
  59. echo 'Running tests ...'
  60. sh """echo '<svg width="88" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h41v20H0z"/><path fill="#fc1" d="M41 0h40v20H41z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="20.5" y="14">tests</text><text x="60" y="14">fail</text></g></svg>' > test_result.svg"""
  61. sh 'CGO_ENABLED=0 /opt/env-go/bin/env-go go test -p 1 --coverprofile=coverage.out ./...'
  62. sh '/opt/env-go/bin/env-go go tool cover --html=coverage.out -o coverage.html'
  63. echo 'Determine overall coverage and writing badge'
  64. script {
  65. coverage = sh(returnStdout: true, script: '/opt/env-go/bin/env-go go tool cover -func=coverage.out | tee coverage.txt | tail -1 | grep -o "[0-9]*.[0-9]*%$" | tr -d "\\n"')
  66. echo "Overall coverage is: ${coverage}"
  67. if (coverage.equals("100.0%")) {
  68. sh """echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#4c1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">$coverage</text></g></svg>' > test_result.svg"""
  69. } else {
  70. sh """echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#fc1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">$coverage</text></g></svg>' > test_result.svg"""
  71. }
  72. }
  73. }
  74. }
  75. stage('Create Release Build Commit') {
  76. // Check for a non-release build to avoid a commit loop
  77. //
  78. when { not { expression { return commit_msg =~ /chore\(release\)\:/ } } }
  79. steps {
  80. // Before running standard-version it is important to fetch
  81. // the existing tags so next version can be calculated
  82. //
  83. echo 'Running standard version ...'
  84. sshagent (credentials: ['Gogs']) {
  85. sh 'git fetch --tags'
  86. }
  87. sh 'standard-version'
  88. // The new version is inserted into the code
  89. //
  90. script {
  91. new_version = sh(returnStdout: true, script: 'git tag | tail -1 | tr -d "\\n"')
  92. }
  93. echo "Inserting version $new_version into the code"
  94. sh "find . -name '*.go' -exec sed -i -e 's/ProductVersion\\ =\\ \\\".*\\\"/ProductVersion = \\\"${new_version.substring(1)}\\\"/g' {} \\;"
  95. // The commit is amended to include the code change
  96. //
  97. echo "Tagging the build and push the changes into the origin repository"
  98. sshagent (credentials: ['Gogs']) {
  99. sh 'git config user.name "Matthias Ladkau"'
  100. sh 'git config user.email "webmaster@devt.de"'
  101. sh 'git commit -a --amend --no-edit'
  102. sh "git tag --force $new_version"
  103. sh 'git push --tags origin master'
  104. }
  105. }
  106. }
  107. stage('Upload Release Build Commit') {
  108. when { expression { return commit_msg =~ /chore\(release\)\:/ } }
  109. steps {
  110. echo "Uploading release build ..."
  111. // After a successful build the resulting artifacts are
  112. // uploaded for publication
  113. //
  114. sshagent (credentials: ['Gogs']) {
  115. // Clear distribution folder
  116. sh 'ssh -o StrictHostKeyChecking=no -p 7000 krotik@devt.de rm -fR pub/eliasdb'
  117. sh 'ssh -o StrictHostKeyChecking=no -p 7000 krotik@devt.de mkdir -p pub/eliasdb'
  118. // Copy distribution packages in place
  119. sh 'scp -P 7000 -o StrictHostKeyChecking=no dist/*.tar.gz krotik@devt.de:~/pub/eliasdb'
  120. sh 'scp -P 7000 -o StrictHostKeyChecking=no dist/checksums.txt krotik@devt.de:~/pub/eliasdb'
  121. // Copy coverage in place
  122. sh 'scp -P 7000 -o StrictHostKeyChecking=no coverage.* krotik@devt.de:~/pub/eliasdb'
  123. // Copy test result in place
  124. sh 'scp -P 7000 -o StrictHostKeyChecking=no test_result.svg krotik@devt.de:~/pub/eliasdb'
  125. }
  126. }
  127. }
  128. }
  129. }