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 | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 10 | rancher_token = credentials("${params.rancher_cli_env}") |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 11 | } |
| 12 | stages { |
| 13 | stage('Install tools') { |
| 14 | steps { |
| 15 | sh ''' |
| 16 | set -x |
| 17 | apt-get update -y |
| 18 | apt-get install -y curl wget jq git |
| 19 | |
| 20 | # Install kubectl |
| 21 | curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl" |
| 22 | chmod +x ./kubectl |
| 23 | mv ./kubectl /usr/local/bin/kubectl |
| 24 | |
| 25 | # Install rancher |
| 26 | wget https://github.com/rancher/cli/releases/download/v2.4.5/rancher-linux-amd64-v2.4.5.tar.gz |
| 27 | tar -xvf rancher-linux-amd64-v2.4.5.tar.gz |
| 28 | mv rancher-v2.4.5/rancher /usr/local/bin |
| 29 | |
| 30 | rm rancher-linux-amd64-v2.4.5.tar.gz |
| 31 | rm -rf rancher-v2.4.5 |
| 32 | # Test Kubectl & Rancher |
| 33 | KUBE_CONFIG=$KUBECONFIG kubectl get nodes |
| 34 | rancher --version |
| 35 | ''' |
| 36 | } |
| 37 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 38 | stage('Init git') { |
| 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 |
| 46 | cat <<EOF > ~/.ssh/config |
| 47 | Host ${git_server} |
| 48 | User ${git_user} |
| 49 | Hostname ${git_server} |
| 50 | Port 29418 |
| 51 | IdentityFile ${keyfile} |
| 52 | EOF |
| 53 | |
| 54 | git clone "ssh://${git_server}:29418/${git_repo}" |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 55 | if [ ! -z ${config_review} ] && [ ! -z ${config_patchset} ]; then |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 56 | CFG_LAST2=\$(echo ${config_review} | tail -c 3) |
| 57 | git fetch "ssh://@${git_server}:29418/${git_repo}" refs/changes/\${CFG_LAST2}/${config_review}/${config_patchset} && git checkout FETCH_HEAD |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 58 | fi |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 59 | |
| 60 | """ |
| 61 | } |
| 62 | } |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 63 | } |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 64 | |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 65 | stage('Login Rancher') { |
| 66 | steps { |
| 67 | sh ''' |
| 68 | rancher login ${rancher_server} --token ${rancher_token} --context ${rancher_context}:${rancher_project} |
| 69 | ''' |
| 70 | } |
| 71 | } |
| 72 | stage('Uninstall Apps') { |
| 73 | options { |
| 74 | timeout(time: 90, unit: "SECONDS") |
| 75 | } |
| 76 | steps { |
| 77 | sh ''' |
| 78 | for app in $(rancher apps ls -q | grep -E '(fluentbit)'); do rancher apps delete $app; done |
| 79 | |
| 80 | until [ "$(rancher apps ls -q | grep -E '(fluentbit)')" = "" ]; do echo "wait deleted apps"; rancher apps ls ; sleep 1; done |
| 81 | ''' |
| 82 | } |
| 83 | } |
| 84 | stage('Install apps') { |
| 85 | options { |
| 86 | timeout(time: 600, unit: "SECONDS") |
| 87 | } |
| 88 | steps { |
| 89 | sh ''' |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 90 | cd ${workspace}/${git_repo}/deployment-configs/aether/apps/${config_env}/ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 91 | until rancher apps install --answers fluentbit-ans.yml --namespace ${fluentbit_ns} cattle-global-data:fluent-fluent-bit fluentbit; do :; done |
| 92 | apps=$(rancher apps -q | grep fluentbit) |
| 93 | for app in $apps; do until rancher wait $app --timeout 20; do :; done; rancher apps ls; done |
| 94 | ''' |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | post { |
| 100 | always { |
Hung-Wei Chiu | 99bff2f | 2020-09-17 13:05:31 -0700 | [diff] [blame] | 101 | sh """ |
| 102 | rm -rf ${workspace}/${git_repo} |
| 103 | """ |
Hung-Wei Chiu | d27e33b | 2020-09-03 12:05:41 -0700 | [diff] [blame] | 104 | cleanWs() |
| 105 | } |
| 106 | } |
| 107 | } |