drone-kubernetes/update.sh

77 lines
2.4 KiB
Bash
Raw Normal View History

2017-02-08 06:04:53 +00:00
#!/bin/bash
2017-12-11 10:10:44 +00:00
set -euo pipefail
2017-01-13 10:07:48 +00:00
2018-01-10 11:28:04 +00:00
# check optional params
if [ ! -z ${PLUGIN_USER} ]; then
USER=${PLUGIN_USER:-default}
2017-01-16 02:00:29 +00:00
fi
2018-01-10 11:28:04 +00:00
if [ ! -z ${PLUGIN_NAMESPACE} ]; then
NAMESPACE=${PLUGIN_NAMESPACE:-default}
fi
2018-01-10 11:28:04 +00:00
# check required params
if [ ! -z ${PLUGIN_CLUSTER} ]; then
2018-01-10 12:00:40 +00:00
# convert cluster name to ucase and assign
CLUSTER=${PLUGIN_CLUSTER^^}
2017-02-07 06:09:44 +00:00
2018-01-10 12:00:40 +00:00
# create dynamic cert var names
2018-01-10 11:28:04 +00:00
SERVER_URL_VAR=SERVER_URL_${CLUSTER}
SERVER_CERT_VAR=SERVER_CERT_${CLUSTER}
CLIENT_CERT_VAR=CLIENT_CERT_${CLUSTER}
CLIENT_KEY_VAR=CLIENT_KEY_${CLUSTER}
2018-01-10 12:00:40 +00:00
# expand the var contents
2018-01-10 11:28:04 +00:00
SERVER_URL=${!SERVER_URL_VAR}
SERVER_CERT=${!SERVER_CERT_VAR}
CLIENT_CERT=${!CLIENT_CERT_VAR}
CLIENT_KEY=${!CLIENT_KEY_VAR}
2018-01-10 11:28:04 +00:00
if [[ -z "${SERVER_URL}" ]]; then
echo "[ERROR] drone secret: ${SERVER_URL_VAR} not added!"
exit 1
fi
2017-02-20 10:06:46 +00:00
2018-01-10 11:28:04 +00:00
if [[ ! -z "${SERVER_CERT}" ]]; then
echo "[INFO] Using secure connection with tls-certificate."
echo ${SERVER_CERT} | base64 -d > ca.crt
kubectl config set-cluster default --server=${SERVER_URL} --certificate-authority=ca.crt
2018-01-10 11:28:04 +00:00
if [[ ! -z "${CLIENT_CERT}" ]] && [[ ! -z "${CLIENT_KEY}" ]]; then
echo "[INFO] Setting client credentials with signed-certificate and key."
echo ${CLIENT_CERT} | base64 -d > client.crt
echo ${CLIENT_KEY} | base64 -d > client.key
kubectl config set-credentials ${USER} --client-certificate=client.crt --client-key=client.key
else
echo "[ERROR] Required plugin parameters:"
echo " - client_cert"
echo " - client_key"
echo "are not provided"
exit 1
fi
else
echo "[WARNING] Required plugin parameter: ${SERVER_CERT_VAR} not added!"
echo "[WARNING] Using insecure connection to cluster"
kubectl config set-cluster default --server=${SERVER_URL} --insecure-skip-tls-verify=true
fi
2017-12-11 11:55:48 +00:00
else
2018-01-10 11:28:04 +00:00
echo "[ERROR] Required pipeline parameter: cluster not provided"
2017-12-11 11:55:48 +00:00
exit 1
fi
2018-01-10 11:28:04 +00:00
kubectl config set-context default --cluster=default --user=${USER}
2017-01-13 10:07:48 +00:00
kubectl config use-context default
2017-02-20 10:20:47 +00:00
2017-02-20 11:08:45 +00:00
# kubectl version
2017-03-07 09:53:50 +00:00
IFS=',' read -r -a DEPLOYMENTS <<< "${PLUGIN_DEPLOYMENT}"
IFS=',' read -r -a CONTAINERS <<< "${PLUGIN_CONTAINER}"
2017-02-08 06:22:54 +00:00
for DEPLOY in ${DEPLOYMENTS[@]}; do
2018-01-10 11:28:04 +00:00
echo Deploying to ${CLUSTER}
2017-03-07 09:53:50 +00:00
for CONTAINER in ${CONTAINERS[@]}; do
2018-01-10 11:28:04 +00:00
kubectl -n ${NAMESPACE} set image deployment/${DEPLOY} \
2018-01-09 23:52:05 +00:00
${CONTAINER}="${PLUGIN_REPO}:${PLUGIN_TAG}" --record
2017-03-07 09:53:50 +00:00
done
# wait on deployment rollout status
kubectl -n ${NAMESPACE} rollout status deployment/${DEPLOY}
2017-02-08 05:45:22 +00:00
done