Jenkinsfile-on-prem 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. pipeline {
  2. agent {
  3. node {
  4. label 'maven'
  5. }
  6. }
  7. parameters {
  8. string(name:'TAG_NAME',defaultValue: '',description:'')
  9. }
  10. environment {
  11. HARBOR_CREDENTIAL_ID = 'harbor-id'
  12. GITLAB_CREDENTIAL_ID = 'gitlab-id'
  13. KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
  14. REGISTRY = 'harbor.devops.kubesphere.local:30280'
  15. HARBOR_NAMESPACE = 'library'
  16. GITLAB_ACCOUNT = 'admin1'
  17. APP_NAME = 'devops-java-sample'
  18. }
  19. stages {
  20. stage ('checkout scm') {
  21. steps {
  22. checkout(scm)
  23. }
  24. }
  25. stage ('unit test') {
  26. steps {
  27. container ('maven') {
  28. sh 'mvn clean -o -gs `pwd`/configuration/settings.xml test'
  29. }
  30. }
  31. }
  32. stage ('build & push') {
  33. steps {
  34. container ('maven') {
  35. sh 'mvn -o -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
  36. sh 'docker build -f Dockerfile-on-prem -t $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
  37. withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$HARBOR_CREDENTIAL_ID" ,)]) {
  38. sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
  39. sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
  40. }
  41. }
  42. }
  43. }
  44. stage('push latest'){
  45. when{
  46. branch 'master'
  47. }
  48. steps{
  49. container ('maven') {
  50. sh 'docker tag $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:latest '
  51. sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:latest '
  52. }
  53. }
  54. }
  55. stage('deploy to dev') {
  56. when{
  57. branch 'master'
  58. }
  59. steps {
  60. input(id: 'deploy-to-dev', message: 'deploy to dev?')
  61. kubernetesDeploy(configs: 'deploy/dev/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
  62. }
  63. }
  64. stage('push with tag'){
  65. when{
  66. expression{
  67. return params.TAG_NAME =~ /v.*/
  68. }
  69. }
  70. steps {
  71. container ('maven') {
  72. input(id: 'release-image-with-tag', message: 'release image with tag?')
  73. withCredentials([usernamePassword(credentialsId: "$GITLAB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
  74. sh 'git config --global user.email "kubesphere@yunify.com" '
  75. sh 'git config --global user.name "kubesphere" '
  76. sh 'git tag -a $TAG_NAME -m "$TAG_NAME" '
  77. sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.devops.kubesphere.local:30080/$GITLAB_ACCOUNT/devops-java-sample.git --tags --ipv4'
  78. }
  79. sh 'docker tag $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:$TAG_NAME '
  80. sh 'docker push $REGISTRY/$HARBOR_NAMESPACE/$APP_NAME:$TAG_NAME '
  81. }
  82. }
  83. }
  84. stage('deploy to production') {
  85. when{
  86. expression{
  87. return params.TAG_NAME =~ /v.*/
  88. }
  89. }
  90. steps {
  91. input(id: 'deploy-to-production', message: 'deploy to production?')
  92. kubernetesDeploy(configs: 'deploy/prod/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
  93. }
  94. }
  95. }
  96. }