Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 1 | pipeline { |
| 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 Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 10 | gcp = credentials("${params.gcp_credential}") |
| 11 | rancher_dev = credentials("${params.rancher_api_env}") |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 12 | } |
| 13 | stages { |
| 14 | stage('Install tools') { |
| 15 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 16 | sh """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 17 | set -x |
| 18 | apt-get update -y |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 19 | apt-get install -y curl wget jq git unzip |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 20 | |
| 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 Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 26 | 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 Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 31 | } |
| 32 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 33 | stage('Init Terraform') { |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 34 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 35 | 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 |
| 40 | cat <<EOF > ~/.ssh/config |
| 41 | Host ${git_server} |
| 42 | User ${git_user} |
| 43 | Hostname ${git_server} |
| 44 | Port 29418 |
| 45 | IdentityFile ${keyfile} |
| 46 | EOF |
| 47 | git clone "ssh://${git_server}:29418/${git_repo}" |
| 48 | cd ${workspace}/${git_repo}/${terraform_dir}/tost/telegraf |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 49 | if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 50 | 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 Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 52 | git checkout FETCH_HEAD |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 53 | fi |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 54 | GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init |
| 55 | """ |
| 56 | } |
| 57 | } |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 58 | } |
| 59 | stage('Uninstall Apps') { |
| 60 | options { |
| 61 | timeout(time: 90, unit: "SECONDS") |
| 62 | } |
| 63 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 64 | 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 Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | stage('Install apps') { |
| 71 | options { |
| 72 | timeout(time: 600, unit: "SECONDS") |
| 73 | } |
| 74 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 75 | 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 Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | post { |
| 84 | always { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 85 | sh """ |
| 86 | rm -rf ${workspace}/${git_repo} |
| 87 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 88 | cleanWs() |
| 89 | } |
| 90 | } |
| 91 | } |