123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- pipeline {
- agent {
- node {
- label 'maven'
- }
- }
- parameters {
- string(name:'TAG_NAME',defaultValue: '',description:'')
- }
- environment {
- DOCKER_CREDENTIAL_ID = 'mydockerhub'
- GITHUB_CREDENTIAL_ID = 'mygitlab'
- KUBECONFIG_CREDENTIAL_ID = 'mykubeconfig'
- REGISTRY = '192.168.122.21:30002'
- DOCKERHUB_NAMESPACE = 'testmaven' // Change me
- GITHUB_ACCOUNT = 'test'
- APP_NAME = 'devops-java-sample'
- SONAR_CREDENTIAL_ID = 'sonartoken'
- }
- stages {
- stage ('checkout scm') {
- steps {
- checkout(scm)
- }
- }
- stage ('unit test') {
- steps {
- container ('maven') {
- sh 'mvn clean -gs `pwd`/configuration/settings.xml test'
- }
- }
- }
- stage('sonarqube analysis') {
- steps {
- container ('maven') {
- withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) {
- withSonarQubeEnv('sonar') {
- sh "mvn sonar:sonar -gs `pwd`/configuration/settings.xml -Dsonar.login=$SONAR_TOKEN"
- }
- }
- timeout(time: 5, unit: 'HOURS') {
- waitForQualityGate abortPipeline: true
- }
- }
- }
- }
-
- stage ('build & push') {
- steps {
- container ('maven') {
- sh 'mvn -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
- archiveArtifacts artifacts: 'target/*.jar', fingerprint: true
- sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
- withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
- sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
- sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER'
- }
- }
- }
- }
- stage('push latest'){
- when{
- anyOf {
- branch 'master'; branch 'sonarqube'
- }
- }
- steps{
- container ('maven') {
- sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
- sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:latest '
- }
- }
- }
- stage('deploy to dev') {
- when{
- anyOf {
- branch 'master'; branch 'sonarqube'
- }
- }
- steps {
- input(id: 'deploy-to-dev', message: 'deploy to dev?')
- container("maven") {
- withCredentials([
- kubeconfigFile(
- credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
- variable: 'KUBECONFIG')
- ]) {
- sh '''
- envsubst < deploy/dev-ol/devops-sample-svc.yaml | kubectl apply -f -
- envsubst < deploy/dev-ol/devops-sample.yaml | kubectl apply -f -
- '''
- }
- }
- }
- }
- stage('push with tag'){
- when{
- expression{
- return params.TAG_NAME =~ /v.*/
- }
- }
- steps {
- container ('maven') {
- input(id: 'release-image-with-tag', message: 'release image with tag?')
- withCredentials([usernamePassword(credentialsId: "$GITHUB_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
- sh 'git config --global user.email "kubesphere@yunify.com" '
- sh 'git config --global user.name "kubesphere" '
- sh 'git tag -a $TAG_NAME -m "$TAG_NAME" '
- sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@gitlab.yinbin.ink:8/$GITHUB_ACCOUNT/devops-java-sample.git --tags --ipv4'
- }
- sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG_NAME '
- sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG_NAME '
- }
- }
- }
- stage('deploy to production') {
- when{
- expression{
- return params.TAG_NAME =~ /v.*/
- }
- }
- steps {
- input(id: 'deploy-to-production', message: 'deploy to production?')
- container("maven") {
- withCredentials([
- kubeconfigFile(
- credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
- variable: 'KUBECONFIG')
- ]) {
- sh '''
- envsubst < deploy/prod-ol/devops-sample-svc.yaml | kubectl apply -f -
- envsubst < deploy/prod-ol/devops-sample.yaml | kubectl apply -f -
- '''
- }
- }
- }
- }
- }
- }
|