Skip to content

Instantly share code, notes, and snippets.

@davidshare
Created August 9, 2019 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidshare/af91c77c92dda24f8aad37caf5ba4147 to your computer and use it in GitHub Desktop.
Save davidshare/af91c77c92dda24f8aad37caf5ba4147 to your computer and use it in GitHub Desktop.
Declarative jenkins pipeline
```#!groovy
// define the images and resources to be used in running the pipeline
pipeline {
agent {
kubernetes {
label 'proj-backend-agent'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
component: ci
spec:
containers:
- name: proj-backend-agent
image: ${IMAGE_REGISTRY}/proj-jenkins-backend
resources:
requests:
cpu: "3000m"
memory: "3Gi"
command:
- cat
tty: true
env:
- name: DOCKER_HOST
value: tcp://localhost:2375
- name: proj-database
image: postgres:9.6-alpine
env:
- name: POSTGRES_USER
value: root
- name: POSTGRES_DB
value: proj_test_db
- name: dind
image: docker:18.05-dind
securityContext:
privileged: true
volumeMounts:
- name: dind-storage
mountPath: /var/lib/docker
volumes:
- name: dind-storage
emptyDir: {}
"""
}
}
stages {
// Load the environment variables to be used in the testing and deployment of the application image
stage('Load proj backend environment variables file') {
steps {
container('proj-backend-agent') {
withCredentials([
file(credentialsId: 'proj-back-env-vars', variable: 'PROJ_BACK_ENV')
]) {
load "$PROJ_BACK_ENV"
}
}
}
}
// This stage makes all bash scripts in the scripts directory executable
stage('Make bash scripts executable') {
steps {
container('proj-backend-agent'){
sh "chmod +x jenkins/scripts/*"
}
}
}
// Install npm modules/dependencies for running the application
stage('Install dependencies') {
steps {
container('proj-backend-agent') {
sh "yarn install"
}
}
}
// Download the test reporter
// Tun the tests and push the test coverage to codeclimate
stage('Test and Report') {
steps {
container('proj-backend-agent') {
withCredentials([
file(credentialsId: 'proj-back-env-vars', variable: 'PROJ_BACK_ENV')
]) {
sh "curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter"
sh "chmod +x ./cc-test-reporter"
sh "./cc-test-reporter before-build"
sh "yarn test --coverage"
sh './cc-test-reporter after-build --exit-code $?'
}
}
}
}
// Login to CCP using the login bash script
// This stage is executed only for the master and staging branches
stage('login to gcp') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
container('proj-backend-agent') {
sh "chmod +x jenkins/scripts/gcp_login.sh"
sh "jenkins/scripts/gcp_login.sh main"
}
}
}
// Generate project artifacts - Transpile the code from ES6+ to ES5 using babel
stage('Transpile code') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
container('proj-backend-agent') {
sh "yarn run build"
}
}
}
// Build the image for deployment and push it to GCR
// This stage is executed only for the master and staging branches
stage('Docker build and push') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
container('proj-backend-agent') {
sh "jenkins/scripts/build_and_push.sh"
}
}
}
// Pull the image for the application from GCP and deploy using kubernetes
// This stage is executed only for the master and staging branches
stage('Deploy') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
container('proj-backend-agent') {
sh "jenkins/scripts/deploy.sh"
}
}
}
// Delete the migration job created using kubernetes
stage('Delete migration job') {
when {
anyOf {
branch 'master'
branch 'develop'
}
}
steps {
container('proj-backend-agent') {
sh "jenkins/scripts/delete_migration.sh"
}
}
}
}
// Execute these stages if the pipeline is successful or fails
post {
// Send a failure notification if the pipeline fails
failure {
container('proj-backend-agent') {
sh "jenkins/scripts/slack_notification.sh send_failure_notification"
}
}
// Send a success notification if the pipeline is executed successfuly
success {
container('proj-backend-agent'){
sh "jenkins/scripts/slack_notification.sh send_success_notification"
}
}
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment