Deploy OpenShift using Jenkins Pipeline and Terraform

Bernd Malmqvist
Dec 24, 2018 · 4 min read
pipeline {
agent any
environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
TF_VAR_email = credentials('TF_VAR_email')
TF_VAR_token = credentials('TF_VAR_token')
TF_VAR_domain = credentials('TF_VAR_domain')
TF_VAR_htpasswd = credentials('TF_VAR_htpasswd')
}
stages {
stage('Prepare workspace') {
steps {
sh 'rm -rf *'
git branch: 'aws-dev', url: 'https://github.com/berndonline/openshift-terraform.git'
sh 'ssh-keygen -b 2048 -t rsa -f ./helper_scripts/id_rsa -q -N ""'
sh 'chmod 600 ./helper_scripts/id_rsa'
sh 'terraform init'
}
}
stage('Run terraform apply') {
steps {
input 'Run terraform apply?'
}
}
stage('terraform apply') {
steps {
sh 'terraform apply -auto-approve'
}
}
stage('OpenShift Installation') {
steps {
sh 'sleep 600'
sh 'scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ./helper_scripts/id_rsa -r ./helper_scripts/id_rsa centos@$(terraform output bastion):/home/centos/.ssh/'
sh 'scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ./helper_scripts/id_rsa -r ./inventory/ansible-hosts centos@$(terraform output bastion):/home/centos/ansible-hosts'
sh 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ./helper_scripts/id_rsa -l centos $(terraform output bastion) -A "cd /openshift-ansible/ && ansible-playbook ./playbooks/openshift-pre.yml -i ~/ansible-hosts"'
sh 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ./helper_scripts/id_rsa -l centos $(terraform output bastion) -A "cd /openshift-ansible/ && ansible-playbook ./playbooks/openshift-install.yml -i ~/ansible-hosts"'
}
}
stage('Run terraform destroy') {
steps {
input 'Run terraform destroy?'
}
}
stage('terraform destroy') {
steps {
sh 'terraform destroy -force '
}
}
}
}

Bernd Malmqvist

Written by

Highly versatile Senior technical Lead Engineer, I am a consummate and competent qualified IT Professional specialising in distributed systems

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade