1+ #! /usr/bin/env groovy
12/*
23Copyright 2018 Google LLC
34
@@ -19,109 +20,87 @@ limitations under the License.
1920// define containerTemplate but that has been deprecated in favor of the yaml
2021// format
2122// Reference: https://github.com/jenkinsci/kubernetes-plugin
22- pipeline {
23- agent {
24- kubernetes {
25- label ' k8s-infra'
26- defaultContainer ' jnlp'
27- yaml """
23+
24+ // set up pod label and GOOGLE_APPLICATION_CREDENTIALS (for Terraform)
25+ def label = " k8s-infra"
26+ def containerName = " k8s-node"
27+ def GOOGLE_APPLICATION_CREDENTIALS = ' /home/jenkins/dev/jenkins-deploy-dev-infra.json'
28+
29+ podTemplate(label : label, yaml : """
2830apiVersion: v1
2931kind: Pod
3032metadata:
3133 labels:
3234 jenkins: build-node
3335spec:
3436 containers:
35- - name: k8s-node
36- image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:1.2.0
37- imagePullPolicy: Always
38- command:
39- - cat
37+ - name: ${ containerName}
38+ image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${ env.CONTAINER_VERSION}
39+ command: ['cat']
4040 tty: true
4141 volumeMounts:
42- # Mount the docker.sock file so we can communicate wth the local docker
43- # daemon
44- - name: docker-sock-volume
45- mountPath: /var/run/docker.sock
46- # Mount the local docker binary
47- - name: docker-bin-volume
48- mountPath: /usr/bin/docker
4942 # Mount the dev service account key
5043 - name: dev-key
5144 mountPath: /home/jenkins/dev
5245 volumes:
53- - name: docker-sock-volume
54- hostPath:
55- path: /var/run/docker.sock
56- - name: docker-bin-volume
57- hostPath:
58- path: /usr/bin/docker
5946 # Create a volume that contains the dev json key that was saved as a secret
6047 - name: dev-key
6148 secret:
6249 secretName: jenkins-deploy-dev-infra
6350"""
64- }
65- }
66-
67- environment {
68- GOOGLE_APPLICATION_CREDENTIALS = ' /home/jenkins/dev/jenkins-deploy-dev-infra.json'
69- }
51+ ) {
52+ node(label) {
53+ try {
54+ // Options covers all other job properties or wrapper functions that apply to entire Pipeline.
55+ properties([disableConcurrentBuilds()])
56+ // set env variable GOOGLE_APPLICATION_CREDENTIALS for Terraform
57+ env. GOOGLE_APPLICATION_CREDENTIALS = GOOGLE_APPLICATION_CREDENTIALS
7058
59+ stage(' Setup' ) {
60+ container(containerName) {
61+ // checkout code from scm i.e. commits related to the PR
62+ checkout scm
7163
72- stages {
73- stage(' Setup access' ) {
74- steps {
75- container(' k8s-node' ) {
76- script {
77- // env.CLUSTER_ZONE will need to be updated to match the
78- // ZONE in the jenkins.propeties file
79- env. CLUSTER_ZONE = " ${ CLUSTER_ZONE} "
80- // env.PROJECT_ID will need to be updated to match your GCP
81- // development project id
82- env. PROJECT_ID = " ${ PROJECT_ID} "
83- env. REGION = " ${ REGION} "
84- }
8564 // Setup gcloud service account access
86- sh " gcloud auth activate-service-account --key-file=${ env. GOOGLE_APPLICATION_CREDENTIALS} "
65+ sh " gcloud auth activate-service-account --key-file=${ GOOGLE_APPLICATION_CREDENTIALS} "
8766 sh " gcloud config set compute/zone ${ env.CLUSTER_ZONE} "
8867 sh " gcloud config set core/project ${ env.PROJECT_ID} "
8968 sh " gcloud config set compute/region ${ env.REGION} "
9069 }
91- }
9270 }
93-
94- stage(' Code linting' ) {
95- steps {
96- container(' k8s-node' ) {
97- sh " make linting"
98- }
71+ stage(' Lint' ) {
72+ container(containerName) {
73+ sh " make lint"
9974 }
10075 }
10176
10277 stage(' Create' ) {
103- steps {
104- container(' k8s-node' ) {
78+ container(containerName) {
10579 sh " make create"
10680 }
107- }
10881 }
10982
11083 stage(' Validate' ) {
111- steps {
112- container(' k8s-node' ) {
84+ container(containerName) {
11385 sh " make validate"
11486 }
115- }
11687 }
117- }
11888
119- post {
120- always {
121- container(' k8s-node' ) {
122- sh ' make teardown'
123- sh ' gcloud auth revoke'
89+ }
90+ catch (err) {
91+ // if any exception occurs, mark the build as failed
92+ // and display a detailed message on the Jenkins console output
93+ currentBuild. result = ' FAILURE'
94+ echo " FAILURE caught echo ${ err} "
95+ throw err
96+ }
97+ finally {
98+ stage(' Teardown' ) {
99+ container(containerName) {
100+ sh " make teardown"
101+ sh " gcloud auth revoke"
124102 }
125- }
103+ }
104+ }
126105 }
127- }
106+ }
0 commit comments