From c861907a2357e5b2852811b04b11b8d2e866f18b Mon Sep 17 00:00:00 2001 From: Kaido Hallik Date: Sat, 18 Feb 2017 02:46:39 +0200 Subject: [PATCH] Separate Docker question for Jenkins pipeline and Gitlab CI --- generators/ci-cd/index.js | 6 +-- generators/ci-cd/prompts.js | 53 ++++++++++++++++------ generators/ci-cd/templates/_.gitlab-ci.yml | 8 ++-- generators/ci-cd/templates/_Jenkinsfile | 14 +++--- generators/ci-cd/templates/_circle.yml | 4 +- 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/generators/ci-cd/index.js b/generators/ci-cd/index.js index a9c597ed1009..4b719e252feb 100644 --- a/generators/ci-cd/index.js +++ b/generators/ci-cd/index.js @@ -41,9 +41,9 @@ module.exports = PipelineGenerator.extend({ insight.trackWithEvent('generator', 'ci-cd'); }, setTemplateVariables: function() { - if (this.abort || this.integrations === undefined) return; - this.gitLabIndent = this.integrations.includes('gitlab') ? ' ' : ''; - this.indent = this.integrations.includes('docker') ? ' ' : ''; + if (this.abort || this.jenkinsIntegrations === undefined) return; + this.gitLabIndent = this.jenkinsIntegrations.includes('gitlab') ? ' ' : ''; + this.indent = this.jenkinsIntegrations.includes('docker') ? ' ' : ''; this.indent += this.gitLabIndent; } }, diff --git a/generators/ci-cd/prompts.js b/generators/ci-cd/prompts.js index fd553da71411..d975662986d7 100644 --- a/generators/ci-cd/prompts.js +++ b/generators/ci-cd/prompts.js @@ -34,44 +34,67 @@ function askPipelines() { function askIntegrations() { if (this.abort || this.pipelines.length === 0) return; var done = this.async(); - var choices = []; - if (this.pipelines.includes('jenkins') || this.pipelines.includes('gitlab')) { - choices.push({name: '[Docker] Perform the build in a docker container', value: 'docker'}); - } + var herokuChoices = []; if (this.pipelines.includes('jenkins')) { - choices.push({name: '[Sonar] Analyze code with Sonar', value: 'sonar'}); - choices.push({name: '[Gitlab] Send build status to Gitlab', value: 'gitlab'}); + herokuChoices.push({name: 'In Jenkins pipeline', value: 'jenkins'}); + } + if (this.pipelines.includes('gitlab')) { + herokuChoices.push({name: 'In Gitlab CI', value: 'gitlab'}); } - if (this.herokuAppName) { - choices.push({name: '[Heroku] Deploy to heroku', value: 'heroku'}); + if (this.pipelines.includes('circle')) { + herokuChoices.push({name: 'In Circle CI', value: 'circle'}); } var prompts = [ { + when: this.pipelines.includes('jenkins'), type: 'checkbox', - name: 'integrations', - message: 'What tasks/integrations do you want to include ?', + name: 'jenkinsIntegrations', + message: 'Jenkins pipeline: what tasks/integrations do you want to include?', default: [], - choices: choices + choices: [ + {name: 'Perform the build in a docker container', value: 'docker'}, + {name: 'Analyze code with Sonar', value: 'sonar'}, + {name: 'Send build status to Gitlab', value: 'gitlab'} + ] }, { - when: response => this.pipelines.includes('jenkins') && response.integrations.includes('sonar'), + when: response => this.pipelines.includes('jenkins') && response.jenkinsIntegrations.includes('sonar'), type: 'input', name: 'jenkinsSonarName', message: 'What is the name of the Sonar server ?', default: 'Sonar' }, { - when: response => response.integrations.includes('heroku'), + when: this.pipelines.includes('gitlab'), + type: 'confirm', + name: 'gitlabUseDocker', + message: 'In Gitlab CI perform the build in a docker container (hint: gitlab.com uses docker container)?', + default: false + }, + { + when: (this.pipelines.includes('jenkins') || this.pipelines.includes('gitlab') || this.pipelines.includes('circle')) && this.herokuAppName, + type: 'checkbox', + name: 'heroku', + message: 'Deploy to heroku?', + default: [], + choices: herokuChoices + }, + { + when: function (response) { + return response.heroku !== undefined && (response.heroku.includes('jenkins') || response.heroku.includes('gitlab') || response.heroku.includes('circle')); + }, type: 'input', name: 'herokuApiKey', - message: 'What is the Heroku API Key ?', + message: 'What is the Heroku API Key?', default: '' }, ]; this.prompt(prompts).then(props => { - this.integrations = props.integrations; + this.jenkinsIntegrations = props.jenkinsIntegrations; this.jenkinsSonarName = props.jenkinsSonarName; + this.gitlabUseDocker = props.gitlabUseDocker; + this.heroku = props.heroku; this.herokuApiKey = props.herokuApiKey; done(); }); diff --git a/generators/ci-cd/templates/_.gitlab-ci.yml b/generators/ci-cd/templates/_.gitlab-ci.yml index 33fbba1e907b..bc835a6cbfec 100644 --- a/generators/ci-cd/templates/_.gitlab-ci.yml +++ b/generators/ci-cd/templates/_.gitlab-ci.yml @@ -1,4 +1,4 @@ -<%_ if (integrations.includes('docker')) { _%> +<%_ if (gitlabUseDocker) { _%> image: openjdk:8 <%_ } _%> @@ -22,7 +22,7 @@ stages: - test - package - release -<%_ if (integrations.includes('heroku')) { _%> +<%_ if (heroku.includes('gitlab')) { _%> - deploy <%_ } _%> @@ -94,7 +94,7 @@ gradle-repackage: artifacts: paths: - build/libs/*.war - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('gitlab')) { _%> gradle-deploy: stage: deploy script: @@ -166,7 +166,7 @@ gatling-test: paths: - target/gatling/* <%_ } _%> - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('gitlab')) { _%> maven-deploy: stage: deploy script: diff --git a/generators/ci-cd/templates/_Jenkinsfile b/generators/ci-cd/templates/_Jenkinsfile index 89ea70d1169b..057ef0f797f1 100644 --- a/generators/ci-cd/templates/_Jenkinsfile +++ b/generators/ci-cd/templates/_Jenkinsfile @@ -5,10 +5,10 @@ node { checkout scm } -<%_ if (integrations.includes('gitlab')) { _%> +<%_ if (jenkinsIntegrations.includes('gitlab')) { _%> gitlabCommitStatus('build') { <%_ } _%> -<%_ if (integrations.includes('docker')) { _%> +<%_ if (jenkinsIntegrations.includes('docker')) { _%> <%= gitLabIndent %> docker.image('openjdk:8').inside('-u root<% if (buildTool === 'maven') { %> -e MAVEN_OPTS="-Duser.home=./"<% } else if (buildTool === 'gradle') { %> -e GRADLE_USER_HOME=.gradle<% } %>') { <%_ } _%> <%= indent %> stage('check java') { @@ -72,7 +72,7 @@ node { <%= indent %> } <%_ } _%> - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('jenkins')) { _%> <%= indent %> stage('package and deploy') { <%= indent %> sh "HEROKU_API_KEY=\"<%= herokuApiKey %>\" ./mvnw com.heroku.sdk:heroku-maven-plugin:1.1.1:deploy-war -DskipTests -Pprod -Dheroku.appName=<%= herokuAppName %>" <%_ } else { _%> @@ -135,14 +135,14 @@ node { <%= indent %> archiveArtifacts artifacts: '**/build/libs/*.war', fingerprint: true <%= indent %> } - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('jenkins')) { _%> <%= indent %> stage('deployment') { <%= indent %> sh "HEROKU_API_KEY=\"<%= herokuApiKey %>\" ./gradlew deployHeroku --no-daemon" <%= indent %> } <%_ } _%> <%_ } _%> -<%_ if (integrations.includes('sonar')) { _%> +<%_ if (jenkinsIntegrations.includes('sonar')) { _%> <%= indent %> stage('quality analysis') { <%= indent %> withSonarQubeEnv('<%= jenkinsSonarName %>') { <%_ if (buildTool === 'maven') { _%> @@ -153,10 +153,10 @@ node { <%= indent %> } <%= indent %> } <%_ } _%> -<%_ if (integrations.includes('docker')) { _%> +<%_ if (jenkinsIntegrations.includes('docker')) { _%> <%= gitLabIndent %> } <%_ } _%> -<%_ if (integrations.includes('gitlab')) { _%> +<%_ if (jenkinsIntegrations.includes('gitlab')) { _%> } <%_ } _%> } diff --git a/generators/ci-cd/templates/_circle.yml b/generators/ci-cd/templates/_circle.yml index a731b8b42cd3..8f3694556273 100644 --- a/generators/ci-cd/templates/_circle.yml +++ b/generators/ci-cd/templates/_circle.yml @@ -62,14 +62,14 @@ test: <%_ } _%> <%_ } _%> <%_ if (buildTool === 'maven') { _%> - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('circle')) { _%> - HEROKU_API_KEY=\"<%= herokuApiKey %>\" ./mvnw com.heroku.sdk:heroku-maven-plugin:1.1.1:deploy-war -DskipTests -Pprod -Dheroku.appName=<%= herokuAppName %> <%_ } else { _%> - ./mvnw package -Pprod -DskipTests <%_ } _%> <%_ } else if (buildTool === 'gradle') { _%> - ./gradlew bootRepackage -Pprod -x test --no-daemon - <%_ if (integrations.includes('heroku')) { _%> + <%_ if (heroku.includes('circle')) { _%> - HEROKU_API_KEY="<%= herokuApiKey %>" ./gradlew deployHeroku --no-daemon <%_ } _%> <%_ } _%>