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