diff --git a/.circleci/config.yml b/.circleci/config.yml index 98e150c..4d9016e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -203,6 +203,9 @@ jobs: floodgate_extra_params: type: string default: "" + pipelines_to_trigger: + type: string + default: "" machine: image: ubuntu-1604:202004-01 steps: @@ -252,6 +255,10 @@ jobs: /floodgate/bin/floodgate << parameters.floodgate_extra_params >> --config ~/floodgate.yaml sync echo "Compare changes - synced resources" /floodgate/bin/floodgate << parameters.floodgate_extra_params >> --config ~/floodgate.yaml compare + - run: + name: Trigger pipelines + command: | + .circleci/libs/trigger-pipelines.sh << parameters.pipelines_to_trigger >> workflows: periodic: @@ -337,5 +344,6 @@ workflows: - start_spinnaker: name: test floodgate with working spinnaker floodgate_extra_params: "-q" + pipelines_to_trigger: "deploy_nginx" requires: - build diff --git a/.circleci/libs/install-and-run-spinnaker.sh b/.circleci/libs/install-and-run-spinnaker.sh index d22373d..ec52c01 100755 --- a/.circleci/libs/install-and-run-spinnaker.sh +++ b/.circleci/libs/install-and-run-spinnaker.sh @@ -3,6 +3,10 @@ EXEC_DIR=$(dirname "$0") HAL_VERSION=${HAL_VERSION:-1.35.0} +# Install packages +sudo apt update +sudo apt install -y jq + # Install Halyard curl -O https://raw.githubusercontent.com/spinnaker/halyard/master/install/debian/InstallHalyard.sh USERNAME=`whoami` @@ -22,6 +26,10 @@ GATE_PASS=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 32 ; echo '') hal -q config provider kubernetes enable CONTEXT=$(kubectl config current-context) hal -q config provider kubernetes account add my-k8s-v2-account --provider-version v2 --context $CONTEXT +## Configure account for inner kind communication +cp ~/.kube/config ~/.kube/kind +sed -i "s/server:\ .*/server:\ https:\/\/10.96.0.1:443/g" ~/.kube/kind +hal -q config provider kubernetes account add inner-kind --provider-version v2 --context $CONTEXT --kubeconfig-file ~/.kube/kind hal -q config deploy edit --type distributed --account-name my-k8s-v2-account ## Install minio diff --git a/.circleci/libs/trigger-pipelines.sh b/.circleci/libs/trigger-pipelines.sh new file mode 100755 index 0000000..d7686ea --- /dev/null +++ b/.circleci/libs/trigger-pipelines.sh @@ -0,0 +1,53 @@ +#!/bin/bash -e +for PIPELINE in $@ ; do + + PASS=`cat ~/.hal/default/profiles/gate-local.yml | grep password` + PASS=${PASS#*:\ } + USER=`cat ~/.hal/default/profiles/gate-local.yml | grep name` + USER=${USER#*:\ } + ALL_APPS=`curl -s -X GET --user "$USER:$PASS" "http://spinnaker/api/v1/applications" | jq -r .[].name` + MAX_ATTEMPTS=20 + + echo "Triggering pipeline with source $PIPELINE" + EVENT_ID=`curl -s -X POST -H "content-type: application/json" -d "{ }" http://spinnaker/api/v1/webhooks/webhook/$PIPELINE | jq -r .eventId` + echo "eventId: $EVENT_ID" + + for APP in $ALL_APPS ; do + PIPELINE_NAME=`curl -s -X GET --user "$USER:$PASS" "http://spinnaker/api/v1/applications/$APP/executions/search?triggerTypes=webhook&eventId=$EVENT_ID" | jq -r .[].name` + ATTEMPTS=0 + + while [[ $PIPELINE_NAME != "" ]] && [ $ATTEMPTS -lt $MAX_ATTEMPTS ] ; do + echo "Checking pipeline $PIPELINE_NAME status" + STATUS=`curl -s -X GET --user "$USER:$PASS" "http://spinnaker/api/v1/applications/$APP/executions/search?triggerTypes=webhook&eventId=$EVENT_ID" | jq -r .[].status` + + case $STATUS in + + "NOT_STARTED") + echo "Waiting for pipeline $PIPELINE_NAME to start" + sleep 3 + ;; + + "RUNNING") + echo "Waiting for pipeline $PIPELINE_NAME to finish" + sleep 3 + ;; + + "SUCCEEDED") + echo "$Pipeline PIPELINE_NAME succeded" + break + ;; + + *) + echo "Pipeline $PIPELINE_NAME exited with status $STATUS" + exit 1 + ;; + esac + ((++ATTEMPTS)) + done + + if [ $ATTEMPTS -ge $MAX_ATTEMPTS ] ; then + echo "Check timed out" + exit 1 + fi + done +done diff --git a/examples/resources/pipelines/deployNginx.json b/examples/resources/pipelines/deployNginx.json new file mode 100644 index 0000000..4a3730c --- /dev/null +++ b/examples/resources/pipelines/deployNginx.json @@ -0,0 +1,36 @@ +{ + "apiVersion": "apps/v1", + "kind": "Deployment", + "metadata": { + "name": "nginx-deployment" + }, + "spec": { + "replicas": 1, + "selector": { + "matchLabels": { + "app": "nginx" + } + }, + "template": { + "metadata": { + "labels": { + "app": "nginx", + "lb": "nginx" + } + }, + "spec": { + "containers": [ + { + "name": "nginx", + "image": "nginx:latest", + "ports": [ + { + "containerPort": 80 + } + ] + } + ] + } + } + } +} diff --git a/examples/resources/pipelines/deployNginx.jsonnet b/examples/resources/pipelines/deployNginx.jsonnet new file mode 100644 index 0000000..08c0ad9 --- /dev/null +++ b/examples/resources/pipelines/deployNginx.jsonnet @@ -0,0 +1,27 @@ +local pipelines = import 'pipeline.libsonnet'; + +local deployment = import 'deployNginx.json'; + +local app = "jsonnetapp"; +local webhookTrigger(name, source) = pipelines.triggers + .webhook(name) + .withSource(source); + +local moniker = pipelines.moniker(app); + +local deployManifest (env, manifest = deployment) = pipelines.stages + .deployManifest('Deploy nginx to ' + env) + .withAccount('inner-kind') + .withManifests(manifest) + .withNamespaceOverride(env) + .withMoniker(moniker); + +local deployNginx = deployManifest('spinnaker'); +local triggerPipeline = webhookTrigger("deploy_nginx", "deploy_nginx"); + +pipelines.pipeline() +.withName('Test Deployment') +.withId('test_deployment') +.withApplication(app) +.withStages(deployNginx) +.withTriggers(triggerPipeline)