blob: 24a79141e36ee7f0a31cff41ac37c734dd59431c [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 Chiud27e33b2020-09-03 12:05:41 -070010 onos_password = credentials("${params.onos_password}")
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070011 git_password = credentials("${params.git_password_env}")
12 gcp = credentials("${params.gcp_credential}")
13 rancher_dev = credentials("${params.rancher_api_env}")
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070014 }
15 stages {
16 stage('Install tools') {
17 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070018 sh """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070019 set -x
20 apt-get update -y
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070021 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 Chiud27e33b2020-09-03 12:05:41 -070027
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 Chiu99bff2f2020-09-17 13:05:31 -070033 # 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 Chiud27e33b2020-09-03 12:05:41 -070039 }
40 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070041 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
49cat <<EOF > ~/.ssh/config
50Host ${git_server}
51 User ${git_user}
52 Hostname ${git_server}
53 Port 29418
54 IdentityFile ${keyfile}
55EOF
56
57 git clone "ssh://${git_server}:29418/${git_repo}"
58 cd ${workspace}/${git_repo}/${terraform_dir}/tost/onos
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070059 if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070060 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
64cat <<EOF >> config.yaml
65config:
66 review: ${config_review}
67 patchset: ${config_patchset}
68EOF
69
70 yq merge tmp.yaml config.yaml > onos.yaml
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070071 fi
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070072 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init
73 """
74 }
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070075 }
76 }
77
78 stage('Uninstall Apps') {
79 options {
80 timeout(time: 90, unit: "SECONDS")
81 }
82 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070083 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 Chiud27e33b2020-09-03 12:05:41 -070087 }
88 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070089 stage('Remove resources') {
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070090 options {
91 timeout(time: 300, unit: "SECONDS")
92 }
93 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070094 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 Chiud27e33b2020-09-03 12:05:41 -0700101 }
102 }
103 stage('Install apps') {
104 options {
105 timeout(time: 600, unit: "SECONDS")
106 }
107 steps {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -0700108 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 Chiud27e33b2020-09-03 12:05:41 -0700112 }
113 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -0700114 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 Chiud27e33b2020-09-03 12:05:41 -0700123
124 }
125 post {
126 always {
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -0700127
128 sh """
129 rm -rf ${workspace}/${git_repo}
130 """
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -0700131 cleanWs()
132 }
133 }
134}