Skip to content

Commit

Permalink
feat: preview environments (#72)
Browse files Browse the repository at this point in the history
* feat: preview

* fix: cleanup charts

* fix: preview db

* feat: sync waves
  • Loading branch information
patrickleet authored Oct 31, 2022
1 parent c3bdff7 commit e09a89f
Show file tree
Hide file tree
Showing 23 changed files with 228 additions and 293 deletions.
48 changes: 29 additions & 19 deletions .github/workflows/pr-close.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
123 changes: 89 additions & 34 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: pr
on:

pull_request:
branches:
- main

jobs:

Expand All @@ -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
Expand Down Expand Up @@ -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 ksvc -n ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview
```
edit-mode: replace
18 changes: 13 additions & 5 deletions .github/workflows/publish-and-promote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions helm/templates/NOTES.txt

This file was deleted.

16 changes: 0 additions & 16 deletions helm/templates/_helpers.tpl

This file was deleted.

3 changes: 2 additions & 1 deletion helm/templates/commands-broker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ kind: Broker
metadata:
name: todo-commands
annotations:
eventing.knative.dev/broker.class: MTChannelBasedBroker
eventing.knative.dev/broker.class: MTChannelBasedBroker
argocd.argoproj.io/sync-wave: "-1"
spec:
delivery:
deadLetterSink:
Expand Down
63 changes: 0 additions & 63 deletions helm/templates/deployment.yaml

This file was deleted.

1 change: 1 addition & 0 deletions helm/templates/events-broker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ metadata:
name: todo-events
annotations:
eventing.knative.dev/broker.class: MTChannelBasedBroker
argocd.argoproj.io/sync-wave: "-1"
spec:
delivery:
deadLetterSink:
Expand Down
10 changes: 4 additions & 6 deletions helm/templates/integration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
apiVersion: batch/v1
kind: Job
metadata:
{{- if .Values.service.name }}
name: {{ .Values.service.name }}-integration-tests-{{ lower .Values.image.tag }}
{{- else }}
name: {{ template "fullname" . }}-integration-tests-{{ lower .Values.image.tag }}
{{- end }}
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
annotations:
argocd.argoproj.io/sync-wave: "2"
spec:
template:
metadata:
Expand All @@ -20,14 +18,14 @@ spec:
serviceAccountName: {{ .Values.service.name }}-integration-tests
initContainers:
- name: wait-for-resources
image: bitnami/kubectl:1.19
image: bitnami/kubectl:1.24
command: [
'sh',
'-c',
"kubectl wait --for=condition=ready pod --selector=cluster-name={{ .Values.sourced.psql.name }} --timeout=600s -n {{ .Release.Namespace }}; REVISION=`kubectl get revisions -n {{ .Release.Namespace }} | grep example-hasura-0 | tail -1 | awk '$1 {print$1}-deployment'`; kubectl rollout status -n {{ .Release.Namespace }} deployment $REVISION-deployment"
]
- name: wait-for-new-revision
image: bitnami/kubectl:1.19
image: bitnami/kubectl:1.24
command: ['sh', '-c', "REVISION=`kubectl get revisions -n {{ .Release.Namespace }} | tail -1 | awk '$1 {print$1}-deployment'`; kubectl rollout status -n {{ .Release.Namespace }} deployment $REVISION-deployment"]
containers:
- name: integration-tests
Expand Down
Loading

0 comments on commit e09a89f

Please sign in to comment.