diff --git a/Jenkinsfile-online b/Jenkinsfile-online index 67c8a7f20..9f6ce9953 100644 --- a/Jenkinsfile-online +++ b/Jenkinsfile-online @@ -10,14 +10,13 @@ pipeline { } environment { - DOCKER_CREDENTIAL_ID = 'dockerhub-id' - GITHUB_CREDENTIAL_ID = 'github-id' - KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig' + DOCKER_CREDENTIAL_ID = 'ray-docker-hub-id' + GITHUB_CREDENTIAL_ID = 'ray-github-id' + KUBECONFIG_CREDENTIAL_ID = 'ks-config' REGISTRY = 'docker.io' - DOCKERHUB_NAMESPACE = 'docker_username' - GITHUB_ACCOUNT = 'kubesphere' - APP_NAME = 'devops-java-sample' - SONAR_CREDENTIAL_ID= 'sonar-token' + DOCKERHUB_NAMESPACE = 'rayzhou' + GITHUB_ACCOUNT = 'rayzhou2017' + APP_NAME = 'ray-devops-java-sample' } stages { @@ -35,21 +34,6 @@ pipeline { } } - stage('sonarqube analysis') { - steps { - container ('maven') { - withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) { - withSonarQubeEnv('sonar') { - sh "mvn sonar:sonar -o -gs `pwd`/configuration/settings.xml -Dsonar.branch=$BRANCH_NAME -Dsonar.login=$SONAR_TOKEN" - } - } - timeout(time: 1, unit: 'HOURS') { - waitForQualityGate abortPipeline: true - } - } - } - } - stage ('build & push') { steps { container ('maven') { diff --git a/Jenkinsfile-online-sq b/Jenkinsfile-online-sq new file mode 100644 index 000000000..51d3ba3d1 --- /dev/null +++ b/Jenkinsfile-online-sq @@ -0,0 +1,119 @@ +pipeline { + agent { + node { + label 'maven' + } + } + + parameters { + string(name:'TAG_NAME',defaultValue: '',description:'') + } + + environment { + DOCKER_CREDENTIAL_ID = 'ray-docker-hub-id' + GITHUB_CREDENTIAL_ID = 'ray-github-id' + KUBECONFIG_CREDENTIAL_ID = 'ks-config' + REGISTRY = 'docker.io' + DOCKERHUB_NAMESPACE = 'rayzhou' + GITHUB_ACCOUNT = 'rayzhou2017' + APP_NAME = 'ray-devops-java-sample' + SONAR_CREDENTIAL_ID= 'sonar-token' + } + + stages { + stage ('checkout scm') { + steps { + checkout(scm) + } + } + + stage ('unit test') { + steps { + container ('maven') { + sh 'mvn clean -o -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 -o -gs `pwd`/configuration/settings.xml -Dsonar.branch=$BRANCH_NAME -Dsonar.login=$SONAR_TOKEN" + } + } + timeout(time: 1, unit: 'HOURS') { + waitForQualityGate abortPipeline: true + } + } + } + } + + stage ('build & push') { + steps { + container ('maven') { + sh 'mvn -o -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package' + 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{ + branch 'master' + } + 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{ + branch 'master' + } + steps { + input(id: 'deploy-to-dev', message: 'deploy to dev?') + kubernetesDeploy(configs: 'deploy/dev-ol/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID") + } + } + 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@github.com/$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?') + kubernetesDeploy(configs: 'deploy/prod-ol/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID") + } + } + } +} diff --git a/src/main/java/io/kubesphere/devops/HelloWorldController.java b/src/main/java/io/kubesphere/devops/HelloWorldController.java index a6bdd4cde..faf471e0d 100644 --- a/src/main/java/io/kubesphere/devops/HelloWorldController.java +++ b/src/main/java/io/kubesphere/devops/HelloWorldController.java @@ -14,6 +14,6 @@ public class HelloWorldController { @RequestMapping("/") public String sayHello() { - return "Really appreaciate your star, that's the power of our life."; + return "Hi! Really appreaciate your star, that's the power of our life."; } } diff --git a/src/test/java/io/kubesphere/devops/HelloWorldControllerTest.java b/src/test/java/io/kubesphere/devops/HelloWorldControllerTest.java index a83b15b54..5ec92d9e2 100644 --- a/src/test/java/io/kubesphere/devops/HelloWorldControllerTest.java +++ b/src/test/java/io/kubesphere/devops/HelloWorldControllerTest.java @@ -8,6 +8,6 @@ public class HelloWorldControllerTest { @Test public void testSayHello() { - assertEquals("Really appreaciate your star, that's the power of our life.", new HelloWorldController().sayHello()); + assertEquals("Hi! Really appreaciate your star, that's the power of our life.", new HelloWorldController().sayHello()); } }