2018-09-25 14:32:53 +00:00
|
|
|
[[ "$TRACE" ]] && set -x
|
|
|
|
export TILLER_NAMESPACE="$KUBE_NAMESPACE"
|
|
|
|
|
2019-03-27 09:36:01 +00:00
|
|
|
function echoerr() {
|
|
|
|
local header="${2}"
|
|
|
|
|
|
|
|
if [ -n "${header}" ]; then
|
|
|
|
printf "\n\033[0;31m** %s **\n\033[0m" "${1}" >&2;
|
|
|
|
else
|
|
|
|
printf "\033[0;31m%s\n\033[0m" "${1}" >&2;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function echoinfo() {
|
|
|
|
local header="${2}"
|
|
|
|
|
|
|
|
if [ -n "${header}" ]; then
|
|
|
|
printf "\n\033[0;33m** %s **\n\033[0m" "${1}" >&2;
|
|
|
|
else
|
|
|
|
printf "\033[0;33m%s\n\033[0m" "${1}" >&2;
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function deployExists() {
|
|
|
|
local namespace="${1}"
|
|
|
|
local deploy="${2}"
|
|
|
|
echoinfo "Checking if ${deploy} exists in the ${namespace} namespace..." true
|
|
|
|
|
|
|
|
helm status --tiller-namespace "${namespace}" "${deploy}" >/dev/null 2>&1
|
|
|
|
local deploy_exists=$?
|
|
|
|
|
|
|
|
echoinfo "Deployment status for ${deploy} is ${deploy_exists}"
|
|
|
|
return $deploy_exists
|
|
|
|
}
|
|
|
|
|
|
|
|
function previousDeployFailed() {
|
|
|
|
set +e
|
|
|
|
local deploy="${1}"
|
|
|
|
echoinfo "Checking for previous deployment of ${deploy}" true
|
|
|
|
|
2019-04-09 11:12:49 +00:00
|
|
|
helm status "${deploy}" >/dev/null 2>&1
|
2019-03-27 09:36:01 +00:00
|
|
|
local status=$?
|
|
|
|
|
|
|
|
# if `status` is `0`, deployment exists, has a status
|
|
|
|
if [ $status -eq 0 ]; then
|
|
|
|
echoinfo "Previous deployment found, checking status..."
|
2019-04-09 11:12:49 +00:00
|
|
|
deployment_status=$(helm status "${deploy}" | grep ^STATUS | cut -d' ' -f2)
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Previous deployment state: ${deployment_status}"
|
|
|
|
if [[ "$deployment_status" == "FAILED" || "$deployment_status" == "PENDING_UPGRADE" || "$deployment_status" == "PENDING_INSTALL" ]]; then
|
|
|
|
status=0;
|
|
|
|
else
|
|
|
|
status=1;
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echoerr "Previous deployment NOT found."
|
|
|
|
fi
|
|
|
|
set -e
|
|
|
|
return $status
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete() {
|
|
|
|
if [ -z "$CI_ENVIRONMENT_SLUG" ]; then
|
|
|
|
echoerr "No release given, aborting the delete!"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
local name="$CI_ENVIRONMENT_SLUG"
|
|
|
|
|
|
|
|
echoinfo "Deleting release '$name'..." true
|
|
|
|
|
|
|
|
helm delete --purge "$name" || true
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
if [ -z "$CI_ENVIRONMENT_SLUG" ]; then
|
|
|
|
echoerr "No release given, aborting the delete!"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
echoinfo "Cleaning up '$CI_ENVIRONMENT_SLUG'..." true
|
|
|
|
|
|
|
|
kubectl -n "$KUBE_NAMESPACE" delete \
|
|
|
|
ingress,svc,pdb,hpa,deploy,statefulset,job,pod,secret,configmap,pvc,secret,clusterrole,clusterrolebinding,role,rolebinding,sa \
|
|
|
|
-l release="$CI_ENVIRONMENT_SLUG" \
|
|
|
|
|| true
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_pod() {
|
|
|
|
local app_name="${1}"
|
|
|
|
local status="${2-Running}"
|
|
|
|
get_pod_cmd="kubectl get pods -n ${KUBE_NAMESPACE} --field-selector=status.phase=${status} -lapp=${app_name},release=${CI_ENVIRONMENT_SLUG} --no-headers -o=custom-columns=NAME:.metadata.name"
|
|
|
|
echoinfo "Running '${get_pod_cmd}'" true
|
|
|
|
|
|
|
|
while true; do
|
2019-04-09 11:12:49 +00:00
|
|
|
local pod_name
|
|
|
|
pod_name="$(eval "${get_pod_cmd}")"
|
2019-03-27 09:36:01 +00:00
|
|
|
[[ "${pod_name}" == "" ]] || break
|
|
|
|
|
|
|
|
echoinfo "Waiting till '${app_name}' pod is ready";
|
|
|
|
sleep 5;
|
|
|
|
done
|
|
|
|
|
|
|
|
echoinfo "The pod name is '${pod_name}'."
|
|
|
|
echo "${pod_name}"
|
|
|
|
}
|
2018-11-06 22:59:25 +00:00
|
|
|
|
2019-03-19 07:30:25 +00:00
|
|
|
function perform_review_app_deployment() {
|
|
|
|
check_kube_domain
|
|
|
|
ensure_namespace
|
|
|
|
install_tiller
|
|
|
|
install_external_dns
|
2019-04-09 11:12:49 +00:00
|
|
|
time deploy || true
|
2019-04-08 17:10:18 +00:00
|
|
|
wait_for_review_app_to_be_accessible
|
2019-03-19 07:30:25 +00:00
|
|
|
add_license
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:32:53 +00:00
|
|
|
function check_kube_domain() {
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Checking that Kube domain exists..." true
|
|
|
|
|
2018-09-25 14:32:53 +00:00
|
|
|
if [ -z ${REVIEW_APPS_DOMAIN+x} ]; then
|
|
|
|
echo "In order to deploy or use Review Apps, REVIEW_APPS_DOMAIN variable must be set"
|
|
|
|
echo "You can do it in Auto DevOps project settings or defining a variable at group or project level"
|
|
|
|
echo "You can also manually add it in .gitlab-ci.yml"
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensure_namespace() {
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Ensuring the ${KUBE_NAMESPACE} namespace exists..." true
|
|
|
|
|
2018-09-25 14:32:53 +00:00
|
|
|
kubectl describe namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_tiller() {
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Checking deployment/tiller-deploy status in the ${TILLER_NAMESPACE} namespace..." true
|
|
|
|
|
|
|
|
echoinfo "Initiating the Helm client..."
|
|
|
|
helm init --client-only
|
|
|
|
|
2018-12-13 15:45:44 +00:00
|
|
|
helm init \
|
|
|
|
--upgrade \
|
|
|
|
--replicas 2
|
2019-03-27 09:36:01 +00:00
|
|
|
|
2018-09-25 14:32:53 +00:00
|
|
|
kubectl rollout status -n "$TILLER_NAMESPACE" -w "deployment/tiller-deploy"
|
2019-03-27 09:36:01 +00:00
|
|
|
|
2018-09-25 14:32:53 +00:00
|
|
|
if ! helm version --debug; then
|
|
|
|
echo "Failed to init Tiller."
|
|
|
|
return 1
|
|
|
|
fi
|
2019-03-27 09:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function install_external_dns() {
|
|
|
|
local release_name="dns-gitlab-review-app"
|
2019-04-09 11:12:49 +00:00
|
|
|
local domain
|
|
|
|
domain=$(echo "${REVIEW_APPS_DOMAIN}" | awk -F. '{printf "%s.%s", $(NF-1), $NF}')
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Installing external DNS for domain ${domain}..." true
|
|
|
|
|
|
|
|
if ! deployExists "${KUBE_NAMESPACE}" "${release_name}" || previousDeployFailed "${release_name}" ; then
|
|
|
|
echoinfo "Installing external-dns Helm chart"
|
|
|
|
helm repo update
|
|
|
|
helm install stable/external-dns \
|
|
|
|
-n "${release_name}" \
|
|
|
|
--namespace "${KUBE_NAMESPACE}" \
|
|
|
|
--set provider="aws" \
|
|
|
|
--set aws.secretKey="${REVIEW_APPS_AWS_SECRET_KEY}" \
|
|
|
|
--set aws.accessKey="${REVIEW_APPS_AWS_ACCESS_KEY}" \
|
|
|
|
--set aws.zoneType="public" \
|
|
|
|
--set domainFilters[0]="${domain}" \
|
|
|
|
--set txtOwnerId="${KUBE_NAMESPACE}" \
|
|
|
|
--set rbac.create="true" \
|
|
|
|
--set policy="sync"
|
|
|
|
else
|
|
|
|
echoinfo "The external-dns Helm chart is already successfully deployed."
|
|
|
|
fi
|
2018-09-25 14:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function create_secret() {
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Creating the ${CI_ENVIRONMENT_SLUG}-gitlab-initial-root-password secret in the ${KUBE_NAMESPACE} namespace..." true
|
2018-09-25 14:32:53 +00:00
|
|
|
|
|
|
|
kubectl create secret generic -n "$KUBE_NAMESPACE" \
|
2019-04-09 11:12:49 +00:00
|
|
|
"${CI_ENVIRONMENT_SLUG}-gitlab-initial-root-password" \
|
|
|
|
--from-literal="password=${REVIEW_APPS_ROOT_PASSWORD}" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--dry-run -o json | kubectl apply -f -
|
|
|
|
}
|
|
|
|
|
2019-03-27 09:36:01 +00:00
|
|
|
function download_gitlab_chart() {
|
|
|
|
echoinfo "Downloading the GitLab chart..." true
|
2018-09-20 14:37:21 +00:00
|
|
|
|
2019-04-09 11:12:49 +00:00
|
|
|
curl -o gitlab.tar.bz2 "https://gitlab.com/charts/gitlab/-/archive/${GITLAB_HELM_CHART_REF}/gitlab-${GITLAB_HELM_CHART_REF}.tar.bz2"
|
2019-03-27 09:36:01 +00:00
|
|
|
tar -xjf gitlab.tar.bz2
|
2019-04-09 11:12:49 +00:00
|
|
|
cd "gitlab-${GITLAB_HELM_CHART_REF}"
|
2019-03-27 09:36:01 +00:00
|
|
|
|
|
|
|
echoinfo "Adding the gitlab repo to Helm..."
|
|
|
|
helm repo add gitlab https://charts.gitlab.io
|
|
|
|
|
|
|
|
echoinfo "Building the gitlab chart's dependencies..."
|
|
|
|
helm dependency build .
|
2018-09-25 14:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deploy() {
|
2019-03-27 09:36:01 +00:00
|
|
|
local name="$CI_ENVIRONMENT_SLUG"
|
|
|
|
echoinfo "Deploying ${name}..." true
|
2018-09-25 14:32:53 +00:00
|
|
|
|
2018-11-06 22:59:25 +00:00
|
|
|
IMAGE_REPOSITORY="registry.gitlab.com/gitlab-org/build/cng-mirror"
|
|
|
|
IMAGE_VERSION="${CI_PROJECT_NAME#gitlab-}"
|
|
|
|
gitlab_migrations_image_repository="${IMAGE_REPOSITORY}/gitlab-rails-${IMAGE_VERSION}"
|
|
|
|
gitlab_sidekiq_image_repository="${IMAGE_REPOSITORY}/gitlab-sidekiq-${IMAGE_VERSION}"
|
|
|
|
gitlab_unicorn_image_repository="${IMAGE_REPOSITORY}/gitlab-unicorn-${IMAGE_VERSION}"
|
|
|
|
gitlab_task_runner_image_repository="${IMAGE_REPOSITORY}/gitlab-task-runner-${IMAGE_VERSION}"
|
|
|
|
gitlab_gitaly_image_repository="${IMAGE_REPOSITORY}/gitaly"
|
|
|
|
gitlab_shell_image_repository="${IMAGE_REPOSITORY}/gitlab-shell"
|
|
|
|
gitlab_workhorse_image_repository="${IMAGE_REPOSITORY}/gitlab-workhorse-${IMAGE_VERSION}"
|
2018-09-25 14:32:53 +00:00
|
|
|
|
|
|
|
# Cleanup and previous installs, as FAILED and PENDING_UPGRADE will cause errors with `upgrade`
|
2018-09-20 14:37:21 +00:00
|
|
|
if [ "$CI_ENVIRONMENT_SLUG" != "production" ] && previousDeployFailed "$CI_ENVIRONMENT_SLUG" ; then
|
2018-09-25 14:32:53 +00:00
|
|
|
echo "Deployment in bad state, cleaning up $CI_ENVIRONMENT_SLUG"
|
|
|
|
delete
|
|
|
|
cleanup
|
|
|
|
fi
|
2018-11-09 11:30:49 +00:00
|
|
|
|
|
|
|
create_secret
|
2019-03-27 09:36:01 +00:00
|
|
|
download_gitlab_chart
|
2018-09-25 14:32:53 +00:00
|
|
|
|
|
|
|
HELM_CMD=$(cat << EOF
|
|
|
|
helm upgrade --install \
|
|
|
|
--wait \
|
|
|
|
--timeout 600 \
|
2018-11-11 11:45:03 +00:00
|
|
|
--set global.appConfig.enableUsagePing=false \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set releaseOverride="$CI_ENVIRONMENT_SLUG" \
|
|
|
|
--set global.hosts.hostSuffix="$HOST_SUFFIX" \
|
|
|
|
--set global.hosts.domain="$REVIEW_APPS_DOMAIN" \
|
|
|
|
--set certmanager.install=false \
|
2019-02-01 13:33:19 +00:00
|
|
|
--set prometheus.install=false \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set global.ingress.configureCertmanager=false \
|
|
|
|
--set global.ingress.tls.secretName=tls-cert \
|
|
|
|
--set global.ingress.annotations."external-dns\.alpha\.kubernetes\.io/ttl"="10"
|
2019-01-24 13:14:42 +00:00
|
|
|
--set nginx-ingress.defaultBackend.resources.requests.memory=7Mi \
|
|
|
|
--set nginx-ingress.controller.resources.requests.memory=440M \
|
2019-02-01 13:36:24 +00:00
|
|
|
--set nginx-ingress.controller.replicaCount=2 \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.unicorn.resources.requests.cpu=200m \
|
|
|
|
--set gitlab.sidekiq.resources.requests.cpu=100m \
|
2019-01-24 13:14:42 +00:00
|
|
|
--set gitlab.sidekiq.resources.requests.memory=800M \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.gitlab-shell.resources.requests.cpu=100m \
|
|
|
|
--set redis.resources.requests.cpu=100m \
|
|
|
|
--set minio.resources.requests.cpu=100m \
|
|
|
|
--set gitlab.migrations.image.repository="$gitlab_migrations_image_repository" \
|
2019-01-17 14:13:31 +00:00
|
|
|
--set gitlab.migrations.image.tag="$CI_COMMIT_REF_SLUG" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.sidekiq.image.repository="$gitlab_sidekiq_image_repository" \
|
2019-01-17 14:13:31 +00:00
|
|
|
--set gitlab.sidekiq.image.tag="$CI_COMMIT_REF_SLUG" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.unicorn.image.repository="$gitlab_unicorn_image_repository" \
|
2019-01-17 14:13:31 +00:00
|
|
|
--set gitlab.unicorn.image.tag="$CI_COMMIT_REF_SLUG" \
|
2018-11-06 22:59:25 +00:00
|
|
|
--set gitlab.task-runner.image.repository="$gitlab_task_runner_image_repository" \
|
2019-01-17 14:13:31 +00:00
|
|
|
--set gitlab.task-runner.image.tag="$CI_COMMIT_REF_SLUG" \
|
2019-04-09 11:12:49 +00:00
|
|
|
--set gitlab.gitaly.image.repository="$gitlab_gitaly_image_repository" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.gitaly.image.tag="v$GITALY_VERSION" \
|
2019-04-09 11:12:49 +00:00
|
|
|
--set gitlab.gitlab-shell.image.repository="$gitlab_shell_image_repository" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--set gitlab.gitlab-shell.image.tag="v$GITLAB_SHELL_VERSION" \
|
|
|
|
--set gitlab.unicorn.workhorse.image="$gitlab_workhorse_image_repository" \
|
2019-01-17 14:13:31 +00:00
|
|
|
--set gitlab.unicorn.workhorse.tag="$CI_COMMIT_REF_SLUG" \
|
2018-09-20 14:37:21 +00:00
|
|
|
--set nginx-ingress.controller.config.ssl-ciphers="ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4" \
|
2018-09-25 14:32:53 +00:00
|
|
|
--namespace="$KUBE_NAMESPACE" \
|
|
|
|
--version="$CI_PIPELINE_ID-$CI_JOB_ID" \
|
|
|
|
"$name" \
|
|
|
|
.
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Deploying with:"
|
|
|
|
echoinfo "${HELM_CMD}"
|
2018-09-25 14:32:53 +00:00
|
|
|
|
|
|
|
eval $HELM_CMD
|
2019-04-08 17:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wait_for_review_app_to_be_accessible() {
|
|
|
|
# In case the Review App isn't completely available yet. Keep trying for 5 minutes.
|
|
|
|
local interval=5
|
|
|
|
local elapsed_seconds=0
|
2019-04-09 11:12:49 +00:00
|
|
|
local max_seconds=$((5 * 60))
|
2019-04-08 17:10:18 +00:00
|
|
|
while true; do
|
2019-04-09 11:12:49 +00:00
|
|
|
local review_app_http_code
|
|
|
|
review_app_http_code=$(curl --silent --output /dev/null --max-time 5 --write-out "%{http_code}" "${CI_ENVIRONMENT_URL}/users/sign_in")
|
|
|
|
if [[ "${review_app_http_code}" -eq "200" ]] || [[ "${elapsed_seconds}" -gt "${max_seconds}" ]]; then
|
|
|
|
break
|
|
|
|
fi
|
2019-04-08 17:10:18 +00:00
|
|
|
|
|
|
|
printf "."
|
|
|
|
let "elapsed_seconds+=interval"
|
|
|
|
sleep ${interval}
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "${review_app_http_code}" == "200" ]]; then
|
|
|
|
echoinfo "The Review App at ${CI_ENVIRONMENT_URL} is ready!"
|
|
|
|
else
|
|
|
|
echoerr "The Review App at ${CI_ENVIRONMENT_URL} isn't ready after 5 minutes of polling..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-09-25 14:32:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 22:59:25 +00:00
|
|
|
function add_license() {
|
|
|
|
if [ -z "${REVIEW_APPS_EE_LICENSE}" ]; then echo "License not found" && return; fi
|
|
|
|
|
|
|
|
task_runner_pod=$(get_pod "task-runner");
|
|
|
|
if [ -z "${task_runner_pod}" ]; then echo "Task runner pod not found" && return; fi
|
|
|
|
|
2019-03-27 09:36:01 +00:00
|
|
|
echoinfo "Installing license..." true
|
|
|
|
|
2018-11-06 22:59:25 +00:00
|
|
|
echo "${REVIEW_APPS_EE_LICENSE}" > /tmp/license.gitlab
|
2019-04-09 11:12:49 +00:00
|
|
|
kubectl -n "$KUBE_NAMESPACE" cp /tmp/license.gitlab "${task_runner_pod}":/tmp/license.gitlab
|
2018-11-06 22:59:25 +00:00
|
|
|
rm /tmp/license.gitlab
|
|
|
|
|
2019-04-09 11:12:49 +00:00
|
|
|
kubectl -n "$KUBE_NAMESPACE" exec -it "${task_runner_pod}" -- /srv/gitlab/bin/rails runner -e production \
|
2018-11-06 22:59:25 +00:00
|
|
|
'
|
|
|
|
content = File.read("/tmp/license.gitlab").strip;
|
|
|
|
FileUtils.rm_f("/tmp/license.gitlab");
|
|
|
|
|
|
|
|
unless License.where(data:content).empty?
|
|
|
|
puts "License already exists";
|
|
|
|
Kernel.exit 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
unless License.new(data: content).save
|
|
|
|
puts "Could not add license";
|
|
|
|
Kernel.exit 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "License added";
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_job_id() {
|
|
|
|
local job_name="${1}"
|
|
|
|
local query_string="${2:+&${2}}"
|
|
|
|
|
|
|
|
local max_page=3
|
|
|
|
local page=1
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs?per_page=100&page=${page}${query_string}"
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "GET ${url}"
|
2018-11-06 22:59:25 +00:00
|
|
|
|
2019-04-09 11:12:49 +00:00
|
|
|
local job_id
|
|
|
|
job_id=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq "map(select(.name == \"${job_name}\")) | map(.id) | last")
|
2018-12-03 15:52:53 +00:00
|
|
|
[[ "${job_id}" == "null" && "${page}" -lt "$max_page" ]] || break
|
2018-11-06 22:59:25 +00:00
|
|
|
|
2018-12-17 18:22:09 +00:00
|
|
|
let "page++"
|
2018-11-06 22:59:25 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "${job_id}" == "" ]]; then
|
|
|
|
echoerr "The '${job_name}' job ID couldn't be retrieved!"
|
|
|
|
else
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "The '${job_name}' job ID is ${job_id}"
|
2018-11-06 22:59:25 +00:00
|
|
|
echo "${job_id}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function play_job() {
|
|
|
|
local job_name="${1}"
|
2019-04-09 11:12:49 +00:00
|
|
|
local job_id
|
|
|
|
job_id=$(get_job_id "${job_name}" "scope=manual");
|
2018-11-06 22:59:25 +00:00
|
|
|
if [ -z "${job_id}" ]; then return; fi
|
|
|
|
|
|
|
|
local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/jobs/${job_id}/play"
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "POST ${url}"
|
2018-11-06 22:59:25 +00:00
|
|
|
|
2019-04-09 11:12:49 +00:00
|
|
|
local job_url
|
|
|
|
job_url=$(curl --silent --show-error --request POST --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".web_url")
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "Manual job '${job_name}' started at: ${job_url}"
|
2018-11-06 22:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wait_for_job_to_be_done() {
|
|
|
|
local job_name="${1}"
|
|
|
|
local query_string="${2}"
|
2019-04-09 11:12:49 +00:00
|
|
|
local job_id
|
|
|
|
job_id=$(get_job_id "${job_name}" "${query_string}")
|
2018-11-06 22:59:25 +00:00
|
|
|
if [ -z "${job_id}" ]; then return; fi
|
|
|
|
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "Waiting for the '${job_name}' job to finish..."
|
2018-11-06 22:59:25 +00:00
|
|
|
|
|
|
|
local url="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/jobs/${job_id}"
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "GET ${url}"
|
2018-11-06 22:59:25 +00:00
|
|
|
|
|
|
|
# In case the job hasn't finished yet. Keep trying until the job times out.
|
|
|
|
local interval=30
|
2018-12-03 15:52:53 +00:00
|
|
|
local elapsed_seconds=0
|
2018-11-06 22:59:25 +00:00
|
|
|
while true; do
|
2019-04-09 11:12:49 +00:00
|
|
|
local job_status
|
|
|
|
job_status=$(curl --silent --show-error --header "PRIVATE-TOKEN: ${API_TOKEN}" "${url}" | jq ".status" | sed -e s/\"//g)
|
2018-11-06 22:59:25 +00:00
|
|
|
[[ "${job_status}" == "pending" || "${job_status}" == "running" ]] || break
|
|
|
|
|
|
|
|
printf "."
|
2018-12-17 18:22:09 +00:00
|
|
|
let "elapsed_seconds+=interval"
|
2018-11-06 22:59:25 +00:00
|
|
|
sleep ${interval}
|
|
|
|
done
|
|
|
|
|
2018-12-03 15:52:53 +00:00
|
|
|
local elapsed_minutes=$((elapsed_seconds / 60))
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "Waited '${job_name}' for ${elapsed_minutes} minutes."
|
2018-11-06 22:59:25 +00:00
|
|
|
|
|
|
|
if [[ "${job_status}" == "failed" ]]; then
|
2019-01-24 11:23:58 +00:00
|
|
|
echoerr "The '${job_name}' failed."
|
2018-11-06 22:59:25 +00:00
|
|
|
elif [[ "${job_status}" == "manual" ]]; then
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "The '${job_name}' is manual."
|
2018-11-06 22:59:25 +00:00
|
|
|
else
|
2019-01-24 11:23:58 +00:00
|
|
|
echoinfo "The '${job_name}' passed."
|
2018-11-06 22:59:25 +00:00
|
|
|
fi
|
|
|
|
}
|