Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

SA-2007: Added app-sync-and-wait task and workflow #261

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/workflows/stakater_app_sync_and_wait.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Stakater app-sync-and-wait as user cluster task Helm Chart
on:
pull_request:
branches:
- main
paths:
- 'stakater-app-sync-and-wait/**'

push:
branches:
- main
paths:
- 'stakater-app-sync-and-wait/**'

jobs:
clustertask-test-run:
name: ClusterTask Test Run on SNO
runs-on: self-hosted
if: github.event_name == 'pull_request' && github.ref != 'refs/heads/main'

steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{github.event.pull_request.head.sha}}

- name: Install CLI tools from OpenShift Mirror
uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4"

# This is used to setup kubeconfig, required by Tilt
- name: Login to cluster
run: oc login --token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) --server=https://kubernetes.default.svc --insecure-skip-tls-verify=true

- name: Set up Helm
uses: azure/setup-helm@v3
with:
version: v3.8.2

# This is required for login to ghcr helm registry
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io/stakater
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Install kubectl
uses: azure/setup-kubectl@v3
with:
version: v1.26.0

- name: Install tilt
run: curl -fsSL https://raw.githubusercontent.com/tilt-dev/tilt/master/scripts/install.sh | bash

next-version:
uses: stakater/tekton-catalog/.github/workflows/next_version.yaml@main
with:
component: stakater-app-sync-and-wait

pull-request:
uses: stakater/tekton-catalog/.github/workflows/pull_request.yaml@main
name: Pull Request
if: ${{ github.ref != 'refs/heads/main' }}
needs:
- next-version
secrets:
REGISTRY_USERNAME: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
with:
CONTAINER_REGISTRY_URL: ghcr.io/stakater
HELM_REGISTRY_URL: oci://ghcr.io/stakater/charts
SUBPATH: stakater-app-sync-and-wait
PUSH_CONTAINER_IMAGE: ${{ needs.next-version.outputs.dockerfile_exists }}
PUBLISH_HEM_CHART: ${{ needs.next-version.outputs.helm_chart_exists }}
NEXT_VERSION: ${{ needs.next-version.outputs.version }}

push-and-release:
uses: stakater/tekton-catalog/.github/workflows/push.yaml@main
name: Push and Release
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
needs:
- next-version
secrets:
REGISTRY_USERNAME: ${{ github.actor }}
REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
with:
CONTAINER_REGISTRY_URL: ghcr.io/stakater
HELM_REGISTRY_URL: oci://ghcr.io/stakater/charts
SUBPATH: stakater-app-sync-and-wait
PUSH_CONTAINER_IMAGE: ${{ needs.next-version.outputs.dockerfile_exists }}
PUBLISH_HEM_CHART: ${{ needs.next-version.outputs.helm_chart_exists }}
NEXT_VERSION: ${{ needs.next-version.outputs.version }}

render-and-replace:
uses: stakater/tekton-catalog/.github/workflows/render_task.yaml@main
name: Render and Replace
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
needs:
- next-version
secrets:
STAKATER_GITHUB_TOKEN: ${{ secrets.STAKATER_GITHUB_TOKEN }}
with:
SUBPATH: stakater-app-sync-and-wait
NEXT_VERSION: ${{ needs.next-version.outputs.version }}
23 changes: 23 additions & 0 deletions stakater-app-sync-and-wait/helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v2
name: stakater-app-sync-and-wait
description: A Helm chart for Tekton Clustertask

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.0
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.0.0"
6 changes: 6 additions & 0 deletions stakater-app-sync-and-wait/helm/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "stakater-app-sync-and-wait.name" -}}
{{- default .Chart.Name | trunc 63 | trimSuffix "-" }}-{{ .Chart.Version }}
{{- end }}
116 changes: 116 additions & 0 deletions stakater-app-sync-and-wait/helm/templates/clustertask.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
apiVersion: tekton.dev/v1beta1
kind: ClusterTask
metadata:
name: stakater-app-sync-and-wait
spec:
description: >-
This task syncs application and waits for it to be healthy.
params:
- description: Reference of the image tag.
name: IMAGE_TAG
type: string
- description: Reference of the image name.
name: IMAGE_NAME
type: string
- name: REPO_NAME
description: Name of the repository
- name: TIMEOUT
description: Timeout for waiting for pod
- name: ARGO_APP_PREFIX
description: Argocd applocation name prefix
default: "NA"
- name: ARGO_PREVIEW_APP_PREFIX
description: Argocd preview application name prefix
default: "NA"
- name: PR_NUMBER
description: In case of PR, PR number that is to be used in image tag. If this field is empty it means that it's a commit on main branch
default: "NA"
- name: GIT_REPOSITORY_URL
description: The git repository url
workspaces:
- name: source
steps:
- name: argo-sync-and-wait
image: docker.io/argoproj/argocd:v2.2.5
env:
- name: ARGOCD_SERVER
valueFrom:
secretKeyRef:
name: argocd-cluster
key: server
- name: ARGOCD_USERNAME
valueFrom:
secretKeyRef:
name: argocd-cluster
key: username
- name: ARGOCD_PASSWORD
valueFrom:
secretKeyRef:
name: argocd-cluster
key: password
script: |
if [ $(params.PR_NUMBER = "NA" ] && [ ! -f pom.xml ]; then
argocd login "$ARGOCD_SERVER" --username="$ARGOCD_USERNAME" --password="$ARGOCD_PASSWORD"
argocd app sync "$(params.ARGO_APP_PREFIX)-$(params.REPO_NAME)"
argocd app wait "$(params.ARGO_APP_PREFIX)-$(params.REPO_NAME)" --health
fi
- image: 'stakater/pipeline-toolbox:v0.0.11'
name: sync-and-wait
env:
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: github-stakater-tekton-bot
key: password
- name: ARGOCD_SERVER
valueFrom:
secretKeyRef:
name: argocd-cluster
key: server
- name: ARGOCD_USERNAME
valueFrom:
secretKeyRef:
name: argocd-cluster
key: username
- name: ARGOCD_PASSWORD
valueFrom:
secretKeyRef:
name: argocd-cluster
key: password
workingDir: $(workspaces.source.path)
script: |
if [ $(params.PR_NUMBER != "NA" ] && [ ! -f pom.xml ]; then
argocd login "$ARGOCD_SERVER" --username="$ARGOCD_USERNAME" --password="$ARGOCD_PASSWORD"
argocd app get "$(params.ARGO_PREVIEW_APP_PREFIX)-$(params.REPO_NAME)" --refresh
while [[ "$SECONDS" -lt $(params.TIMEOUT) ]]
do
for pod in $(oc get pod -n pr-$(params.PR_NUMBER-$(params.REPO_NAME) -o=jsonpath={.items..metadata.name});
do podImage=$(oc get pod $pod -n pr-$(params.PR_NUMBER-$(params.REPO_NAME) -o jsonpath={.spec.containers[*].image});
image=$(params.IMAGE_NAME):$(params.IMAGE_TAG)
if [ $podImage == $image ]; then
readyStatus=$(oc get pods $pod -n pr-$(params.PR_NUMBER-$(params.REPO_NAME) -o jsonpath='{.status.containerStatuses[*].ready}')
break;
fi
done
if [ $readyStatus == true ]; then
echo “pod is ready”

# commenting app url on repository
REPO_URL=$(params.GIT_REPOSITORY_URL)
#Remove protocol
REPO_NAME=${REPO_URL#*//}
#Remove baseurl
REPO_NAME=${REPO_NAME#*/}
URL=$(oc get route -n pr-$(params.PR_NUMBER-$(params.REPO_NAME) -l app=$(yq .application.applicationName deploy/values.yaml) -o jsonpath='{range .items[*]}{.spec.host}')
echo $URL
curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
-X POST -d '{"body": "Tekton - Pipeline ran successfully and url https://'$URL' is available"}' \
"https://api.github.com/repos/${REPO_NAME}/issues/$(params.PR_NUMBER/comments"

break;
fi
echo “waiting for pod” && sleep 3
done
else
echo "Not a PR, wont wait for the pods"
fi
Empty file.
Loading