From d0689d7c0d1debece45cd4edcc5a5bffd71a3890 Mon Sep 17 00:00:00 2001 From: Dario Rodriguez Gonzalez Date: Tue, 17 Mar 2020 23:26:37 +0100 Subject: [PATCH] Added from existing template and docs --- devon4j-mts/Jenkinsfile | 34 -- documentation/Home.asciidoc | 2 + documentation/_Sidebar.asciidoc | 2 + documentation/devon4j-mts.asciidoc | 2 +- documentation/devon4j-pl.asciidoc | 2 +- documentation/devon4net.asciidoc | 2 +- documentation/devon4ng-pl.asciidoc | 2 +- documentation/devon4node-pl.asciidoc | 2 +- documentation/from-existing-devonfw.asciidoc | 86 ++++ documentation/master-production-line.asciidoc | 4 + from-existing-devonfw/Jenkinsfile | 370 ++++++++++++++++++ 11 files changed, 469 insertions(+), 39 deletions(-) create mode 100644 documentation/from-existing-devonfw.asciidoc create mode 100644 from-existing-devonfw/Jenkinsfile diff --git a/devon4j-mts/Jenkinsfile b/devon4j-mts/Jenkinsfile index fe363e6c..57446c47 100644 --- a/devon4j-mts/Jenkinsfile +++ b/devon4j-mts/Jenkinsfile @@ -187,17 +187,6 @@ pipeline { jobDsl scriptText: """ pipelineJob("${params.JENKINS_FOLDER}/MyThaiStar_FRONTEND_DEPLOY") { description('Deploy job for MyThaiStar Frontend Only.') - parameters { - credentialsParam('registryCredentialsId') { - type('com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl') - required() - defaultValue('nexus-api') - description('registry credentials') - } - stringParam('registryUrl', 'REPLACE_ME', 'docker registry url') - stringParam('VERSION', '3.2.0', 'Version number') - stringParam('dockerNetwork', 'my-thai-star', 'The docker network for the deployed container') - } definition { cpsScm { scm { @@ -221,17 +210,6 @@ pipeline { jobDsl scriptText: """ pipelineJob("${params.JENKINS_FOLDER}/MyThaiStar_SERVER_DEPLOY") { description('Deploy job for MyThaiStar Backend only.') - parameters { - credentialsParam('registryCredentialsId') { - type('com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl') - required() - defaultValue('nexus-api') - description('registry credentials') - } - stringParam('registryUrl', 'REPLACE_ME', 'docker registry url') - stringParam('VERSION', '3.2.0', 'Version number') - stringParam('dockerNetwork', 'my-thai-star', 'The docker network for the deployed container') - } definition { cpsScm { scm { @@ -255,18 +233,6 @@ pipeline { jobDsl scriptText: """ pipelineJob("${params.JENKINS_FOLDER}/MyThaiStar_REVERSE-PROXY_DEPLOY") { description('Deployment Pipeline for both Frontend and Backend at the same time.') - parameters { - credentialsParam('registryCredentialsId') { - type('com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl') - required() - defaultValue('nexus-api') - description('registry credentials') - } - booleanParam('buildReverseProxy', false, 'Build again the reverse proxy image?') - stringParam('registryUrl', 'REPLACE_ME', 'docker registry url') - stringParam('VERSION', '3.2.0', 'Version number') - stringParam('dockerNetwork', 'my-thai-star', 'The docker network for the deployed container') - } definition { cpsScm { scm { diff --git a/documentation/Home.asciidoc b/documentation/Home.asciidoc index 838a01b7..f98cb793 100644 --- a/documentation/Home.asciidoc +++ b/documentation/Home.asciidoc @@ -24,7 +24,9 @@ This repository contains a collection of templates that can be used inside a Pro - link:how-to-add-a-template.asciidoc[How to add a Template to your PL instance] - link:devon4j-pl.asciidoc[devon4j Template] - link:devon4ng-pl.asciidoc[devon4ng Template] +- link:devon4net-pl.asciidoc[devon4net Template] - link:devon4node-pl.asciidoc[devon4node Template] +- link:from-existing-devonfw.asciidoc[From existing devonfw Template] == Utility Templates diff --git a/documentation/_Sidebar.asciidoc b/documentation/_Sidebar.asciidoc index 46d6db7a..abb8fb09 100644 --- a/documentation/_Sidebar.asciidoc +++ b/documentation/_Sidebar.asciidoc @@ -3,7 +3,9 @@ *** link:how-to-add-a-template.asciidoc[How to add a Template to your PL instance] *** link:devon4j-pl.asciidoc[devon4j Template] *** link:devon4ng-pl.asciidoc[devon4ng Template] +*** link:devon4net-pl.asciidoc[devon4net Template] *** link:devon4node-pl.asciidoc[devon4node Template] +*** link:from-existing-devonfw.asciidoc[From existing devonfw Template] ** Utility Templates *** link:inialize-instance.asciidoc[Initialize Instance Template] *** link:install-sonar-plugin.asciidoc[Install SonarQube Plugin Template] diff --git a/documentation/devon4j-mts.asciidoc b/documentation/devon4j-mts.asciidoc index 982c0117..e32d2381 100644 --- a/documentation/devon4j-mts.asciidoc +++ b/documentation/devon4j-mts.asciidoc @@ -21,7 +21,7 @@ toc::[] Please read all of the following sections carefully. -=== Overview +== Overview This template will configure your PL instance to have a 'ready to use' My-Thai-Star devonfw application. It is only an example. In order to start a new project, please use the other templates. This includes: diff --git a/documentation/devon4j-pl.asciidoc b/documentation/devon4j-pl.asciidoc index 45a99987..14bd7761 100644 --- a/documentation/devon4j-pl.asciidoc +++ b/documentation/devon4j-pl.asciidoc @@ -20,7 +20,7 @@ image::images/devon4j-pl/devonfw.png[400,300] = devon4j Template for Production Line -=== Overview +== Overview This template will configure your PL instance to have a 'ready to use' devon4j template. It can be used as a starting point for your Java projects. + This includes CICD files for a devonfw technology stack with configuration for: diff --git a/documentation/devon4net.asciidoc b/documentation/devon4net.asciidoc index bbdb4d94..7dcdd515 100644 --- a/documentation/devon4net.asciidoc +++ b/documentation/devon4net.asciidoc @@ -20,7 +20,7 @@ image::images/devon4node-pl/devonfw.png[400,300] = devon4net Template for Production Line -=== Overview +== Overview This template will configure your PL instance to have a 'ready to use' devon4net template. It can be used as a starting point for your .NET projects. + This includes CICD files for a devonfw technology stack with configuration for: diff --git a/documentation/devon4ng-pl.asciidoc b/documentation/devon4ng-pl.asciidoc index 99dfaa1e..ff47e0c0 100644 --- a/documentation/devon4ng-pl.asciidoc +++ b/documentation/devon4ng-pl.asciidoc @@ -20,7 +20,7 @@ image::images/devon4ng-pl/devonfw.png[400,300] = devon4ng Template for Production Line -=== Overview +== Overview This template will configure your PL instance to have a 'ready to use' devon4ng template. It can be used as a starting point for your Angular projects. + This includes CICD files for a devonfw technology stack with configuration for: diff --git a/documentation/devon4node-pl.asciidoc b/documentation/devon4node-pl.asciidoc index 799dcdd1..f7152e8d 100644 --- a/documentation/devon4node-pl.asciidoc +++ b/documentation/devon4node-pl.asciidoc @@ -20,7 +20,7 @@ image::images/devon4node-pl/devonfw.png[400,300] = devon4node Template for Production Line -=== Overview +== Overview This template will configure your PL instance to have a 'ready to use' devon4node template. It can be used as a starting point for your Node projects. + This includes CICD files for a devonfw technology stack with configuration for: diff --git a/documentation/from-existing-devonfw.asciidoc b/documentation/from-existing-devonfw.asciidoc new file mode 100644 index 00000000..b475942a --- /dev/null +++ b/documentation/from-existing-devonfw.asciidoc @@ -0,0 +1,86 @@ +:toc: macro + +ifdef::env-github[] +:tip-caption: :bulb: +:note-caption: :information_source: +:important-caption: :heavy_exclamation_mark: +:caution-caption: :fire: +:warning-caption: :warning: +endif::[] + +toc::[] +:idprefix: +:idseparator: - +:reproducible: +:source-highlighter: rouge +:listing-caption: Listing + +image::images/devon4node-pl/pl.png[400,300] +image::images/devon4node-pl/devonfw.png[400,300] + += From existing devonfw Template for Production Line + +== Overview + +From existing devonfw template is very similar to devon4j, devon4ng, devon4net and devon4node templates. The main difference is from existing devonfw template will no create a new devonfw project, it takes an existing project from GitLab and then add/create everything in order to apply a CICD strategy to your project. + +== Prerequisites +To be able to run Jenkins Node job under ProductionLine you need to configure below settings in Jenkins and Gitlab + +* Jenkins + +** Execute the link:./initialize-instance.asciidoc[initialize instance template] +** If you plan to deploy into OpenShift, you need to execute link:./openshift-configuration.asciidoc[openshift-configuration] template also. +* Gitlab + +** Create a project and upload your current code. In order to start a new project in your local machine, you can use the link:https://github.com/devonfw/ide[devonfw-ide]. The project must be a devon4j, devon4ng, devon4net or devon4node project. +** Generate User Private Token + +Go to your Profile in Gitlab + + +image::./images/devon4node-pl/profile.png[500,400] + +Next click on the pen icon + + +image::./images/devon4node-pl/pen.png[500,400] + +On the left menu choose Access Tokens and put token name and check fields like below + + +image::./images/devon4node-pl/token.JPG[600,500] + +Click "Create personal access token", you should receive notification about created token and token string. Copy the token string. + +image::./images/devon4node-pl/created_token.JPG[600,500] + +The GitLab API user needs to have API access and the rights to create a new group. To set this permission follow the next steps: + + +* Enter the Admin control panel +* Select 'Users' +* Select the user(s) in question and click 'Edit' +* Scroll down to 'Access' and un-tick 'Can Create Group' + +== How to insert the Template + +In order to add the template, you can follow the link:./how-to-add-a-template.asciidoc[guide]. + + +== How to run the Template + +* Build the job with parameters: +** REPOSITORY_URL: The internal repository URL. Without protocol. Example: gitlab-core:80/gitlab/mygroup/myproject-frontend. +** GIT_BRANCH: The branch where you want to apply the CICD changes. +** MERGE_STRATEGY: Choose the merge strategy for cicdgen. For more information see the link:https://github.com/devonfw/cicdgen/wiki/merge-strategies[CICDGEN merge documentation page] +** GITLAB_USER_PRIVATE_TOKEN: Private Token of a Production Line Gitlab User that can be used to create/update repositories. The token proprietary user must have admin rights in the repository. Created as prerequisite, you only need to add it as credential with GitLab API token *Kind*. +** DEPLOY: Choose the environment where you want to deploy. The deployment could be *none*, *docker* or *openshift*. If *docker* or *openshift* were selected, extra parameters will be required in their dedicated steps: +*** Configuring DOCKER: +**** DOCKER_URL: The remote docker daemon URL +**** DOCKER_CERT: Credentials to access docker daemon. If the daemon is not secure, you can leave this empty. +*** Configuring Openshift: +**** OC_NAME: Openshift cluster name. It was defined in the Openshift Configuration template +**** DOCKER_REGISTRY_CREDENTIALS: Nexus docker registry user credentials. It was created in the initialize instance pipeline. The default username is nexus-api, the default password is the same as your service account. + +After executing this template, you will have: + +* Your GitLab project updated. +** Added a Jenkinsfile with all CICD stages. +** The repository is updated in order to have the jenkins webhook. +* A new multibranch pipeline in jenkins inside the folder _PROJECT_NAME_ with the name _PROJECT_NAME_-_PROJECT_SUFFIX_. As the webhook is already configured, it should be executed on every push to GitLab repository. +* If you choose docker for deployment, your Jenkinsfile should contain two extra stages in order to build and deploy the docker image. Also, the repository should contain the Dockerfiles to create the docker images. +* If you choose OpenShift for deployment, three new applications should be created in your OpenShift. Those applications represent three environments of your application: develop, uat and stage. Also, your Jenkinsfile should contain three extra stages in order to build and deploy the docker image and check that the pod is running without errors. Also, the repository should contain the Dockerfiles to create the docker images. \ No newline at end of file diff --git a/documentation/master-production-line.asciidoc b/documentation/master-production-line.asciidoc index 0ad48ce1..a85a6493 100644 --- a/documentation/master-production-line.asciidoc +++ b/documentation/master-production-line.asciidoc @@ -29,8 +29,12 @@ include::devon4j-pl[leveloffset=2] include::devon4ng-pl[leveloffset=2] +include::devon4net-pl[leveloffset=2] + include::devon4node-pl[leveloffset=2] +include::from-existing-devonfw[leveloffset=2] + == Utility Templates include::initialize-instance[leveloffset=2] diff --git a/from-existing-devonfw/Jenkinsfile b/from-existing-devonfw/Jenkinsfile new file mode 100644 index 00000000..85288d67 --- /dev/null +++ b/from-existing-devonfw/Jenkinsfile @@ -0,0 +1,370 @@ +@Library('ProductionLineTemplateLib') + +import com.capgemini.productionline.configuration.* + +JenkinsConfiguration jenkinsConfiguration = new JenkinsConfiguration(this); +GitLab gitlabConfiguration; +OpenshiftConfiguration openshiftConfiguration = new OpenshiftConfiguration(this); + +pipeline{ + + agent any + + //Jenkins Job Parameters + parameters { + + // string(name: 'GROUP_ID', defaultValue: 'com.devonfw', description: 'The group id of the project.') + string(name: 'REPOSITORY_URL', defaultValue: 'REPLACE_ME', description: 'The internal repository URL. Without protocol.') + string(name: 'GIT_BRANCH', defaultValue: 'develop', description: 'The branch where you want to apply the CICD changes') + choice choices: ['error', 'keep', 'override', 'combine'], description: 'Choose the merge strategy for cicdgen.', name: 'MERGE_STRATEGY' + credentials(credentialType: 'com.dabsquared.gitlabjenkins.connection.GitLabApiTokenImpl', defaultValue: 'gitlab-api-token', description: 'NEEDS TO BE SET!. Private Token of a Production Line Gitlab User that can be used to create repositories.', name: 'GITLAB_USER_PRIVATE_TOKEN', required: true) + choice choices: ['none', 'docker', 'openshift'], description: 'Choose the environment where you want to deploy', name: 'DEPLOY' + } + + environment { + NODE_VERSION = '12.16.1' + } + + stages { + stage('Clone') { + steps { + script { + dir('project') { + def token = GitLab.gitlabApiToken(GITLAB_USER_PRIVATE_TOKEN); + gitlabConfiguration = new GitLab(this, token, ProductionLineGlobals.GITLAB_BASE_URL); + + FULL_GITLAB_URL = 'http://oauth2:' + token + '@' + REPOSITORY_URL.replace('http://', '').replace('https://', '').replace('.git', '') + '.git' + git branch: params.GIT_BRANCH, url: FULL_GITLAB_URL + } + } + } + } + + stage('Analyze project'){ + steps { + script { + dir('project') { + PROJECT_TYPE = 'error' + def repoParts = REPOSITORY_URL.replace('.git','').split('/') + PROJECT_NAME_FULL = repoParts[-1] + PROJECT_GROUP_NAME = repoParts[-2] + PROJECT_NAME = PROJECT_NAME_FULL.split('-')[0] + if(fileExists('pom.xml')) { + PROJECT_TYPE = 'devon4j' + } else if (fileExists('package.json')) { + def packageContent = readFile 'package.json' + if (packageContent.contains('@angular/core')) { + PROJECT_TYPE = 'devon4ng' + } + + if (packageContent.contains('@nestjs/core')) { + PROJECT_TYPE = 'devon4node' + } + } else if (fileExists('devon4net.sln')) { + PROJECT_TYPE = 'devon4net' + } + + if (PROJECT_TYPE == 'error') { + error "Your repository do not contains any devonfw project." + } + } + } + } + } + + stage('PL Setup'){ + steps { + script { + dir('project') { + if (params.DEPLOY == 'openshift' && jenkinsConfiguration.getLastStableBuild('UTILS/Openshift_Configuration') == null) { + error "In order to deploy using openshift, you need to execute the job UTILS/Openshift_Configuration as prerequisite" + } + + println "Trying to install all required plugins" + //Install the required Jenkins plugins + //The template currently requires the following plugins to be present + def pluginsToInstall = ["ssh-credentials", "custom-tools-plugin", "http_request", "job-dsl", "sonar", "ansible", "pipeline-maven", "nodejs", "gitlab-plugin"] + + if (params.DEPLOY == 'docker') { + pluginsToInstall << "docker-plugin" << "docker-build-step" << "docker-workflow" << "jclouds-jenkins" + } + + if (params.DEPLOY == 'openshift') { + pluginsToInstall << 'openshift-client' + } + + def pluginsHaveBeenInstalled = jenkinsConfiguration.installPlugin(pluginsToInstall) + + if( pluginsHaveBeenInstalled ) { + println "New plugins were installed - Restart" + // We need to restart Jenkins in case new plugins have been installed. + //if set to false, then do a force restart + //still requires to manually start the job again afterwards + jenkinsConfiguration.restartJenkins(false); + error "Restarting Jenkins, please execute pipeline after restart" + } else { + println "No plugins were installed" + } + + //Add nodejs + println "Trying to install nodejs config" + String installName = "NodeJS ${NODE_VERSION}" + String nodeJS_Version = NODE_VERSION + String npmPackages = "yarn" + String home = "" + long npmPackagesRefreshHours = 100 + + nodeJSInstallSuccess = jenkinsConfiguration.addNodeJS_Version(installName, nodeJS_Version, npmPackages, home, npmPackagesRefreshHours) + if (PROJECT_TYPE == 'devon4j') { + jdkInstallSuccess = jenkinsConfiguration.addJdkVersion("OpenJDK11", "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_x64_linux_hotspot_11.0.6_10.tar.gz", "jdk-11.0.6+10") + } + if (PROJECT_TYPE == 'devon4net') { + jenkinsConfiguration.addCustomTool('dotnet', '', """if ! which dotnet > /dev/null; then +wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg +sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/ +wget -q https://packages.microsoft.com/config/debian/10/prod.list +sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list +sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg +sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list +sudo apt-get update +sudo apt-get install -y apt-transport-https +sudo apt-get update +sudo apt-get install -y dotnet-sdk-3.1 +fi""", '/usr/bin') + } + + // Get the Pl Url + env.PL_URL = JENKINS_URL.replace("https://", "").split("/")[0] + env.REGISTRY_URL = 'docker-registry-' + env.PL_URL + + //Add the required script approval + println "Adding required script approvals in Jenkins" + jenkinsConfiguration.approveSignature("method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object") + } + } + } + } + + stage('Configuring DOCKER'){ + when { + beforeInput true + equals expected: 'docker', actual: params.DEPLOY + } + input { + message 'Introduce the docker host' + parameters { + string defaultValue: 'tcp://127.0.0.1:2763', description: 'The remote docker daemon URL', name: 'DOCKER_URL', trim: true + credentials(credentialType: 'org.jenkinsci.plugins.docker.commons.credentials.DockerServerCredentials', description: 'Credentials to access docker daemon', name: 'DOCKER_CERT', required: true) + } + } + steps { + script { + env.DOCKER_URL = DOCKER_URL + env.DOCKER_CERT = DOCKER_CERT + } + } + } + + stage('Configuring Openshift'){ + when { + beforeInput true + equals expected: 'openshift', actual: params.DEPLOY + } + input { + message 'Introduce the openshift host' + parameters { + string defaultValue: 'default', description: 'Openshift configuration name', name: 'OC_NAME', trim: true + credentials(credentialType: 'com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl', defaultValue: 'nexus-api', description: 'Nexus registry integration user credentials', name: 'DOCKER_REGISTRY_CREDENTIALS', required: true) + } + } + tools { + oc 'OpenShiftv3.11.0' + } + steps { + script { + if(!openshiftConfiguration.existsOpenshiftGlobalConfiguration(OC_NAME)) { + error "Cluster configuration called ${OC_NAME} does not exists, please create it. You can use the UTILS/Openshift_Configuration template." + } + env.OC_NAME = OC_NAME + + env.SECRET_NAME = "docker-registry-" + env.PL_URL.split("\\.")[0] + + openshift.withCluster(env.OC_NAME){ + openshift.withProject() { + // Create secret if not exist + def secret = openshift.selector('secret', "${env.SECRET_NAME}") + if (!secret.exists()) { + echo "secret not exist" + withCredentials([usernamePassword(credentialsId: "${DOCKER_REGISTRY_CREDENTIALS}", passwordVariable: 'drpass', usernameVariable: 'druser')]) { + openshift.create('secret', "docker-registry ${env.SECRET_NAME} --docker-server=${env.REGISTRY_URL} --docker-username=${druser} --docker-password=${drpass} --docker-email=no-reply@email.com") + } + } + + if (PROJECT_TYPE == 'devon4node') { + def PORT='3000' + def CPU='100m' + def MEMORY='512Mi' + + sh """wget https://raw.githubusercontent.com/devonfw/devonfw-shop-floor/develop/dsf4openshift/configure-environments/devon4node/devon4node.yaml""" + + // Create environments for dev, uat and prod + openshift.create( openshift.process( '', '-f', 'devon4node.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-dev", "-p", "NODE_TAG=${NODE_VERSION}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=latest", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}", "-p", "NODE_ENV=develop" ) ) + openshift.create( openshift.process( '', '-f', 'devon4node.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-uat", "-p", "NODE_TAG=${NODE_VERSION}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=release", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}", "-p", "NODE_ENV=uat" ) ) + openshift.create( openshift.process( '', '-f', 'devon4node.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-stage", "-p", "NODE_TAG=${NODE_VERSION}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=production", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}", "-p", "NODE_ENV=production" ) ) + } + + if (PROJECT_TYPE == 'devon4j') { + def OPENJDK_TAG='jre-11.0.6_10-alpine' + def PORT='8080' + def CPU='100m' + def MEMORY='600Mi' + + + sh """wget https://raw.githubusercontent.com/devonfw/devonfw-shop-floor/develop/dsf4openshift/configure-environments/devon4j/devon4j.yaml""" + + // Create environments for dev, uat and prod + openshift.create( openshift.process( '', '-f', 'devon4j.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-dev", "-p", "OPENJDK_TAG=${OPENJDK_TAG}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=latest", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4j.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-uat", "-p", "OPENJDK_TAG=${OPENJDK_TAG}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=release", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4j.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-stage", "-p", "OPENJDK_TAG=${OPENJDK_TAG}", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=production", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + } + + if (PROJECT_TYPE == 'devon4net') { + def PORT='8082' + def CPU='100m' + def MEMORY='400Mi' + + sh """wget https://raw.githubusercontent.com/devonfw/devonfw-shop-floor/develop/dsf4openshift/configure-environments/devon4net/devon4net.yaml""" + + // Create environments for dev, uat and prod + openshift.create( openshift.process( '', '-f', 'devon4net.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-dev", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=latest", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4net.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-uat", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=release", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4net.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-stage", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=production", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + + } + + if (PROJECT_TYPE == 'devon4ng') { + def PORT='8080' + def CPU='100m' + def MEMORY='400Mi' + + sh """wget https://raw.githubusercontent.com/devonfw/devonfw-shop-floor/develop/dsf4openshift/configure-environments/devon4ng/devon4ng.yaml""" + + // Create environments for dev, uat and prod + openshift.create( openshift.process( '', '-f', 'devon4ng.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-dev", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=latest", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4ng.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-uat", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=release", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + openshift.create( openshift.process( '', '-f', 'devon4ng.yaml', "-p", "APPLICATION_NAME=${PROJECT_NAME_FULL}", "-p", "APPLICATION_GROUP_NAME=${PROJECT_NAME}", "-p", "APPLICATION_NAME_SUFFIX=-stage", "-p", "DOCKER_IMAGE=${env.REGISTRY_URL}/${PROJECT_NAME_FULL}", "-p", "DOCKER_TAG=production", "-p", "SECRET=${env.SECRET_NAME}", "-p", "CPU=${CPU}", "-p", "MEMORY=${MEMORY}", "-p", "PORT=${PORT}" ) ) + } + } + } + } + } + } + + stage('Generate template job'){ + steps{ + println "Trying to create Jenkins jobs" + script{ + if (!jenkinsConfiguration.existsJob("${PROJECT_NAME}/${PROJECT_NAME_FULL}")) { + //Disable Job Security + println "Disable job dsl security" + jenkinsConfiguration.disableJobDSLScriptSecurity() + + //Prepare folders in Jenkins for the jobs + println "Trying to create folder for jobs" + jobDsl scriptText: """ + folder("${PROJECT_NAME}"){ + description('Jobs for ${PROJECT_NAME} project.') + } + """ + + //Build job + println "Trying to create Build job" + jobDsl scriptText: """ + multibranchPipelineJob("${PROJECT_NAME}/${PROJECT_NAME_FULL}") { + description('Build job for ${PROJECT_NAME_FULL}.') + branchSources { + git { + id('${UUID.randomUUID().toString()}') + remote('${FULL_GITLAB_URL}') + credentialsId('') + includes('master release* develop') + } + } + } + """ + + //Enable Job Security again + println "Enable job dsl security" + jenkinsConfiguration.enableJobDSLScriptSecurity() + } + } + } + } + stage('Install cicdgen') { + tools { + nodejs "NodeJS ${NODE_VERSION}" + } + steps{ + sh 'sudo env "PATH=\$PATH" npm i -g @devonfw/cicdgen@1.6' + } + } + + stage('Create cicdgen template') { + tools { + nodejs "NodeJS ${NODE_VERSION}" + } + steps{ + script{ + dir('project'){ + def dockerString = '' + def ocString = '' + def otherParamsString = '--merge ' + params.MERGE_STRATEGY + ' ' + + if (params.DEPLOY == 'docker'){ + dockerString = "--docker --dockerurl ${env.DOCKER_URL} --registryurl ${env.REGISTRY_URL} " + if (env.DOCKER_CERT) { + dockerString += "--dockercertid ${env.DOCKER_CERT} " + } + } + + if (params.DEPLOY == 'openshift'){ + ocString = "--openshift --ocname ${env.OC_NAME} --registryurl ${env.REGISTRY_URL} " + } + + if (PROJECT_TYPE == 'devon4ng' || PROJECT_TYPE == 'devon4node' || PROJECT_TYPE == 'devon4net') { + otherParamsString += '--groupid com.capgemini ' + + if (PROJECT_TYPE == 'devon4net') { + otherParamsString += "--appname ${PROJECT_NAME_FULL} --appversion 0.0.1 " + } + } + + sh 'git config user.email "jenkins-pl@example.com"' + sh 'git config user.name "Jenkins"' + sh "cicdgen generate ${PROJECT_TYPE} ${ocString}${dockerString}${otherParamsString}" + } + } + } + } + + stage('Pushing template to Gitlab') { + steps{ + script { + dir('project'){ + gitlabConfiguration.createWebhook(PROJECT_GROUP_NAME, PROJECT_NAME_FULL, "http://jenkins-core:8080/jenkins/project/${PROJECT_NAME}/${PROJECT_NAME_FULL}", '') + + sh """git push --set-upstream origin ${params.GIT_BRANCH}""" + } + } + } + } + + } + + post{ + always{ + cleanWs() + deleteDir() + } + } +}