blob: 24a777b233e54db1ba5397c32de9f77ae44273f8 [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}")
10 registry_password = credentials("${params.registry_password_env}")
11 git_password = credentials("${params.git_password_env}")
12 rancher_token = credentials("${params.rancher_api_env}")
13 }
14 stages {
15 stage('Install tools') {
16 steps {
17 sh '''
18 set -x
19 apt-get update -y
20 apt-get install -y curl wget jq git
21
22 # Install kubectl
23 curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl"
24 chmod +x ./kubectl
25 mv ./kubectl /usr/local/bin/kubectl
26
27 # Install rancher
28 wget https://github.com/rancher/cli/releases/download/v2.4.5/rancher-linux-amd64-v2.4.5.tar.gz
29 tar -xvf rancher-linux-amd64-v2.4.5.tar.gz
30 mv rancher-v2.4.5/rancher /usr/local/bin
31
32 rm rancher-linux-amd64-v2.4.5.tar.gz
33 rm -rf rancher-v2.4.5
34 # Test Kubectl & Rancher
35 KUBE_CONFIG=$KUBECONFIG kubectl get nodes
36 rancher --version
37 '''
38 }
39 }
40 stage('Clone Config Repo') {
41 options {
42 timeout(time: 10, unit: "SECONDS")
43 }
44 steps {
45 sh '''
46 git clone https://${git_user}:${git_password}@${git_server}/${git_repo}
47 if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then
48 cd ${git_repo}
49 CFG_LAST2=$(echo ${config_review} | tail -c 3)
50 git fetch "https://${git_user}:${git_password}@${git_server}/a/${git_repo}" refs/changes/${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD
51 git checkout FETCH_HEAD
52 echo "config.review: ${config_review}" >> deployment-configs/aether/apps/${config_env}/stratum-ans.yml
53 echo "config.patchset: ${config_patchset}" >> deployment-configs/aether/apps/${config_env}/stratum-ans.yml
54 cd ..
55 fi
56
57 '''
58 }
59 }
60 stage('Login Rancher') {
61 steps {
62 sh '''
63 rancher login ${rancher_server} --token ${rancher_token} --context ${rancher_context}:${rancher_project}
64 '''
65 }
66 }
67 stage('Push Secrets') {
68 steps {
69 sh '''
70
71 rancher namespaces ls | grep ${stratum_ns} || rancher namespaces create ${stratum_ns}
72
73 kubectl -n ${stratum_ns} delete secret git-secret --ignore-not-found=true
74 kubectl -n ${stratum_ns} create secret generic git-secret --from-literal=username=${git_user} --from-literal=password=${git_password}
75 kubectl -n ${stratum_ns} delete secret aether-registry-credential --ignore-not-found=true
76 kubectl -n ${stratum_ns} create secret docker-registry aether-registry-credential --docker-server=${registry_server} --docker-username=${registry_user} --docker-password=${registry_password}
77
78
79 '''
80 }
81 }
82
83 stage('Uninstall Apps') {
84 options {
85 timeout(time: 90, unit: "SECONDS")
86 }
87 steps {
88 sh '''
89 for app in $(rancher apps ls -q | grep -E '(stratum)'); do rancher apps delete $app; done
90
91 until [ "$(rancher apps ls -q | grep -E '(stratum)')" = "" ]; do echo "wait deleted apps"; rancher apps ls ; sleep 1; done
92 '''
93 }
94 }
95 stage('Install apps') {
96 options {
97 timeout(time: 600, unit: "SECONDS")
98 }
99 steps {
100 sh '''
101 cd ${git_repo}/deployment-configs/aether/apps/${config_env}/
102 until rancher apps install --answers stratum-ans.yml --namespace ${stratum_ns} cattle-global-data:${stratum_catalog_name}-stratum stratum; do :; done
103
104 apps=$(rancher apps -q | grep stratum)
105 for app in $apps; do until rancher wait $app --timeout 20; do :; done; rancher apps ls; done
106 '''
107 }
108 }
109
110 }
111 post {
112 always {
113 cleanWs()
114 }
115 }
116}