Jenkinsfile-online 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. pipeline {
  2. agent {
  3. node {
  4. label 'maven'
  5. }
  6. }
  7. parameters {
  8. string(name:'TAG_NAME',defaultValue: '',description:'')
  9. }
  10. environment {
  11. DOCKER_CREDENTIAL_ID = 'mydockerhub'
  12. GITHUB_CREDENTIAL_ID = 'mygitlab'
  13. KUBECONFIG_CREDENTIAL_ID = 'mykubeconfig'
  14. REGISTRY = '192.168.122.21:30002'
  15. DOCKERHUB_NAMESPACE = 'testmaven' // Change me
  16. GITHUB_ACCOUNT = 'test'
  17. APP_NAME = 'devops-java-sample'
  18. SONAR_CREDENTIAL_ID = 'sonartoken'
  19. }
  20. stages {
  21. stage ('checkout scm') {
  22. steps {
  23. checkout(scm)
  24. }
  25. }
  26. stage ('unit test') {
  27. steps {
  28. container ('maven') {
  29. sh 'mvn clean -gs `pwd`/configuration/settings.xml test'
  30. }
  31. }
  32. }
  33. stage('sonarqube analysis') {
  34. steps {
  35. container ('maven') {
  36. withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) {
  37. withSonarQubeEnv('sonar') {
  38. sh "mvn sonar:sonar -gs `pwd`/configuration/settings.xml -Dsonar.login=$SONAR_TOKEN"
  39. }
  40. }
  41. timeout(time: 5, unit: 'HOURS') {
  42. waitForQualityGate abortPipeline: true
  43. }
  44. }
  45. }
  46. }
  47. stage ('build & push') {
  48. steps {
  49. container ('maven') {
  50. sh 'mvn -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
  51. archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
  52. sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
  53. withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
  54. sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
  55. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
  56. }
  57. }
  58. }
  59. }
  60. stage('push latest'){
  61. when{
  62. anyOf {
  63. branch 'master'; branch 'sonarqube'
  64. }
  65. }
  66. steps{
  67. container ('maven') {
  68. sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
  69. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
  70. }
  71. }
  72. }
  73. stage('deploy to dev') {
  74. when{
  75. anyOf {
  76. branch 'master'; branch 'sonarqube'
  77. }
  78. }
  79. steps {
  80. input(id: 'deploy-to-dev', message: 'deploy to dev?')
  81. container("maven") {
  82. withCredentials([
  83. kubeconfigFile(
  84. credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
  85. variable: 'KUBECONFIG')
  86. ]) {
  87. sh '''
  88. envsubst < deploy/dev-ol/devops-sample-svc.yaml | kubectl apply -f -
  89. envsubst < deploy/dev-ol/devops-sample.yaml | kubectl apply -f -
  90. '''
  91. }
  92. }
  93. }
  94. }
  95. stage('push with tag'){
  96. when{
  97. expression{
  98. return params.TAG_NAME =~ /v.*/
  99. }
  100. }
  101. steps {
  102. container ('maven') {
  103. input(id: 'release-image-with-tag', message: 'release image with tag?')
  104. withCredentials([usernamePassword(credentialsId: "$GITHUB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
  105. sh 'git config --global user.email "kubesphere@yunify.com" '
  106. sh 'git config --global user.name "kubesphere" '
  107. sh 'git tag -a $TAG_NAME -m "$TAG_NAME" '
  108. sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.yinbin.ink:8/$GITHUB_ACCOUNT/devops-java-sample.git --tags --ipv4'
  109. }
  110. sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG_NAME '
  111. sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG_NAME '
  112. }
  113. }
  114. }
  115. stage('deploy to production') {
  116. when{
  117. expression{
  118. return params.TAG_NAME =~ /v.*/
  119. }
  120. }
  121. steps {
  122. input(id: 'deploy-to-production', message: 'deploy to production?')
  123. container("maven") {
  124. withCredentials([
  125. kubeconfigFile(
  126. credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
  127. variable: 'KUBECONFIG')
  128. ]) {
  129. sh '''
  130. envsubst < deploy/prod-ol/devops-sample-svc.yaml | kubectl apply -f -
  131. envsubst < deploy/prod-ol/devops-sample.yaml | kubectl apply -f -
  132. '''
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }