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