22# Assumes User is logged in to cluster
33set -euo pipefail
44
5+ app_name=" basic-python-tekton"
6+ app_namespace=" basic-python-tekton"
7+
58Help ()
69{
710 # Display Help
811 echo " Execute a tekton pipeline with various build types."
912 echo
1013 echo " Syntax: scriptTemplate [-h|g|b|n]"
1114 echo " options:"
15+ echo " a application name, default ${app_name} "
16+ echo " n namespace to be used, default ${app_namespace} "
1217 echo " g the git url"
1318 echo " r git branch reference, use this for Pull Requests. e.g. refs/pull/587/head"
1419 echo " h Print this Help."
1520 echo " b build type [buildconfig, binary, s2i]"
16- echo " n If set then no human interaction is required for subsequent deployments"
17- echo " t Time in seconds to sleep between subsequent deployments, default 300 (no human only)"
18- echo " c Number of deployments, default 1 (no human only)"
21+ echo " t Time in seconds to sleep between subsequent deployments, default 300 (enables non-interaction mode)"
22+ echo " c Number of deployments, default 1 (enables non-interaction mode)"
1923 echo
2024}
2125
@@ -29,7 +33,7 @@ sleep_between=300
2933no_deployments=1
3034
3135# Get the options
32- while getopts " :hg:b:r:nt:c :" option; do
36+ while getopts " :hg:b:r:n:t:c:a :" option; do
3337 case $option in
3438 h) # display Help
3539 Help
@@ -40,12 +44,16 @@ while getopts ":hg:b:r:nt:c:" option; do
4044 current_branch=$OPTARG ;;
4145 b) # Enter the build type
4246 build_type=$OPTARG ;;
43- n) # No human interaction
44- NO_HUMAN=true;;
4547 t) # Sleep between subsequent deployments
46- sleep_between=$OPTARG ;;
48+ sleep_between=$OPTARG && \
49+ NO_HUMAN=true;;
4750 c) # Number of subsequent deployments
48- no_deployments=$OPTARG ;;
51+ no_deployments=$OPTARG && \
52+ NO_HUMAN=true;;
53+ a) # Application name
54+ app_name=$OPTARG ;;
55+ n) # Application namespace
56+ app_namespace=$OPTARG ;;
4957\? ) # Invalid option
5058 echo " Error: Invalid option"
5159 exit ;;
@@ -59,12 +67,19 @@ if [[ $(git status --porcelain --untracked-files=no) ]]; then
5967fi
6068
6169echo " ============================"
62- echo " Executing the basic-python-tekton demo for Pelorus..."
70+ echo " Executing the ${app_name} demo for Pelorus..."
6371echo " "
6472echo " *** Current Options used ***"
73+ echo " App name: ${app_name} "
74+ echo " Used namespace: ${app_namespace} "
6575echo " Git URL: $url "
6676echo " Git ref: $current_branch "
6777echo " Build Type: $build_type "
78+ echo " No interaction mode: ${NO_HUMAN} "
79+ if [ " ${NO_HUMAN} " == true ]; then
80+ echo " Number of deployments: ${no_deployments} "
81+ echo " Time between deployments: ${sleep_between} [s]"
82+ fi
6883echo " ============================"
6984echo " "
7085
@@ -77,57 +92,57 @@ for cmd in oc tkn; do
7792done
7893if ! [[ $all_cmds_found ]]; then exit 1; fi
7994
80-
8195tekton_setup_dir=" $( dirname " ${BASH_SOURCE[0]} " ) /tekton-demo-setup"
8296python_example_txt=" $( dirname " ${BASH_SOURCE[0]} " ) /python-example/response.txt"
8397
84- echo " Switch to the basic-python-tekton namespace"
85- oc get ns basic-python-tekton || oc create ns basic-python-tekton
86- oc project basic-python-tekton
98+ # Create namespace if one doesn't exist
99+ if oc get namespace " ${app_namespace} " > /dev/null 2>&1 ; then
100+ echo " 1. Namespace '${app_namespace} ' already exists"
101+ else
102+ echo " 1. Create namespace: ${app_namespace} "
103+ oc process -f " $tekton_setup_dir /01-new-project-request_template.yaml" -p PROJECT_NAME=" ${app_namespace} " | oc create -f -
104+ fi
87105
88106echo " Clean up resources prior to execution:"
89107# cleaning resources vs. deleting the namespace to preserve pipeline run history
90108# resources are cleaned to ensure that the new running artifact is from the latest build
91- oc delete --all imagestream -n basic-python-tekton & > /dev/null || true
92- oc scale dc/basic-python-tekton --replicas=0 & > /dev/null || true
93- oc delete dc/basic-python-tekton -n basic-python-tekton & > /dev/null || true
94- oc delete buildConfig basic-python-tekton & > /dev/null || true
95- oc delete buildconfig.build.openshift.io/basic-python-tekton & > /dev/null || true
96- oc delete -all pods -n basic-python-tekton & > /dev/null || true
97- oc delete --all replicationcontroller -n basic-python-tekton & > /dev/null || true
109+ oc delete --all imagestream -n " ${app_namespace} " & > /dev/null || true
110+ oc scale " dc/${app_name} " --replicas=0 -n " ${app_namespace} " & > /dev/null || true
111+ oc delete " dc/${app_name} " -n " ${app_namespace} " & > /dev/null || true
112+ oc delete buildConfig " ${app_name} " -n " ${app_namespace} " & > /dev/null || true
113+ oc delete " buildconfig.build.openshift.io/${app_name} " -n " ${app_namespace} " & > /dev/null || true
114+ oc delete --all pods -n " ${app_namespace} " & > /dev/null || true
115+ oc delete --all replicationcontroller -n " ${app_namespace} " & > /dev/null || true
116+ oc delete --all template.template.openshift.io -n " ${app_namespace} " & > /dev/null || true
117+ oc delete " clusterrolebinding.rbac.authorization.k8s.io/pipeline-role-binding-${app_namespace} " & > /dev/null || true
98118
99119echo " Setting up resources:"
100120
101- echo " 1 . Installing tekton operator"
102- oc apply -f " $tekton_setup_dir /01 -tekton-operator.yaml"
121+ echo " 2 . Installing tekton operator"
122+ oc apply -f " $tekton_setup_dir /02 -tekton-operator.yaml"
103123
104- echo " 2. Setting up python tekton project"
105- if ! project_setup_output=" $( oc apply -f " $tekton_setup_dir /02-project.yaml" 2>&1 ) " ; then
106- if echo " $project_setup_output " | grep -q " AlreadyExists" ; then
107- echo " Project already exists"
108- else
109- echo " $project_setup_output " >&2
110- exit 1
111- fi
112- else
113- echo " $project_setup_output "
114- fi
124+ echo " 3. Creating pipeline-role ClusterRole"
125+ oc apply -f " $tekton_setup_dir /03-rbac-pipeline-role.yaml"
115126
127+ echo " 4. Creating ClusterRoleBinding with ServiceAccount"
128+ oc process -f " $tekton_setup_dir /04-service-account_template.yaml" -p PROJECT_NAMESPACE=" ${app_namespace} " -n default | oc create -f -
116129
117- echo " 3 . Setting up build and deployment information"
118- oc process -f " $tekton_setup_dir /03 -build-and-deploy.yaml" > /tmp/03 -build-and-deploy.yaml.out 2> /tmp/03 -build-and-deploy.yaml.err
119- oc apply -f /tmp/03 -build-and-deploy.yaml.out
130+ echo " 5 . Setting up build and deployment information"
131+ oc process -f " $tekton_setup_dir /05 -build-and-deploy.yaml" -p NAMESPACE= " ${app_namespace} " -p APPLICATION_NAME= " ${app_name} " -n default > /tmp/05 -build-and-deploy.yaml.out 2> /tmp/05 -build-and-deploy.yaml.err
132+ oc apply -n " ${app_namespace} " - f /tmp/05 -build-and-deploy.yaml.out
120133
121- route=" $( oc get -n basic-python-tekton route/basic-python-tekton --output=go-template=' http://{{.spec.host}}' ) "
134+ route=$( oc get -n " ${app_namespace} " " route/${app_name} " --output=go-template=' http://{{.spec.host}}' )
122135
123136counter=1
124137
125138function run_pipeline {
126- tkn pipeline start -n basic-python-tekton --showlog basic-python-tekton-pipeline \
127- -w name=repo,claimName=basic-python-tekton-build-pvc \
139+ set -x
140+ tkn pipeline start -n " ${app_namespace} " --showlog " ${app_name} -pipeline" \
141+ -w name=repo,claimName=" ${app_name} -build-pvc" \
128142 -p git-url=" $url " -p git-revision=" $current_branch " \
129- -l app.kubernetes.io/name=basic-python-tekton \
143+ -l app.kubernetes.io/name=" ${app_name} " \
130144 -p BUILD_TYPE=" $build_type "
145+ set +x
131146}
132147
133148echo -e " \nRunning pipeline\n"
0 commit comments