Skip to content

Add new Jenkins deployment workflow #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 41 additions & 59 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,90 +1,72 @@
#!groovy

// import shared library: https://github.com/cmu-delphi/jenkins-shared-library
// Import shared lib.
@Library('jenkins-shared-library') _

pipeline {
/*
Declare variables.
- indicator_list should contain all the indicators to handle in the pipeline.
- Keep in sync with '.github/workflows/python-ci.yml'.
- TODO: Get this list automatically from python-ci.yml at runtime?
*/
def indicator_list = ["cdc_covidnet", "claims_hosp", "combo_cases_and_deaths", "google_symptoms", "jhu", "nchs_mortality", "quidel", "quidel_covidtest", "safegraph", "safegraph_patterns", "usafacts"]
def build_package = [:]
def deploy_staging = [:]
def deploy_production = [:]

pipeline {
agent any

stages {

stage ("Environment") {
stage('Build and Package') {
when {
anyOf {
branch "deploy-*";
changeRequest target: "deploy-*", comparator: "GLOB"
}
branch "main";
}
steps {
script {
// Get the indicator name from the pipeline env.
if ( env.CHANGE_TARGET ) {
INDICATOR = env.CHANGE_TARGET.replaceAll("deploy-", "")
indicator_list.each { indicator ->
build_package[indicator] = {
sh "jenkins/build-and-package.sh ${indicator}"
}
}
else if ( env.BRANCH_NAME ) {
INDICATOR = env.BRANCH_NAME.replaceAll("deploy-", "")
}
else {
INDICATOR = ""
}
}
}
}

stage('Build') {
when {
changeRequest target: "deploy-*", comparator: "GLOB"
}
steps {
sh "jenkins/${INDICATOR}-jenkins-build.sh"
}
}

stage('Test') {
when {
changeRequest target: "deploy-*", comparator: "GLOB"
}
steps {
sh "jenkins/${INDICATOR}-jenkins-test.sh"
}
}

stage('Package') {
when {
changeRequest target: "deploy-*", comparator: "GLOB"
}
steps {
sh "jenkins/${INDICATOR}-jenkins-package.sh"
parallel build_package
}
}
}

stage('Deploy to staging env') {
stage('Deploy staging') {
when {
changeRequest target: "deploy-*", comparator: "GLOB"
branch "main";
}
steps {
sh "jenkins/jenkins-deploy-staging.sh ${INDICATOR}"
script {
indicator_list.each { indicator ->
deploy_staging[indicator] = {
sh "jenkins/deploy-staging.sh ${indicator}"
}
}
parallel deploy_staging
}
}
}

stage('Deploy') {
stage('Deploy production') {
when {
branch "deploy-*"
branch "prod";
}
steps {
sh "jenkins/${INDICATOR}-jenkins-deploy.sh"
script {
indicator_list.each { indicator ->
deploy_production[indicator] = {
sh "jenkins/deploy-production.sh ${indicator}"
}
}
parallel deploy_production
}
}
}
}

post {
always {
script {
/*
Use slackNotifier.groovy from shared library and provide current
build result as parameter.
*/
// Use slackNotifier.groovy from shared lib.
slackNotifier(currentBuild.currentResult)
}
}
Expand Down
3 changes: 2 additions & 1 deletion ansible/vars.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
runtime_user: "indicators"
jenkins_user: "jenkins"
jenkins_artifact_dir: "/var/lib/jenkins/artifacts"
#jenkins_artifact_dir: "/var/lib/jenkins/artifacts" # TODO: Switcheroo to `artifacts` after this goes live!
jenkins_artifact_dir: "/var/lib/jenkins/test-artifacts"
indicators_runtime_dir: "/home/{{ runtime_user }}/runtime"
package: "{{ indicator }}.tar.gz" # {{ indicator }} is passed in from the Jenkins shell script wrapper.
python_version: "3.8.2"
Expand Down
32 changes: 32 additions & 0 deletions jenkins/build-and-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# Jenkins build-and-package
#

set -eo pipefail
source ~/.bash_profile

# Vars
local_indicator=$1

#
# Build
#

cd "${WORKSPACE}/${local_indicator}" || exit

# Set up venv
python -m venv env
source env/bin/activate
pip install ../_delphi_utils_python/.
pip install .

#
# Package
#

cd "${WORKSPACE}" || exit

# Create .tar.gz for deployment
# TODO: Switcheroo to `artifacts` after this goes live!
tar -czvf "${JENKINS_HOME}/test-artifacts/${local_indicator}.tar.gz" "${local_indicator}"
18 changes: 18 additions & 0 deletions jenkins/deploy-production.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
#
# Jenkins deploy
#

set -eo pipefail
source ~/.bash_profile

#
# Deploy
#

local_indicator=$1

cd "${WORKSPACE}/ansible" || exit

# Ansible!
ansible-playbook ansible-deploy.yaml --extra-vars "indicator=${local_indicator}" -i inventory
18 changes: 18 additions & 0 deletions jenkins/deploy-staging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
#
# Jenkins deploy
#

set -eo pipefail
source ~/.bash_profile

#
# Deploy
#

local_indicator=$1

cd "${WORKSPACE}/ansible" || exit

# Ansible!
ansible-playbook ansible-deploy-staging.yaml --extra-vars "indicator=${local_indicator}" -i inventory