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 | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 10 | onos_password = credentials("${params.onos_password}") |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 11 | git_password = credentials("${params.git_password_env}") |
| 12 | gcp = credentials("${params.gcp_credential}") |
| 13 | rancher_dev = credentials("${params.rancher_api_env}") |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 14 | } |
| 15 | stages { |
| 16 | stage('Install tools') { |
| 17 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 18 | sh """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 19 | set -x |
| 20 | apt-get update -y |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 21 | apt-get install -y curl wget jq git unzip |
| 22 | |
| 23 | # Install yq |
| 24 | wget https://github.com/mikefarah/yq/releases/download/3.4.0/yq_linux_amd64 -O /usr/bin/yq &&\ |
| 25 | chmod +x /usr/bin/yq |
| 26 | yq --help |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 27 | |
| 28 | # Install kubectl |
| 29 | curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl" |
| 30 | chmod +x ./kubectl |
| 31 | mv ./kubectl /usr/local/bin/kubectl |
| 32 | |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 33 | # Install terraform |
| 34 | wget https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip |
| 35 | unzip terraform_0.13.2_linux_amd64.zip |
| 36 | mv terraform /usr/local/bin |
| 37 | terraform version |
| 38 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 41 | stage('Init Terraform') { |
| 42 | steps { |
| 43 | withCredentials([sshUserPrivateKey(credentialsId: "aether_jenkins", keyFileVariable: 'keyfile')]) { |
| 44 | |
| 45 | sh """#!/bin/bash |
| 46 | set -x |
| 47 | mkdir -p ~/.ssh |
| 48 | ssh-keyscan -t rsa -p 29418 ${git_server} >> ~/.ssh/known_hosts |
| 49 | cat <<EOF > ~/.ssh/config |
| 50 | Host ${git_server} |
| 51 | User ${git_user} |
| 52 | Hostname ${git_server} |
| 53 | Port 29418 |
| 54 | IdentityFile ${keyfile} |
| 55 | EOF |
| 56 | |
| 57 | git clone "ssh://${git_server}:29418/${git_repo}" |
| 58 | cd ${workspace}/${git_repo}/${terraform_dir}/tost/onos |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 59 | if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 60 | CFG_LAST2=\$(echo ${config_review} | tail -c 3) |
| 61 | git fetch "ssh://${git_server}:29418/${git_repo}" refs/changes/\${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD |
| 62 | cp onos.yaml tmp.yaml |
| 63 | |
| 64 | cat <<EOF >> config.yaml |
| 65 | config: |
| 66 | review: ${config_review} |
| 67 | patchset: ${config_patchset} |
| 68 | EOF |
| 69 | |
| 70 | yq merge tmp.yaml config.yaml > onos.yaml |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 71 | fi |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 72 | GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init |
| 73 | """ |
| 74 | } |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | stage('Uninstall Apps') { |
| 79 | options { |
| 80 | timeout(time: 90, unit: "SECONDS") |
| 81 | } |
| 82 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 83 | sh """ |
| 84 | cd ${workspace}/${git_repo}/${terraform_dir}/tost/onos |
| 85 | 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 |
| 86 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 89 | stage('Remove resources') { |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 90 | options { |
| 91 | timeout(time: 300, unit: "SECONDS") |
| 92 | } |
| 93 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 94 | sh """ |
| 95 | pvcs=\$(kubectl -n onos-tost get pvc -lapp=onos-tost-atomix -o name) |
| 96 | for pv in \${pvcs}; do kubectl -n onos-tost delete \${pv}; done |
| 97 | |
| 98 | kubectl -n ${onos_ns} delete secret onos-git-secret || true |
| 99 | kubectl -n ${onos_ns} delete secret onos-secret || true |
| 100 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | stage('Install apps') { |
| 104 | options { |
| 105 | timeout(time: 600, unit: "SECONDS") |
| 106 | } |
| 107 | steps { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 108 | sh """ |
| 109 | cd ${workspace}/${git_repo}/${terraform_dir}/tost/onos |
| 110 | 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 |
| 111 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 114 | stage('Push Secrets') { |
| 115 | steps { |
| 116 | sh """ |
| 117 | |
| 118 | kubectl -n ${onos_ns} create secret generic onos-git-secret --from-literal=username=${git_user} --from-literal=password=${git_password} |
| 119 | kubectl -n ${onos_ns} create secret generic onos-secret --from-literal=username=${onos_user} --from-literal=password=${onos_password} |
| 120 | """ |
| 121 | } |
| 122 | } |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | post { |
| 126 | always { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 127 | |
| 128 | sh """ |
| 129 | rm -rf ${workspace}/${git_repo} |
| 130 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 131 | cleanWs() |
| 132 | } |
| 133 | } |
| 134 | } |