Skip to content

Instantly share code, notes, and snippets.

@rafaeltuelho
Last active February 27, 2023 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafaeltuelho/ddbaecc1ed8c8cf8aa5303b65f6f08fd to your computer and use it in GitHub Desktop.
Save rafaeltuelho/ddbaecc1ed8c8cf8aa5303b65f6f08fd to your computer and use it in GitHub Desktop.
Sample Jenkins Pipeline for Openshift CI/CD demos
def version, mvnCmd = "mvn -s configuration/cicd-settings-nexus3.xml"
pipeline {
agent {
label 'maven'
}
stages {
stage('Build App') {
steps {
// if your Git server uses an TLS self signed cetificate ...
// sh "git config --global http.sslVerify false"
git branch: 'eap-7',
url: 'http://gogs:3000/gogs/openshift-tasks.git'
//if your git server requires authentication, create a credential on your Jenkins Server and references it here
// credentialsId: 'myGitCredentialId'
script {
def pom = readMavenPom file: 'pom.xml'
version = pom.version
}
sh "${mvnCmd} install -DskipTests=true"
}
}
stage('Test') {
steps {
sh "${mvnCmd} test"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
stage('Code Analysis') {
steps {
script {
sh "${mvnCmd} sonar:sonar -Dsonar.host.url=http://sonarqube:9000 -DskipTests=true"
}
}
}
stage('Archive App') {
steps {
sh "${mvnCmd} deploy -DskipTests=true -P nexus3"
}
}
stage('Create Image Builder') {
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector("bc", "tasks").exists();
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.newBuild("--name=tasks", "--image-stream=jboss-eap70-openshift:1.5", "--binary=true")
}
}
}
}
}
stage('Build Image') {
steps {
sh "rm -rf oc-build && mkdir -p oc-build/deployments"
sh "cp target/openshift-tasks.war oc-build/deployments/ROOT.war"
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("bc", "tasks").startBuild("--from-dir=oc-build", "--wait=true")
}
}
}
}
}
stage('Create DEV') {
when {
expression {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
return !openshift.selector('dc', 'tasks').exists()
}
}
}
}
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
def app = openshift.newApp("tasks:latest")
app.narrow("svc").expose();
openshift.set("probe dc/tasks --readiness --get-url=http://:8080/ws/demo/healthcheck --initial-delay-seconds=30 --failure-threshold=10 --period-seconds=10")
openshift.set("probe dc/tasks --liveness --get-url=http://:8080/ws/demo/healthcheck --initial-delay-seconds=180 --failure-threshold=10 --period-seconds=10")
def dc = openshift.selector("dc", "tasks")
while (dc.object().spec.replicas != dc.object().status.availableReplicas) {
sleep 10
}
openshift.set("triggers", "dc/tasks", "--manual")
}
}
}
}
}
stage('Deploy DEV') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.DEV_PROJECT) {
openshift.selector("dc", "tasks").rollout().latest();
}
}
}
}
}
stage('Promote to STAGE?') {
steps {
timeout(time:15, unit:'MINUTES') {
input message: "Promote to STAGE?", ok: "Promote"
}
script {
openshift.withCluster() {
openshift.tag("${env.DEV_PROJECT}/tasks:latest", "${env.STAGE_PROJECT}/tasks:${version}")
}
}
}
}
stage('Deploy STAGE') {
steps {
script {
openshift.withCluster() {
openshift.withProject(env.STAGE_PROJECT) {
if (openshift.selector('dc', 'tasks').exists()) {
openshift.selector('dc', 'tasks').delete()
openshift.selector('svc', 'tasks').delete()
openshift.selector('route', 'tasks').delete()
}
openshift.newApp("tasks:${version}").narrow("svc").expose()
openshift.set("probe dc/tasks --readiness --get-url=http://:8080/ws/demo/healthcheck --initial-delay-seconds=30 --failure-threshold=10 --period-seconds=10")
openshift.set("probe dc/tasks --liveness --get-url=http://:8080/ws/demo/healthcheck --initial-delay-seconds=180 --failure-threshold=10 --period-seconds=10")
}
}
}
}
}
}
}
@rafaeltuelho
Copy link
Author

rafaeltuelho commented Sep 25, 2018

sample maven settings.xml

<settings>
  <servers>
     <server>
        <id>nexus3</id>
        <username>admin</username>
        <password>admin123</password>
     </server>
  </servers>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus:8081/repository/maven-all-public/</url>
    </mirror>
  </mirrors>
</settings>

@rafaeltuelho
Copy link
Author

Sample Maven profile to deploy artifacts on nexus

		<profile>
			<id>nexus3</id>
			<distributionManagement>
				<repository>
					<id>nexus3</id>
					<name>Releases</name>
					<url>http://nexus:8081/repository/maven-releases</url>
				</repository>
				<snapshotRepository>
					<id>nexus3</id>
					<name>Snapshot</name>
					<url>http://nexus:8081/repository/maven-snapshots</url>
				</snapshotRepository>
			</distributionManagement>
		</profile>

@rafaeltuelho
Copy link
Author

rafaeltuelho commented Sep 25, 2018

Sample Pipeline BuildConfig

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  annotations:
    pipeline.alpha.openshift.io/uses: '[{"name": "jenkins", "namespace": "", "kind": "DeploymentConfig"}]'
  name: app-pipeline
spec:
  runPolicy: Serial
  source:
    type: None
  strategy:
    jenkinsPipelineStrategy:
      env:
        - name: DEV_PROJECT
          value: dev-app
        - name: STAGE_PROJECT
          value: stage-app
      jenkinsfile: |-
        ...
        PUT the JENKSFILE CONTENT HERE
        ...
    type: JenkinsPipeline
  triggers:
    - github:
        secret: someSecret
      type: GitHub
    - generic:
        secret: someSecret
      type: Generic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment