Files
cicd/common/templates/_pipeline.yaml

469 lines
16 KiB
YAML

{{- define "common.pipeline" }}
{{- $common := dict "Values" .Values.common -}}
{{- $noCommon := omit .Values "common" -}}
{{- $overrides := dict "Values" $noCommon -}}
{{- $noValues := omit . "Values" -}}
{{- with merge $noValues $overrides $common -}}
{{- $thisapp := include "common.name" . -}}
apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
labels:
app: {{ include "common.name" . }}
name: {{ include "common.name" . }}
namespace: {{ .Release.Namespace }}
spec:
params:
- description: Ref of the application
name: ref
type: string
- description: git unique head commit id
name: revision
type: string
- description: repository name
name: repourl
type: string
- description: repository full name
name: repofullname
type: string
- description: commit author username
name: author
type: string
- description: commit author email
name: email
type: string
- description: commit message
name: message
type: string
- description: pull request index number
name: index
type: string
- description: The URL of the repo containing the helm charts
name: git-helm-url
type: string
- description: the base git server hostname
name: githost
type: string
- description: the base quay server hostname
name: quayhost
type: string
tasks:
- name: set-pending-status
params:
- name: githost
value: $(params.githost)
- name: statusurl
value: https://console-openshift-console.apps.ocp.endofday.com/pipelines/ns/goghvideo-cicd-pipeline/pipeline-runs
- name: revision
value: $(params.revision)
- name: repofullname
value: $(params.repofullname)
- name: description
value: CI Pipeline
- name: state
value: pending
taskRef:
kind: Task
name: gitea-set-status
workspaces:
- name: gitauth
workspace: gitauth
- name: copy-shared-utilities
runAfter:
- set-pending-status
taskRef:
kind: Task
name: copy-shared-utilities
workspaces:
- name: utilities
- name: prepare
params:
- name: gitrepositoryurl
value: $(params.repourl)
- name: gitbranch
value: $(params.ref)
runAfter:
- set-pending-status
taskRef:
kind: Task
name: git-semver
workspaces:
- name: repo
workspace: source
- name: gitauth
workspace: gitauth
- name: standards
params:
- name: package
value: $(params.githost)/goghvideo/{{ include "common.name" . }}
- name: context
value: ./{{ include "common.name" . }}/src
- name: version
value: latest
runAfter:
- prepare
taskRef:
kind: Task
name: golangci-lint
workspaces:
- name: source
workspace: source
- name: build
params:
- name: TLSVERIFY
value: "false"
- name: BUILDER_IMAGE
value: {{ .Values.quayHostname }}/goghvideo/golang-s2i-buildah:v1
- name: PATH_CONTEXT
value: {{ include "common.name" . }}/src
- name: verbose
value: "true"
- name: IMAGE
value: {{ .Values.quayHostname }}/goghvideo/{{ include "common.name" . }}:$(params.revision)
- name: ENV_VARS
value:
- semver=$(tasks.prepare.results.version)
runAfter:
- standards
taskRef:
kind: Task
name: s2i-go
workspaces:
- name: source
workspace: source
- name: dockerconfig
workspace: dockerconfig
- name: ephemeral-namespace
params:
- name: VERSION
value: "4.17"
- name: SCRIPT
value: |
echo "${SHELL}"
RANDOMID=$(openssl rand -hex 4)
oc new-project goghvideo-test-${RANDOMID} >/dev/null
oc label namespace goghvideo-test-${RANDOMID} app=goghvideo-test
oc create -f - <<EOF
apiVersion: v1
data: {}
kind: ConfigMap
metadata:
labels:
config.openshift.io/inject-trusted-cabundle: "true"
name: config-trusted-cabundle
EOF
x=0; until [[ -n $(oc -n goghvideo-test-${RANDOMID} get secret/goghvideo-openshift-builder-pull-secret 2>/dev/null) || $x -eq 10 ]]; do echo "Waiting for secret replication" && sleep 10 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
oc -n goghvideo-test-${RANDOMID} secrets link default goghvideo-openshift-builder-pull-secret --for=pull
oc apply -f - <<EOF
apiVersion: rabbitmq.com/v1beta1
kind: RabbitmqCluster
metadata:
name: rabbitmq
namespace: goghvideo-test-${RANDOMID}
spec:
delayStartSeconds: 10
image: $(params.quayhost)/goghvideo/bitnami-rabbitmq
imagePullSecrets:
- name: goghvideo-openshift-builder-pull-secret
persistence:
storage: 10Gi
replicas: 1
resources:
limits:
cpu: "2"
memory: 2Gi
requests:
cpu: "1"
memory: 2Gi
service:
type: ClusterIP
terminationGracePeriodSeconds: 30
EOF
x=0; until [[ -n $(oc -n goghvideo-test-${RANDOMID} get sa/rabbitmq-server 2>/dev/null ) || $x -eq 10 ]]; do echo "Waiting for operator to deploy rabbit" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
oc -n goghvideo-test-${RANDOMID} adm policy add-scc-to-user anyuid -z rabbitmq-server
x=0; until [[ $(oc -n goghvideo-test-${RANDOMID} get sts rabbitmq-server -o jsonpath="{.status.readyReplicas}") -gt 0 || $x -eq 20 ]]; do echo "Waiting for Rabbit MQ to startup" && sleep 15 && ((x++)); done
if [[ "${x}" -eq 20 ]]; then exit 1; fi
oc -n goghvideo-test-${RANDOMID} create route edge --service=rabbitmq --port=15672
oc -n goghvideo-test-${RANDOMID} get pods
RABBITHOST=$(oc -n goghvideo-test-${RANDOMID} get secret/rabbitmq-default-user -o template='{{ "{{" }} .data.host | base64decode {{ "}}" }}')
RABBITUSER=$(oc -n goghvideo-test-${RANDOMID} get secret/rabbitmq-default-user -o template='{{ "{{" }} .data.username | base64decode {{ "}}" }}')
RABBITPASS=$(oc -n goghvideo-test-${RANDOMID} get secret/rabbitmq-default-user -o template='{{ "{{" }} .data.password | base64decode {{ "}}" }}')
oc -n goghvideo-test-${RANDOMID} create secret generic amqp --from-literal=url=amqp://${RABBITUSER}:${RABBITPASS}@${RABBITHOST}/
curl --retry 12 --retry-connrefused --retry-delay 10 -O http://${RABBITHOST}:15672/cli/rabbitmqadmin
chmod +x rabbitmqadmin
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare exchange name=conversion type=topic 2>/dev/null ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare queue name=transcode durable=true queue_type=quorum ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare queue name=notification durable=true queue_type=quorum ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare queue name=upload-nextcloud durable=true queue_type=quorum ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare queue name=complete durable=true queue_type=quorum ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare binding source="conversion" destination_type="queue" destination="transcode" routing_key="transcode" ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare binding source="conversion" destination_type="queue" destination="notification" routing_key="notification" ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare binding source="conversion" destination_type="queue" destination="upload-nextcloud" routing_key="upload-nextcloud" ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
x=0; until [[ -n $( ./rabbitmqadmin -H ${RABBITHOST} -u ${RABBITUSER} -p ${RABBITPASS} declare binding source="conversion" destination_type="queue" destination="complete" routing_key="complete" ) || $x -eq 10 ]]; do echo "waiting for exchange to be created" && sleep 5 && ((x++)); done
if [[ "${x}" -eq 10 ]]; then exit 1; fi
echo -n "goghvideo-test-${RANDOMID}" > $(results.namespace.path)
runAfter:
- build
taskRef:
kind: Task
name: openshift-client
workspaces:
- name: kubeconfig-dir
workspace: dockerconfig
- name: prepare-helm-charts
params:
- name: url
value: $(params.git-helm-url)
runAfter:
- ephemeral-namespace
taskRef:
kind: Task
name: git-clone
workspaces:
- name: output
workspace: helm
- name: basic-auth
workspace: gitauth
- name: deploy-notification-app
params:
- name: charts_dir
value: notification
- name: release_namespace
value: $(tasks.ephemeral-namespace.results.namespace)
- name: release_name
value: notification
{{- if eq $thisapp "notification" }}
- name: overwrite_values
value: image.tag=$(params.revision)
{{- end }}
runAfter:
- prepare-helm-charts
taskRef:
kind: Task
name: helm-upgrade-from-source
workspaces:
- name: source
workspace: helm
- name: deploy-upload-to-nextcloud-app
params:
- name: charts_dir
value: upload-to-nextcloud
- name: release_namespace
value: $(tasks.ephemeral-namespace.results.namespace)
- name: release_name
value: upload-to-nextcloud
{{- if eq $thisapp "upload-to-nextcloud" }}
- name: overwrite_values
value: image.tag=$(params.revision)
{{- end }}
runAfter:
- prepare-helm-charts
taskRef:
kind: Task
name: helm-upgrade-from-source
workspaces:
- name: source
workspace: helm
- name: deploy-conversion-engine-job
params:
- name: charts_dir
value: conversion-engine
- name: release_namespace
value: $(tasks.ephemeral-namespace.results.namespace)
- name: release_name
value: conversion-engine
{{- if eq $thisapp "conversion-engine" }}
- name: overwrite_values
value: image.tag=$(params.revision)
{{- end }}
runAfter:
- prepare-helm-charts
taskRef:
kind: Task
name: helm-upgrade-from-source
workspaces:
- name: source
workspace: helm
- name: get-filedrop-name
params:
- name: VERSION
value: "4.11"
- name: SCRIPT
value: |
#!/usr/bin/env bash
wget "https://github.com/jqlang/jq/releases/download/jq-1.7/jq-linux-amd64" -O /usr/local/bin/jq
chmod +x /usr/local/bin/jq
INPUTSRC=$(oc -n $(tasks.ephemeral-namespace.results.namespace) get scaledjob/conversion-engine -o json | /usr/local/bin/jq -r '.spec.jobTargetRef.template.spec.initContainers[0].env[] | select(.name == "sourcefile").value')
if [[ -z "${INPUTSRC}" ]]; then exit 1; fi
echo -n "${INPUTSRC}" > $(results.filedrop.path)
runAfter:
- deploy-conversion-engine-job
- deploy-upload-to-nextcloud-app
- deploy-notification-app
taskRef:
kind: Task
name: openshift-client
workspaces:
- name: kubeconfig-dir
workspace: dockerconfig
- name: perform-e2e-test
params:
- name: VERSION
value: "4.11"
- name: SCRIPT
value: |
#!/usr/bin/env bash
oc create -f - <<EOF && sleep 2
apiVersion: batch/v1
kind: Job
metadata:
generateName: e2e-test-
namespace: $(tasks.ephemeral-namespace.results.namespace)
labels:
app: e2e-test
spec:
completions: 1
parallelism: 1
template:
metadata:
creationTimestamp: null
spec:
restartPolicy: Never
containers:
- env:
- name: amqp
valueFrom:
secretKeyRef:
key: url
name: amqp
- name: dropfile
value: $(tasks.get-filedrop-name.results.filedrop)
image: $(params.quayhost)/goghvideo/e2e-test:v0.1.5
imagePullPolicy: Always
name: transcode-job
volumeMounts:
- mountPath: "/conversion"
name: "pvc-conversion"
volumes:
- name: "pvc-conversion"
persistentVolumeClaim:
claimName: "pvc-conversion"
EOF
MYJOB=$(oc -n $(tasks.ephemeral-namespace.results.namespace) get jobs -l app=e2e-test -o name)
echo "Job Reference: ${MYJOB}"
x=0; until [[ $(oc -n $(tasks.ephemeral-namespace.results.namespace) get ${MYJOB} -o go-template="{{ "{{" }}if .status.active{{ "}}{{" }}.status.active{{ "}}{{" }}else{{ "}}" }}0{{ "{{" }}end{{ "}}" }}") -gt 0 || $x -eq 60 ]]; do echo "Waiting for e2e job to start" && sleep 2 && ((x++)); done
if [[ "${x}" -eq 60 ]]; then exit 1; fi
x=0; until [[ $(oc -n $(tasks.ephemeral-namespace.results.namespace) get ${MYJOB} -o go-template="{{ "{{" }}if .status.succeeded{{ "}}{{" }}.status.succeeded{{ "}}{{" }}else{{ "}}" }}0{{ "{{" }}end{{ "}}" }}" ) -gt 0 || $x -eq 20 ]]; do echo "Waiting for test to complete" && sleep 15 && ((x++)); done
if [[ "${x}" -eq 20 ]]; then exit 1; fi
runAfter:
- get-filedrop-name
taskRef:
kind: Task
name: openshift-client
workspaces:
- name: kubeconfig-dir
workspace: dockerconfig
- name: set-success-status
params:
- name: githost
value: $(params.githost)
- name: quayhost
value: {{ .Values.quayHostname }}
- name: statusurl
value: https://console-openshift-console.apps.ocp.endofday.com/pipelines/ns/goghvideo-cicd-pipeline/pipeline-runs
- name: revision
value: $(params.revision)
- name: repofullname
value: $(params.repofullname)
- name: description
value: CI Pipeline
- name: state
value: success
runAfter:
- perform-e2e-test
taskRef:
kind: Task
name: gitea-set-status
workspaces:
- name: gitauth
workspace: gitauth
- name: promote
params:
- name: ref
value: $(params.ref)
- name: repofullname
value: $(params.repofullname)
- name: revision
value: $(params.revision)
- name: semver
value: $(tasks.prepare.results.version)
- name: author
value: $(params.author)
- name: email
value: $(params.email)
- name: appname
value: {{ include "common.name" . }}
- name: quayhost
value: {{ .Values.quayHostname }}
- name: githost
value: $(params.githost)
- name: index
value: $(params.index)
runAfter:
- set-success-status
taskRef:
kind: Task
name: promote
workspaces:
- name: source
workspace: source
- name: gitauth
workspace: gitauth
- name: quayauth
workspace: quayauth
- name: utilities
workspace: utilities
- name: helm
workspace: helm
workspaces:
- name: source
- name: gitauth
- name: dockerconfig
- name: helm
- name: gitsshauth
- name: quayauth
- name: utilities
{{- end }}
{{- end }}