Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit 27e573d

Browse files
authored
Merge pull request #12 from pydevops/dev
use scripted Jenkinsfile to solve version bump issue
2 parents 8d536f8 + db0f929 commit 27e573d

File tree

2 files changed

+45
-66
lines changed

2 files changed

+45
-66
lines changed

Jenkinsfile

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env groovy
12
/*
23
Copyright 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: """
2830
apiVersion: v1
2931
kind: Pod
3032
metadata:
3133
labels:
3234
jenkins: build-node
3335
spec:
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+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
SHELL := /usr/bin/env bash
1717

1818
# All is the first target in the file so it will get picked up when you just run 'make' on its own
19-
linting: check_shell check_python check_golang check_terraform check_docker check_base_files check_headers check_trailing_whitespace
19+
lint: check_shell check_python check_golang check_terraform check_docker check_base_files check_headers check_trailing_whitespace
2020

2121
# The .PHONY directive tells make that this isn't a real target and so
2222
# the presence of a file named 'check_shell' won't cause this target to stop

0 commit comments

Comments
 (0)