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

Commit

Permalink
Merge pull request #26 from GoogleCloudPlatform/Jenkinsfile
Browse files Browse the repository at this point in the history
Jenkinsfile formatting and pod template
  • Loading branch information
Chris Love committed Aug 13, 2019
2 parents a351bcd + b29ef6b commit a188a4d
Showing 1 changed file with 63 additions and 81 deletions.
144 changes: 63 additions & 81 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env groovy

/*
Copyright 2018 Google LLC
Expand All @@ -13,94 +14,75 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// The declarative agent is defined in yaml. It was previously possible to
// define containerTemplate but that has been deprecated in favor of the yaml
// format
// Reference: https://github.com/jenkinsci/kubernetes-plugin

// set up pod label and GOOGLE_APPLICATION_CREDENTIALS (for Terraform)
def label = "k8s-infra"
def containerName = "k8s-node"
def GOOGLE_APPLICATION_CREDENTIALS = '/home/jenkins/dev/jenkins-deploy-dev-infra.json'

podTemplate(label: label, yaml: """
apiVersion: v1
kind: Pod
metadata:
labels:
jenkins: build-node
spec:
containers:
- name: ${containerName}
image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${env.CONTAINER_VERSION}
command: ['cat']
tty: true
volumeMounts:
# Mount the dev service account key
- name: dev-key
mountPath: /home/jenkins/dev
volumes:
# Create a volume that contains the dev json key that was saved as a secret
- name: dev-key
secret:
secretName: jenkins-deploy-dev-infra
"""
) {
node(label) {
try {
// Options covers all other job properties or wrapper functions that apply to entire Pipeline.
properties([disableConcurrentBuilds()])
// set env variable GOOGLE_APPLICATION_CREDENTIALS for Terraform
env.GOOGLE_APPLICATION_CREDENTIALS=GOOGLE_APPLICATION_CREDENTIALS

stage('Setup') {
container(containerName) {
// checkout code from scm i.e. commits related to the PR
checkout scm
def containerName = "cloud-sql-postgres"
def GOOGLE_APPLICATION_CREDENTIALS = '/home/jenkins/dev/jenkins-deploy-dev-infra.json'
def jenkins_container_version = env.JENKINS_CONTAINER_VERSION

// Setup gcloud service account access
sh "gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS}"
sh "gcloud config set compute/zone ${env.CLUSTER_ZONE}"
sh "gcloud config set core/project ${env.PROJECT_ID}"
sh "gcloud config set compute/region ${env.REGION}"
}
}
stage('Lint') {
container(containerName) {
sh "make lint"
}
}
podTemplate(
containers: [
containerTemplate(name: "${containerName}",
image: "gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${jenkins_container_version}",
command: 'tail -f /dev/null',
resourceRequestCpu: '1000m',
resourceLimitCpu: '2000m',
resourceRequestMemory: '1Gi',
resourceLimitMemory: '2Gi'
)
],
volumes: [secretVolume(mountPath: '/home/jenkins/dev',
secretName: 'jenkins-deploy-dev-infra'
)]
) {
node(POD_LABEL) {
try {
// Options covers all other job properties or wrapper functions that apply to entire Pipeline.
properties([disableConcurrentBuilds()])
// set env variable GOOGLE_APPLICATION_CREDENTIALS for Terraform
env.GOOGLE_APPLICATION_CREDENTIALS = GOOGLE_APPLICATION_CREDENTIALS

stage('Create') {
container(containerName) {
sh "make create"
}
}
stage('Setup') {
container(containerName) {
// checkout code from scm i.e. commits related to the PR
checkout scm

stage('Validate') {
container(containerName) {
sh "make validate"
// Setup gcloud service account access
sh "gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS}"
sh "gcloud config set compute/zone ${env.CLUSTER_ZONE}"
sh "gcloud config set core/project ${env.PROJECT_ID}"
sh "gcloud config set compute/region ${env.REGION}"
}
}
stage('Lint') {
container(containerName) {
sh "make lint"
}
}
stage('Create') {
container(containerName) {
sh "make create"
}
}
stage('Validate') {
container(containerName) {
sh "make validate"
}
}
} catch (err) {
// if any exception occurs, mark the build as failed
// and display a detailed message on the Jenkins console output
currentBuild.result = 'FAILURE'
echo "FAILURE caught echo ${err}"
throw err
} finally {
stage('Teardown') {
container(containerName) {
sh "make teardown"
}
}
}
}

}
catch (err) {
// if any exception occurs, mark the build as failed
// and display a detailed message on the Jenkins console output
currentBuild.result = 'FAILURE'
echo "FAILURE caught echo ${err}"
throw err
}
finally {
stage('Teardown') {
container(containerName) {
sh "make teardown"
sh "gcloud auth revoke"
}
}
}
}
}

0 comments on commit a188a4d

Please sign in to comment.