From d26619a9f7c6379ee791053aacdebf08c1acf713 Mon Sep 17 00:00:00 2001
From: Student User <student@workstation.lab.example.com>
Date: Tue, 07 Jul 2020 15:44:36 +0200
Subject: [PATCH] sample app and pipeline

---
 Jenkinsfile  |   92 ++++++++++++++++++++++++++++++++++++++++++++++
 app.js       |   11 +++++
 package.json |   14 +++++++
 3 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..e353c78
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,92 @@
+pipeline {
+    environment {
+        DOMAIN='apps.ocp4.example.com'
+        PRJ="hello-${env.BRANCH_NAME}-${env.BUILD_NUMBER}"
+        APP='nodeapp'
+    }
+    agent {
+      node {
+        label 'nodejs'
+      }
+    }
+    stages {
+        stage('create') {
+            steps {
+                script {
+                    // Uncomment to get lots of debugging output
+                    //openshift.logLevel(1)
+                    openshift.withCluster() {
+                        echo("Create project ${env.PRJ}") 
+                        openshift.newProject("${env.PRJ}")
+                        openshift.withProject("${env.PRJ}") {
+                            echo('Grant to developer read access to the project')
+                            openshift.raw('policy', 'add-role-to-user', 'view', 'developer')
+                            echo("Create app ${env.APP}") 
+                            openshift.newApp("${env.GIT_URL}#${env.BRANCH_NAME}", "--strategy source", "--name ${env.APP}")
+                        }
+                    }
+                }
+            }
+        }
+        stage('build') {
+            steps {
+                script {
+                    openshift.withCluster() {
+                        openshift.withProject("${env.PRJ}") {
+                            def bc = openshift.selector('bc', "${env.APP}")
+                            echo("Wait for build from bc ${env.APP} to finish") 
+                            timeout(5) {
+                                def builds = bc.related('builds').untilEach(1) {
+                                    def phase = it.object().status.phase
+                                    if (phase == "Failed" || phase == "Error" || phase == "Cancelled") {
+                                        error 'OpenShift build failed or was cancelled'
+                                    }
+                                    return (phase == "Complete")
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        stage('deploy') {
+            steps {
+                script {
+                    openshift.withCluster() {
+                        openshift.withProject("${env.PRJ}") {
+                            echo("Expose route for service ${env.APP}") 
+                            // Default Jenkins settings to not allow to query properties of an object
+                            // So we cannot query the widlcard domain of the ingress controller
+                            // Nor the auto genereted host of a route
+                            openshift.expose("svc/${env.APP}", "--hostname ${env.PRJ}.${env.DOMAIN}")
+                            echo("Wait for deployment from dc ${env.APP} to finish") 
+                            timeout(5) {
+                                openshift.selector('dc', "${env.APP}").rollout().status()
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        stage('test') {
+            input {
+                message 'About to test the application'
+                ok 'Ok'
+            }
+            steps {
+                echo "Check that '${env.PRJ}.${env.DOMAIN}' returns HTTP 200"
+                sh "curl -s --fail ${env.PRJ}.${env.DOMAIN}"
+            }
+        }
+    }
+    post {
+        always {
+            script {
+                openshift.withCluster() {
+                    echo("Delete project ${env.PRJ}") 
+                    openshift.delete("project/${env.PRJ}")
+                }
+            }
+        }
+    }
+}
diff --git a/app.js b/app.js
new file mode 100644
index 0000000..cc895d4
--- /dev/null
+++ b/app.js
@@ -0,0 +1,11 @@
+var express = require('express');
+app = express();
+
+app.get('/', function (req, res) {
+  res.send('Hello wonderful world!\n');
+});
+
+app.listen(8080, function () {
+  console.log('Example app listening on port 8080!');
+});
+
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..29b13f4
--- /dev/null
+++ b/package.json
@@ -0,0 +1,14 @@
+{
+  "name": "nodejs-helloworld",
+  "version": "1.0.0",
+  "description": "Hello World!",
+  "main": "app.js",
+  "scripts": {
+    "start": "node app.js"
+  },
+  "author": "Red Hat Training",
+  "license": "ASL",
+  "dependencies": {
+    "express": "4.14.x"
+  }
+}

--
Gitblit v1.9.3