diff --git a/Jenkinsfile b/Jenkinsfile index d8f390c5..156f2c86 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,26 +1,50 @@ -#!/usr/bin/env groovy +node { + def project = 'REPLACE_WITH_YOUR_PROJECT_ID' + def appName = 'gceme' + def feSvcName = "${appName}-frontend" + def imageTag = "gcr.io/${project}/${appName}:${env.BRANCH_NAME}.${env.BUILD_NUMBER}" -pipeline { + checkout scm - agent { - docker { - image 'node' - args '-u root' - } - } + stage 'Build image' + sh("docker build -t ${imageTag} .") - stages { - stage('Build') { - steps { - echo 'Building...' - sh 'npm install' - } - } - stage('Test') { - steps { - echo 'Testing...' - sh 'npm test' - } - } - } + stage 'Run Go tests' + sh("docker run ${imageTag} go test") + + stage 'Push image to registry' + sh("gcloud docker -- push ${imageTag}") + + stage "Deploy Application" + switch (env.BRANCH_NAME) { + // Roll out to canary environment + case "canary": + // Change deployed image in canary to the one we just built + sh("sed -i.bak 's#gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/canary/*.yaml") + sh("kubectl --namespace=production apply -f k8s/services/") + sh("kubectl --namespace=production apply -f k8s/canary/") + sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}") + break + + // Roll out to production + case "master": + // Change deployed image in canary to the one we just built + sh("sed -i.bak 's#gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/production/*.yaml") + sh("kubectl --namespace=production apply -f k8s/services/") + sh("kubectl --namespace=production apply -f k8s/production/") + sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}") + break + + // Roll out a dev environment + default: + // Create namespace if it doesn't exist + sh("kubectl get ns ${env.BRANCH_NAME} || kubectl create ns ${env.BRANCH_NAME}") + // Don't use public load balancing for development branches + sh("sed -i.bak 's#LoadBalancer#ClusterIP#' ./k8s/services/frontend.yaml") + sh("sed -i.bak 's#gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/dev/*.yaml") + sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/services/") + sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/dev/") + echo 'To access your environment run `kubectl proxy`' + echo "Then access your service via http://localhost:8001/api/v1/proxy/namespaces/${env.BRANCH_NAME}/services/${feSvcName}:80/" + } } diff --git a/app.js b/app.js index d470d3bf..d7078222 100644 --- a/app.js +++ b/app.js @@ -15,7 +15,7 @@ Server.route({ } }); -// don't start server if this file was required +// don't start server if this file was required YESSSSS if (!module.parent) {