blob: 2eba5add34cb4c76a2a97d10f4fd60c55196a87b [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 {
16 sh '''
17 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
26 # Test Kubectl & Rancher
27 kubectl get nodes
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070028
29 # Install
30 wget https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip
31 unzip terraform_0.13.2_linux_amd64.zip
32 mv terraform /usr/local/bin
33 terraform version
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070034 '''
35 }
36 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -070037
38 stage('Init Terraform') {
39 steps {
40 withCredentials([sshUserPrivateKey(credentialsId: "aether_jenkins", keyFileVariable: 'keyfile')]) {
41
42 sh """#!/bin/bash
43 set -x
44 mkdir -p ~/.ssh
45 ssh-keyscan -t rsa -p 29418 ${git_server} >> ~/.ssh/known_hosts
46cat <<EOF > ~/.ssh/config
47Host ${git_server}
48 User ${git_user}
49 Hostname ${git_server}
50 Port 29418
51 IdentityFile ${keyfile}
52EOF
53
54 git clone "ssh://${git_server}:29418/${git_repo}"
55 cd ${workspace}/${git_repo}/${terraform_dir}/tost
56 if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then
57 CFG_LAST2=\$(echo ${config_review} | tail -c 3)
58 git fetch "ssh://${git_server}:29418/${git_repo}" refs/changes/\${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD
59 fi
60 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform init
61
62 """
63 }
64 }
65 }
66 stage('Perform Terraform') {
67 steps {
68 sh """
69 cd ${workspace}/${git_repo}/${terraform_dir}/tost
70 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform destroy -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}' -var-file=app_map.tfvars -auto-approve
71 GOOGLE_BACKEND_CREDENTIALS=${gcp} terraform apply -var-file=${rancher_dev} -var 'cluster_name=${rancher_cluster}' -var-file=app_map.tfvars -auto-approve
72
73 """
74 }
75 }
76
77 stage('Install shared resources') {
78 steps {
79 sh(script: "date -u")
80 build(job: "${params.job_name}-shared")
81 }
82 }
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -070083 stage('Parallel Stage') {
84 parallel {
85 stage('onos') {
86 steps {
87 sh(script: "date -u")
88 build(job: "${params.job_name}-onos")
89 }
90 }
91 stage('stratum') {
92 steps {
93 sh(script: "date -u")
94 build(job: "${params.job_name}-stratum")
95 }
96 }
97 stage('telegraf') {
98 steps {
99 sh(script: "date -u")
100 build(job: "${params.job_name}-telegraf")
101 }
102 }
103 }
104 }
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -0700105 stage('E2E Testing') {
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -0700106 options {
107 timeout(time: 120, unit: "SECONDS")
108 }
109 steps {
110 sh """
111 echo ${params.target_server}
112 timestamp=\$(date +%s)
113 if [ ! -z "${params.target_server}" ]; then
114 kubectl delete --wait=true ds -l test=e2e || true
115cat <<EOF > test.yaml
116apiVersion: apps/v1
117kind: DaemonSet
118metadata:
119 name: ping-test-\${timestamp}
120 labels:
121 test: e2e-\${timestamp}
122spec:
123 selector:
124 matchLabels:
125 test: e2e-\${timestamp}
126 template:
127 metadata:
128 labels:
129 test: e2e-\${timestamp}
130 spec:
131 hostNetwork: true
132 terminationGracePeriodSeconds: 5
133 initContainers:
134 - name: ping-test
135 image: alpine:3.12.0
136 command: ["sh", "-c", "until ping ${params.target_server} -c1 -W 4; do echo 'ping retry'; sleep 3; done;"]
137 containers:
138 - name: ping-test-wait
139 image: alpine:3.12.0
140 command: ["sh", "-c", "sleep 100000"]
141 restartPolicy: Always
142EOF
143 kubectl apply -f test.yaml
144 number=\$(kubectl get --no-headers ds -ltest=e2e-\${timestamp} -o custom-columns=':.status.desiredNumberScheduled')
145 echo "\${number}"
146 until [ \${number} -eq \$(kubectl get pods -l test=e2e-\${timestamp} --no-headers -o custom-columns=':.status.phase' | grep Running | wc -l) ]; do echo 'wait ping completed'; sleep 5; done;
147 fi
148 """
149 }
150 }
151 }
152 post {
153 always {
154 sh """
155 if [ ! -z "${params.target_server}" ]; then
156 kubectl delete -f test.yaml
157 fi
Hung-Wei Chiu99bff2f2020-09-17 13:05:31 -0700158 rm -rf ${workspace}/${git_repo}
Hung-Wei Chiud27e33b2020-09-03 12:05:41 -0700159 """
160 cleanWs()
161 }
162 }
163}