Jenkinsfile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. pipeline {
  2. agent any
  3. /**
  4. * Build file for Brawler
  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 '/opt/env-go/bin/env-go make dist'
  51. }
  52. }
  53. stage('Test') {
  54. // The tests are run in both stages - no release commit is made if the tests fail.
  55. // The output is the coverage data and the badge.
  56. //
  57. steps {
  58. echo 'Running tests ...'
  59. 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"""
  60. sh 'CGO_ENABLED=0 /opt/env-go/bin/env-go go test -p 1 --coverprofile=coverage.out ./...'
  61. sh '/opt/env-go/bin/env-go go tool cover --html=coverage.out -o coverage.html'
  62. echo 'Determine overall coverage and writing badge'
  63. script {
  64. 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"')
  65. echo "Overall coverage is: ${coverage}"
  66. if (coverage.equals("100.0%")) {
  67. 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"""
  68. } else {
  69. 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"""
  70. }
  71. }
  72. }
  73. }
  74. stage('Create Release Build Commit') {
  75. // Check for a non-release build to avoid a commit loop
  76. //
  77. when { not { expression { return commit_msg =~ /chore\(release\)\:/ } } }
  78. steps {
  79. // Before running standard-version it is important to fetch
  80. // the existing tags so next version can be calculated
  81. //
  82. echo 'Running standard version ...'
  83. sshagent (credentials: ['Gogs']) {
  84. sh 'git fetch --tags'
  85. }
  86. sh 'standard-version'
  87. // The new version is inserted into the code
  88. //
  89. script {
  90. new_version = sh(returnStdout: true, script: 'git tag | tail -1 | tr -d "\\n"')
  91. }
  92. echo "Inserting version $new_version into the code"
  93. sh "find . -name '*.go' -exec sed -i -e 's/ProductVersion\\ =\\ \\\".*\\\"/ProductVersion = \\\"${new_version.substring(1)}\\\"/g' {} \\;"
  94. // The commit is amended to include the code change
  95. //
  96. echo "Tagging the build and push the changes into the origin repository"
  97. sshagent (credentials: ['Gogs']) {
  98. sh 'git config user.name "Matthias Ladkau"'
  99. sh 'git config user.email "webmaster@devt.de"'
  100. sh 'git commit -a --amend --no-edit'
  101. sh "git tag --force $new_version"
  102. sh 'git push --tags origin master'
  103. }
  104. }
  105. }
  106. stage('Upload Release Build Commit') {
  107. when { expression { return commit_msg =~ /chore\(release\)\:/ } }
  108. steps {
  109. echo "Uploading release build ..."
  110. // After a successful build the resulting artifacts are
  111. // uploaded for publication
  112. //
  113. sshagent (credentials: ['Gogs']) {
  114. // Clear distribution folder
  115. sh 'ssh -o StrictHostKeyChecking=no -p 7000 krotik@devt.de rm -fR pub/brawler'
  116. sh 'ssh -o StrictHostKeyChecking=no -p 7000 krotik@devt.de mkdir -p pub/brawler'
  117. // Copy distribution packages in place
  118. sh 'scp -P 7000 -o StrictHostKeyChecking=no dist/*.tar.gz krotik@devt.de:~/pub/brawler'
  119. sh 'scp -P 7000 -o StrictHostKeyChecking=no dist/checksums.txt krotik@devt.de:~/pub/brawler'
  120. // Copy coverage in place
  121. sh 'scp -P 7000 -o StrictHostKeyChecking=no coverage.* krotik@devt.de:~/pub/brawler'
  122. // Copy test result in place
  123. sh 'scp -P 7000 -o StrictHostKeyChecking=no test_result.svg krotik@devt.de:~/pub/brawler'
  124. }
  125. }
  126. }
  127. }
  128. }