diff --git a/.github/workflows/pr-close.yaml b/.github/workflows/pr-close.yaml index 0e6a3fb..cf19868 100644 --- a/.github/workflows/pr-close.yaml +++ b/.github/workflows/pr-close.yaml @@ -7,31 +7,41 @@ jobs: pr-close: runs-on: ubuntu-latest + + permissions: + issues: write + pull-requests: write + contents: write steps: - - name: downcase REPO - run: | - echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV} - - - name: Checkout + - name: Checkout Preview Environments Project uses: actions/checkout@v3 + with: + repository: cloudnativeentrepreneur/example-preview-envs + persist-credentials: false - - name: test + - name: Commit Preview Env Changes run: | - echo "Test" + rm helm/templates/${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}.yaml + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add . + git commit -m "remove: ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}" - # - name: Setup Kube config - # uses: azure/k8s-set-context@v3 - # with: - # method: kubeconfig - # kubeconfig: ${{ secrets.KUBE_CONFIG }} + - name: Push Preview Env Changes + uses: ad-m/github-push-action@master + with: + github_token: ${{ secrets.GH_ORG_TOKEN }} + repository: cloudnativeentrepreneur/example-preview-envs - # - name: Delete preview - # run: | - # # Install vCluster - # curl -s -L "https://github.com/loft-sh/vcluster/releases/latest" | sed -nE 's!.*"([^"]*vcluster-linux-amd64)".*!https://github.com\1!p' | xargs -n 1 curl -L -o vcluster && chmod +x vcluster; - # sudo mv vcluster /usr/local/bin; + - name: Create comment about garbage collection + uses: peter-evans/create-or-update-comment@v2 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + Your preview environment has been removed. It will be garbage collected soon. - # # Delete the cluster - # vcluster delete demo-hasura-pr-${{ github.event.pull_request.number }} --namespace prs + To create a new preview environment, open a new pull request. + edit-mode: replace \ No newline at end of file diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index d7854b1..9bde9fd 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -3,6 +3,8 @@ name: pr on: pull_request: + branches: + - main jobs: @@ -15,21 +17,32 @@ jobs: # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: - - uses: actions/checkout@v2 + + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} cache: 'npm' - - run: npm ci - - run: npm run lint --if-present - - run: npm run build --if-present - - run: npm test - - run: npx codecov + - name: Run Npm Quality Scripts + run: | + npm ci + npm run lint --if-present + npm run build --if-present + npm run test --if-present + npx codecov preview: - needs: quality + # needs: quality runs-on: ubuntu-latest + + permissions: + packages: write + contents: write + issues: write + pull-requests: write + steps: - name: downcase REPO @@ -65,34 +78,76 @@ jobs: push: true tags: | ${{ steps.meta.outputs.tags }} + ghcr.io/${{ env.REPO }}:pr-${{ github.event.pull_request.number }}-${{ github.sha }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache cache-to: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache,mode=max + + - name: Setup Helm + uses: azure/setup-helm@v3 - # - name: Setup Kube config - # uses: azure/k8s-set-context@v3 - # with: - # method: kubeconfig - # kubeconfig: ${{ secrets.KUBE_CONFIG }} - - # - name: Deploy preview - # run: | - # # Install vCluster - # curl -s -L "https://github.com/loft-sh/vcluster/releases/latest" | sed -nE 's!.*"([^"]*vcluster-linux-amd64)".*!https://github.com\1!p' | xargs -n 1 curl -L -o vcluster && chmod +x vcluster; - # sudo mv vcluster /usr/local/bin; - - # # Create a cluster - # vcluster create example-hasura-pr-${{ github.event.pull_request.number }} --namespace prs --expose - # vcluster connect example-hasura-pr-${{ github.event.pull_request.number }} --namespace prs - # export KUBECONFIG=$PWD/kubeconfig.yaml - # sleep 5 - # kubectl get ingresses - - # # Deploy - # cd kustomize/overlays/preview - # kustomize edit set image ${{ env.REPO }}=${{ env.REPO }}:pr-${{ github.event.pull_request.number }} - # kustomize build | kubectl apply --filename - - # kubectl rollout status deployment example-hasura - # - name: Test - # run: | - # echo "I was to lazy to write tests" + - name: Checkout Preview Environments Project + uses: actions/checkout@v3 + with: + path: previews + repository: cloudnativeentrepreneur/example-preview-envs + persist-credentials: false + + - name: Create Preview from Helm template + run: | + mkdir -p previews/helm/templates + helm template preview/helm/ \ + --set pr=pr-${{ github.event.pull_request.number }} \ + --set tag=pr-${{ github.event.pull_request.number }}-${{ github.sha }} \ + --set headRef=${{ github.head_ref }} \ + --set repository.name=${{ github.event.repository.name }} \ + > previews/helm/templates/${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}.yaml + + - name: Commit Preview Env Changes + run: | + cd previews + + if output=$(git status --porcelain) && [ -z "$output" ]; then + # Working directory clean + + echo "No changes to commit" + else + # Uncommitted changes + + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add -A + git commit -m "promote: ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }} ${{ github.sha }}" + fi + + - name: Push Preview Env Changes + uses: ad-m/github-push-action@master + with: + directory: previews + github_token: ${{ secrets.GH_ORG_TOKEN }} + repository: cloudnativeentrepreneur/example-preview-envs + + - name: Find Comment + uses: peter-evans/find-comment@v2 + id: fc + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: Your preview environment + + - name: Create or update comment + uses: peter-evans/create-or-update-comment@v2 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + Your preview environment has been published! :rocket: + + This service doesn't have a public URL. + + You can verify the PR is ready with `kubectl`: + + ```bash + kubectl get all -n ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview + ``` + edit-mode: replace diff --git a/.github/workflows/publish-and-promote.yaml b/.github/workflows/publish-and-promote.yaml index 8f568a8..9b23805 100644 --- a/.github/workflows/publish-and-promote.yaml +++ b/.github/workflows/publish-and-promote.yaml @@ -51,7 +51,7 @@ jobs: context: . push: true tags: | - ghcr.io/${{ steps.meta.outputs.tags }} + ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache cache-to: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache,mode=max @@ -78,10 +78,18 @@ jobs: - name: Commit Prod Env Changes run: | - git config --local user.email "github-actions[bot]@users.noreply.github.com" - git config --local user.name "github-actions[bot]" - git add . - git commit -m "feat: Promoting ${{ github.event.repository.name }} to ${{ github.ref_name }}" + if output=$(git status --porcelain) && [ -z "$output" ]; then + # Working directory clean + + echo "No changes to commit" + else + # Uncommitted changes + + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add -A + git commit -m "feat: Promoting ${{ github.event.repository.name }} to ${{ github.ref_name }}" + fi - name: Push prod uses: ad-m/github-push-action@master diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index ba1583c..6809876 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -23,7 +23,7 @@ jobs: - run: npm ci - run: npm run lint --if-present - run: npm run build --if-present - - run: npm test + - run: npm run test --if-present - run: npx codecov release: diff --git a/helm/Chart.yaml b/helm/Chart.yaml index 57b1994..91acca4 100644 --- a/helm/Chart.yaml +++ b/helm/Chart.yaml @@ -2,4 +2,4 @@ apiVersion: v1 description: A Helm chart for Kubernetes icon: https://raw.githubusercontent.com/jenkins-x/jenkins-x-platform/d273e09/images/nodejs.png name: example-hasura-projections-service -version: dev +version: 0.0.0-dev diff --git a/helm/Kptfile b/helm/Kptfile deleted file mode 100755 index 8c254a2..0000000 --- a/helm/Kptfile +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: kpt.dev/v1alpha1 -kind: Kptfile -metadata: - name: charts -upstream: - type: git - git: - commit: e95d99e4f74847a5eed092d3799fb7cfed5d0eac - repo: https://github.com/jenkins-x/jx3-pipeline-catalog - directory: /helm/charts - ref: master diff --git a/helm/README.md b/helm/README.md deleted file mode 100755 index 83de828..0000000 --- a/helm/README.md +++ /dev/null @@ -1 +0,0 @@ -# Javascript application \ No newline at end of file diff --git a/helm/templates/NOTES.txt b/helm/templates/NOTES.txt deleted file mode 100755 index 97823be..0000000 --- a/helm/templates/NOTES.txt +++ /dev/null @@ -1,4 +0,0 @@ - -Get the application URL by running these commands: - -kubectl get ingress {{ template "fullname" . }} diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl deleted file mode 100755 index f0d83d2..0000000 --- a/helm/templates/_helpers.tpl +++ /dev/null @@ -1,16 +0,0 @@ -{{/* vim: set filetype=mustache: */}} -{{/* -Expand the name of the chart. -*/}} -{{- define "name" -}} -{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} -{{- end -}} - -{{/* -Create a default fully qualified app name. -We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). -*/}} -{{- define "fullname" -}} -{{- $name := default .Chart.Name .Values.nameOverride -}} -{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} -{{- end -}} diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml deleted file mode 100644 index 57e9ef2..0000000 --- a/helm/templates/deployment.yaml +++ /dev/null @@ -1,52 +0,0 @@ -{{- if .Values.knative.eventing.local }} -{{- else }} -{{- if .Values.knativeDeploy }} -{{- else }} -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ .Values.service.name }} - labels: - app: {{ .Values.service.name }} -spec: - replicas: 1 - selector: - matchLabels: - app: {{ .Values.service.name }} - template: - metadata: - labels: - app: {{ .Values.service.name }} - spec: - containers: - - name: {{ .Values.service.name }} - image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" - imagePullPolicy: IfNotPresent - env: - - name: HASURA_URL - value: http://example-hasura.{{ .Release.Namespace }}.svc.cluster.local -{{- range $pkey, $pval := .Values.env }} - - name: {{ $pkey }} - value: {{ quote $pval }} -{{- end }} - - name: DEBUG - value: register-server* - ports: - - containerPort: 3000 - livenessProbe: - httpGet: - path: {{ .Values.livenessProbe.probePath | default .Values.probePath }} - port: {{ .Values.service.internalPort }} - initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }} - periodSeconds: {{ .Values.livenessProbe.periodSeconds }} - successThreshold: {{ .Values.livenessProbe.successThreshold }} - timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }} - readinessProbe: - httpGet: - path: {{ .Values.livenessProbe.probePath | default .Values.probePath }} - port: {{ .Values.service.internalPort }} - periodSeconds: {{ .Values.readinessProbe.periodSeconds }} - successThreshold: {{ .Values.readinessProbe.successThreshold }} - timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }} -{{- end }} -{{- end }} diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml deleted file mode 100755 index 24402b6..0000000 --- a/helm/templates/service.yaml +++ /dev/null @@ -1,29 +0,0 @@ -{{- if .Values.knative.eventing.local }} -{{- else }} -{{- if .Values.knativeDeploy }} -{{- else }} -apiVersion: v1 -kind: Service -metadata: -{{- if .Values.service.name }} - name: {{ .Values.service.name }} -{{- else }} - name: {{ template "fullname" . }} -{{- end }} - labels: - chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" -{{- if .Values.service.annotations }} - annotations: -{{ toYaml .Values.service.annotations | indent 4 }} -{{- end }} -spec: - type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.externalPort }} - targetPort: {{ .Values.service.internalPort }} - protocol: TCP - name: http - selector: - app: {{ .Values.service.name }} -{{- end }} -{{- end }} diff --git a/helm/templates/todo.completed.yaml b/helm/templates/todo.completed.yaml index 9df7ab2..483a6cf 100644 --- a/helm/templates/todo.completed.yaml +++ b/helm/templates/todo.completed.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: todo.completed + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: todo-events filter: diff --git a/helm/templates/todo.initialized.yaml b/helm/templates/todo.initialized.yaml index 1db50cf..f5a2206 100644 --- a/helm/templates/todo.initialized.yaml +++ b/helm/templates/todo.initialized.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: todo.initialized + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: todo-events filter: diff --git a/helm/templates/todo.removed.yaml b/helm/templates/todo.removed.yaml index bf870a2..4da83b1 100644 --- a/helm/templates/todo.removed.yaml +++ b/helm/templates/todo.removed.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: todo.removed + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: todo-events filter: diff --git a/helm/templates/todo.reopened.yaml b/helm/templates/todo.reopened.yaml index 41d6d0f..15bfee8 100644 --- a/helm/templates/todo.reopened.yaml +++ b/helm/templates/todo.reopened.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: todo.reopened + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: todo-events filter: diff --git a/helm/templates/web3auth.user-created.yaml b/helm/templates/web3auth.user-created.yaml index 4ad06af..1608666 100644 --- a/helm/templates/web3auth.user-created.yaml +++ b/helm/templates/web3auth.user-created.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: web3auth.user-created + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: web3auth-events filter: diff --git a/helm/templates/web3auth.user-updated.yaml b/helm/templates/web3auth.user-updated.yaml index 7be693d..c8ece33 100644 --- a/helm/templates/web3auth.user-updated.yaml +++ b/helm/templates/web3auth.user-updated.yaml @@ -2,6 +2,8 @@ apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: web3auth.user-updated + annotations: + argocd.argoproj.io/sync-wave: "-1" spec: broker: web3auth-events filter: diff --git a/preview/Kptfile b/preview/Kptfile deleted file mode 100644 index a806d3b..0000000 --- a/preview/Kptfile +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: kpt.dev/v1alpha1 -kind: Kptfile -metadata: - name: preview -upstream: - type: git - git: - commit: e95d99e4f74847a5eed092d3799fb7cfed5d0eac - repo: https://github.com/jenkins-x/jx3-pipeline-catalog - directory: /helm/preview - ref: master diff --git a/preview/helm/Chart.yaml b/preview/helm/Chart.yaml new file mode 100644 index 0000000..48b499d --- /dev/null +++ b/preview/helm/Chart.yaml @@ -0,0 +1,6 @@ +# Helm chart.yaml +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Kubernetes +name: example-policy-service-preview +version: 1.0.0 diff --git a/preview/helm/templates/app.yaml b/preview/helm/templates/app.yaml new file mode 100644 index 0000000..8bd2e2a --- /dev/null +++ b/preview/helm/templates/app.yaml @@ -0,0 +1,29 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: {{ .Values.repository.name }}-{{ .Values.pr }}-preview-app + namespace: argocd + finalizers: + - resources-finalizer.argocd.argoproj.io + annotations: + argocd.argoproj.io/sync-wave: "2" +spec: + project: example-preview-envs + source: + path: helm + repoURL: https://github.com/cloudnativeentrepreneur/{{ .Values.repository.name }}.git + targetRevision: {{ .Values.headRef }} + helm: + version: v3 + values: | + image: + tag: {{ .Values.tag }} + destination: + namespace: {{ .Values.repository.name }}-{{ .Values.pr }}-preview + server: https://kubernetes.default.svc + syncPolicy: + automated: + selfHeal: true + prune: true + syncOptions: + - CreateNamespace=true diff --git a/preview/helm/templates/environment.yaml b/preview/helm/templates/environment.yaml new file mode 100644 index 0000000..e21e221 --- /dev/null +++ b/preview/helm/templates/environment.yaml @@ -0,0 +1,23 @@ + +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: {{ .Values.repository.name }}-{{ .Values.pr }}-preview-env + namespace: argocd + finalizers: + - resources-finalizer.argocd.argoproj.io +spec: + project: example-preview-envs + source: + path: helm + repoURL: https://github.com/cloudnativeentrepreneur/environment.git + targetRevision: v0.2.0 + helm: + version: v3 + destination: + namespace: {{ .Values.repository.name }}-{{ .Values.pr }}-preview + server: https://kubernetes.default.svc + syncPolicy: + automated: + selfHeal: true + prune: true \ No newline at end of file diff --git a/preview/helm/values.yaml b/preview/helm/values.yaml new file mode 100644 index 0000000..5e2d67e --- /dev/null +++ b/preview/helm/values.yaml @@ -0,0 +1,5 @@ +pr: +headRef: +tag: +repository: + name: \ No newline at end of file diff --git a/preview/helmfile.yaml b/preview/helmfile.yaml deleted file mode 100644 index 56a8dae..0000000 --- a/preview/helmfile.yaml +++ /dev/null @@ -1,45 +0,0 @@ -environments: - default: - values: - - jx-values.yaml -repositories: -- name: jx3 - url: https://jenkins-x-charts.github.io/repo -- name: bitnami - url: https://charts.bitnami.com/bitnami -releases: -- chart: jx3/jx-verify - name: jx-verify - namespace: '{{ requiredEnv "PREVIEW_NAMESPACE" }}' - values: - - values.verify.yaml -- chart: '../charts/{{ requiredEnv "APP_NAME" }}' - name: preview - wait: true - createNamespace: true - namespace: '{{ requiredEnv "PREVIEW_NAMESPACE" }}' - values: - - jx-values.yaml - - values.preview.yaml - - values.yaml.gotmpl - hooks: - - events: ["presync"] - showlogs: true - command: "jx" - args: - - secret - - copy - - --create-namespace - - --selector - - "secret.jenkins-x.io/replica-source=true" - - --to - - '{{ requiredEnv "PREVIEW_NAMESPACE" }}' - - events: ["presync"] - showlogs: true - command: "kubectl" - args: - - label - - namespace - - '{{ requiredEnv "PREVIEW_NAMESPACE" }}' - - istio-injection=enabled - - --overwrite=true diff --git a/preview/values.preview.yaml b/preview/values.preview.yaml deleted file mode 100644 index eebfd3b..0000000 --- a/preview/values.preview.yaml +++ /dev/null @@ -1,4 +0,0 @@ -knativeDeploy: true - -runIntegrationTests: true -vaultRole: jx-vault \ No newline at end of file diff --git a/preview/values.verify.yaml b/preview/values.verify.yaml deleted file mode 100644 index 1d7e3c2..0000000 --- a/preview/values.verify.yaml +++ /dev/null @@ -1,3 +0,0 @@ -verifyJob: - podAnnotations: - sidecar.istio.io/inject: "false" diff --git a/preview/values.yaml.gotmpl b/preview/values.yaml.gotmpl deleted file mode 100644 index 0283df2..0000000 --- a/preview/values.yaml.gotmpl +++ /dev/null @@ -1,8 +0,0 @@ -jxRequirements: - ingress: - namespaceSubDomain: "-pr{{ requiredEnv "PULL_NUMBER" }}." - -image: - repository: "{{ requiredEnv "DOCKER_REGISTRY" }}/{{ requiredEnv "DOCKER_REGISTRY_ORG" }}/{{ requiredEnv "APP_NAME" }}" - tag: "{{ requiredEnv "VERSION" }}" - pullPolicy: "Always"