blob: ac6149681a40d242332058ff1e791164c204ecb1 [file] [log] [blame]
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -07001pipeline {
2 agent {
3 docker {
4 image 'ubuntu:18.04'
5 args '-u root:sudo'
6 }
7 }
8 environment {
9 KUBECONFIG = credentials("${params.k8s_config}")
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070010 gcp = credentials("${params.gcp_credential}")
11 rancher_dev = credentials("${params.rancher_api_env}")
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070012 }
13 stages {
14 stage('Install tools') {
15 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070016 sh """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070017 set -x
18 apt-get update -y
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070019 apt-get install -y curl wget jq git unzip
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070020
21 # Install kubectl
22 curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl"
23 chmod +x ./kubectl
24 mv ./kubectl /usr/local/bin/kubectl
25
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070026 wget https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip
27 unzip terraform_0.13.2_linux_amd64.zip
28 mv terraform /usr/local/bin
29 terraform version
30 """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070031 }
32 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070033 stage('Init Terraform') {
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070034 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070035 withCredentials([sshUserPrivateKey(credentialsId: "aether_jenkins", keyFileVariable: 'keyfile')]) {
36 sh """#!/bin/bash
37 set -x
38 mkdir -p ~/.ssh
39 ssh-keyscan -t rsa -p 29418 ${git_server} >> ~/.ssh/known_hosts
40cat <<EOF > ~/.ssh/config
41Host ${git_server}
42 User ${git_user}
43 Hostname ${git_server}
44 Port 29418
45 IdentityFile ${keyfile}
46EOF
47 git clone "ssh://${git_server}:29418/${git_repo}"
48 cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070049 if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070050 CFG_LAST2=\$(echo ${config_review} | tail -c 3)
51 git fetch "ssh://@${git_server}:29418/${git_repo}" refs/changes/\${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070052 git checkout FETCH_HEAD
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070053 fi
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070054 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init
55 """
56 }
57 }
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070058 }
59 stage('Uninstall Apps') {
60 options {
61 timeout(time: 90, unit: "SECONDS")
62 }
63 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070064 sh """
65 cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
66 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform destroy -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}' -var 'project_name=tost' -var-file=app_map.tfvars -auto-approve
67 """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070068 }
69 }
70 stage('Install apps') {
71 options {
72 timeout(time: 600, unit: "SECONDS")
73 }
74 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070075 sh """
76 cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf
77 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform apply -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}' -var 'project_name=tost' -var-file=app_map.tfvars -auto-approve
78 """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070079 }
80 }
81
82 }
83 post {
84 always {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070085 sh """
86 rm -rf ${workspace}/${git_repo}
87 """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070088 cleanWs()
89 }
90 }
91}