Jenkins Pipeline in Openshift - v1
Jenkins Pipeline in Openshift - v1
Jenkins Pipeline in Openshift - v1
Prerequisites:
Step 2: Start Jenkins inside openshift( openshift has Jenkins application within it).
a. Select Jenkins ephemeral and give a password of your choice and click create.
Click Create
Continue to Overview
b. You can see Jenkins pod building. Wait for a few minutes for the pod to
complete.
c. Use the route(http link – top right corner) to access the jenkins dashboard).
Username: admin
Password: <your apssword>
Jenkins by default is linked to Openshift, so we can deal with pipeline via Jenkins and also via
Openshift.
Pipeline yaml
apiVersion: v1
kind: BuildConfig
metadata:
name: myfirstpipeline
labels:
name: myfirstpipeline
annotations:
pipeline.alpha.openshift.io/uses: '[{"name": "mong", "namespace": "", "kind":
"DeploymentConfig"}]'
spec:
triggers:
-
type: GitHub
github:
secret: secret101
-
type: Generic
generic:
secret: secret101
runPolicy: Serial
source:
type: None
strategy:
type: JenkinsPipeline
jenkinsPipelineStrategy:
jenkinsfile: "node() {\nstage 'build'\nopenshiftBuild(buildConfig: 'mong', showBuildLogs: 'true')\
nstage 'deploy'\nopenshiftDeploy(deploymentConfig: 'mong')\nopenshiftScale(deploymentConfig:
'mong',replicaCount: '2')\n}"
output:
resources:
postCommit:
4. This pipeline is also visible in the Jenkins. Switch to Jenkins Dashboard and
refresh , you can see the pipeline.
5. In Openshift, the pipeline section only has the build and deploy to Openshift
part. Choose the pipeline that you created.
6. We can edit the pipeline using ActionsEdit
node() {
stage 'build'
openshiftBuild(buildConfig: 'mong', showBuildLogs: 'true')
stage 'deploy'
openshiftDeploy(deploymentConfig: 'mong')
openshiftScale(deploymentConfig: 'mong',replicaCount: '2')
}
Pipeline section with Build and deploy to openshift
7. In the above code “mong” refers to the name of the application. So , Jenkins
will deploy and build application with name “mong” inside openshift.
8. We need to add other pipeline tasks like Junit testing, sonarqube analysis and
Cobertura coverage. Just add these using the pipeline code as below.
Replace the existing Jenkins file with the following code.
node('maven') {
git url: 'https://github.com/babluchandran/cobertura'
stage 'sonar'
sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dsonar.host.url=http://sonarqube-stage.cloudapps-
b7a2.oslab.opentlc.com/ -Dsonar.login= -Dsonar.projectName=mong -Dsonar.projectVersion=1 -
Dsonar.projectKey=xxx -Dsonar.sources=. -DskipTests=true"
stage 'test'
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
stage 'build'
openshiftBuild(buildConfig: 'mong', showBuildLogs: 'true')
//stage 'buildtwo'
//sh "${mvnHome}/bin/mvn clean install -DskipTests=true"
stage 'coverage'
step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile:
'**/target/site/cobertura/coverage.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0,
onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])
//sh "${mvnHome}/bin/mvn jacoco:report sonar:sonar -Dsonar.host.url=http://sonarqube-stage.cloudapps-
b7a2.oslab.opentlc.com -DskipTests=true"
stage 'deploy'
input message: "Promote to DEPLOY?", ok: "Promote"
openshiftDeploy(deploymentConfig: 'mong')
openshiftScale(deploymentConfig: 'mong',replicaCount: '2')
}
9. The git url in this code refers to the repository of our “mong” application. In
my case I am using github as the repository.
Click Save.
10. Now we have to add the necessary plugins in Jenkins, this includes
SonarQube , Cobertura, Maven, junit etc..
11. In Jenkins Dashboard,
Go to Jenkinsmanage JenkinsManage PluginsAvailable and select all
the necessary plugins mentioned above and click Install without restart.
SonarQube:
Configure SonarQube, “SonarQubeScanner” is the name mentioned in the yaml in sonar
stage section and save.
13. We now have to have the SonarQube server in Openshift. Install SonarQube
using the commands in Openshift master machine.
oc new-app postgresql-ephemeral \
-p POSTGRESQL_USER=sonar,POSTGRESQL_PASSWORD=sonar,POSTGRESQL_DATABASE=sonar
oc new-app docker.io/openshiftdemos/sonarqube:6.0 \
-e
SONARQUBE_JDBC_USERNAME=sonar,SONARQUBE_JDBC_PASSWORD=sonar,SONARQUBE_JDBC_U
RL=jdbc:postgresql://postgresql/sonar
This will run a new pod in openshift for SonarQube.
This will start a new build in openshift. View log will redirect us to the Jenkins log dashboard.