From 381e52b4cb1ad3cae4cbd01be4fce4bb52818845 Mon Sep 17 00:00:00 2001 From: Ayesha Siddiqa Date: Wed, 20 Dec 2023 17:24:13 +0500 Subject: [PATCH 1/3] Added app-sync-and-wait task and workflow --- .../workflows/stakater_app_sync_and_wait.yaml | 123 ++++++++++++++++++ stakater-app-sync-and-wait/helm/Chart.yaml | 23 ++++ .../helm/templates/_helpers.tpl | 6 + .../helm/templates/clustertask.yaml | 116 +++++++++++++++++ stakater-app-sync-and-wait/helm/values.yaml | 0 5 files changed, 268 insertions(+) create mode 100644 .github/workflows/stakater_app_sync_and_wait.yaml create mode 100644 stakater-app-sync-and-wait/helm/Chart.yaml create mode 100644 stakater-app-sync-and-wait/helm/templates/_helpers.tpl create mode 100644 stakater-app-sync-and-wait/helm/templates/clustertask.yaml create mode 100644 stakater-app-sync-and-wait/helm/values.yaml diff --git a/.github/workflows/stakater_app_sync_and_wait.yaml b/.github/workflows/stakater_app_sync_and_wait.yaml new file mode 100644 index 00000000..05cdc538 --- /dev/null +++ b/.github/workflows/stakater_app_sync_and_wait.yaml @@ -0,0 +1,123 @@ +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 + + - name: Tilt CI - Setup Dependencies + run: tilt ci -f .github/Tiltfile-setup-dependencies + + - name: Tilt CI - Run Clustertask + env: + TEST_GIT_REPO_SECRET: ${{ secrets.TEKTON_CATALOG_ARC_CLUSTERTASK_TOKEN }} + # Token created with stakater. Expires on Thu, Jun 29 2023. + run: tilt ci -f stakater-app-sync-and-wait/tests/Tiltfile-clustertask + + - name: Tilt down - Clustertask + if: always() + run: tilt down -f stakater-app-sync-and-wait/tests/Tiltfile-delete-clustertask + + - name: Tilt down - Dependencies + if: always() + run: tilt down -f .github/Tiltfile-delete-dependencies --delete-namespaces + 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 }} diff --git a/stakater-app-sync-and-wait/helm/Chart.yaml b/stakater-app-sync-and-wait/helm/Chart.yaml new file mode 100644 index 00000000..9efd4fc4 --- /dev/null +++ b/stakater-app-sync-and-wait/helm/Chart.yaml @@ -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" \ No newline at end of file diff --git a/stakater-app-sync-and-wait/helm/templates/_helpers.tpl b/stakater-app-sync-and-wait/helm/templates/_helpers.tpl new file mode 100644 index 00000000..d2f9c5f1 --- /dev/null +++ b/stakater-app-sync-and-wait/helm/templates/_helpers.tpl @@ -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 }} diff --git a/stakater-app-sync-and-wait/helm/templates/clustertask.yaml b/stakater-app-sync-and-wait/helm/templates/clustertask.yaml new file mode 100644 index 00000000..efa7d3af --- /dev/null +++ b/stakater-app-sync-and-wait/helm/templates/clustertask.yaml @@ -0,0 +1,116 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +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 \ No newline at end of file diff --git a/stakater-app-sync-and-wait/helm/values.yaml b/stakater-app-sync-and-wait/helm/values.yaml new file mode 100644 index 00000000..e69de29b From 956a66ad4e02bc29a93d5d9b0fcac205f2aed1d1 Mon Sep 17 00:00:00 2001 From: Ayesha Siddiqa Date: Wed, 20 Dec 2023 17:38:13 +0500 Subject: [PATCH 2/3] updated kind of task --- stakater-app-sync-and-wait/helm/templates/clustertask.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stakater-app-sync-and-wait/helm/templates/clustertask.yaml b/stakater-app-sync-and-wait/helm/templates/clustertask.yaml index efa7d3af..dece2e3b 100644 --- a/stakater-app-sync-and-wait/helm/templates/clustertask.yaml +++ b/stakater-app-sync-and-wait/helm/templates/clustertask.yaml @@ -1,5 +1,5 @@ apiVersion: tekton.dev/v1beta1 -kind: Task +kind: ClusterTask metadata: name: stakater-app-sync-and-wait spec: From 7905d3cc68f36345edadc2878f7ce298c8c575b4 Mon Sep 17 00:00:00 2001 From: Ayesha Siddiqa Date: Wed, 20 Dec 2023 18:21:31 +0500 Subject: [PATCH 3/3] removed dynamic tests part from job --- .../workflows/stakater_app_sync_and_wait.yaml | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/.github/workflows/stakater_app_sync_and_wait.yaml b/.github/workflows/stakater_app_sync_and_wait.yaml index 05cdc538..7a18b0d5 100644 --- a/.github/workflows/stakater_app_sync_and_wait.yaml +++ b/.github/workflows/stakater_app_sync_and_wait.yaml @@ -55,22 +55,6 @@ jobs: - name: Install tilt run: curl -fsSL https://raw.githubusercontent.com/tilt-dev/tilt/master/scripts/install.sh | bash - - name: Tilt CI - Setup Dependencies - run: tilt ci -f .github/Tiltfile-setup-dependencies - - - name: Tilt CI - Run Clustertask - env: - TEST_GIT_REPO_SECRET: ${{ secrets.TEKTON_CATALOG_ARC_CLUSTERTASK_TOKEN }} - # Token created with stakater. Expires on Thu, Jun 29 2023. - run: tilt ci -f stakater-app-sync-and-wait/tests/Tiltfile-clustertask - - - name: Tilt down - Clustertask - if: always() - run: tilt down -f stakater-app-sync-and-wait/tests/Tiltfile-delete-clustertask - - - name: Tilt down - Dependencies - if: always() - run: tilt down -f .github/Tiltfile-delete-dependencies --delete-namespaces next-version: uses: stakater/tekton-catalog/.github/workflows/next_version.yaml@main with: