Skip to content

Commit

Permalink
feat: previews (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickleet authored Nov 6, 2022
1 parent fe5b91c commit 16c1b53
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 101 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
127 changes: 78 additions & 49 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,20 @@ name: pr
on:

pull_request:
branches:
- main

jobs:

# quality:
# runs-on: ubuntu-latest

# strategy:
# matrix:
# node-version: [18.x]
# # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

# steps:
# - uses: actions/checkout@v2
# - 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

preview:
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 +52,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
22 changes: 0 additions & 22 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,8 @@ on:
- main
jobs:

# quality:
# runs-on: ubuntu-latest

# strategy:
# matrix:
# node-version: [18.x]
# # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

# steps:
# - uses: actions/checkout@v2
# - 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

release:
runs-on: ubuntu-latest
# needs: quality

permissions:
packages: write
Expand Down
12 changes: 6 additions & 6 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ readinessProbe:

resources:
limits:
cpu: '1'
cpu: "1"
memory: 1Gi
requests:
cpu: 200m
Expand All @@ -50,7 +50,7 @@ hasura:
name: example-hasura-metadata-postgresql
database: metadata
username: postgres
hostname:
hostname:
write: example-hasura-metadata-postgresql
read: example-hasura-metadata-postgresql-repl
pool: example-hasura-metadata-db-pgpool
Expand Down Expand Up @@ -106,7 +106,7 @@ readmodel:
name: example-readmodel-postgresql
database: readmodel
username: postgres
hostname:
hostname:
write: example-readmodel-postgresql
read: example-readmodel-postgresql-repl
pool: example-readmodel-pgpool
Expand All @@ -116,7 +116,7 @@ sourced:
name: example-sourced-db-postgresql
database: sourced
username: postgres
hostname:
hostname:
write: example-sourced-db-postgresql
read: example-sourced-db-postgresql-repl
pool: example-sourced-db-pgpool
Expand All @@ -126,7 +126,7 @@ web3auth:
name: web3auth-db-postgresql
database: web3auth
username: postgres
hostname:
hostname:
write: web3auth-db-postgresql
read: web3auth-db-postgresql-repl
pool: web3auth-db-pgpool
pool: web3auth-db-pgpool
6 changes: 6 additions & 0 deletions preview/helm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Helm chart.yaml
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: example-todo-model-service-preview
version: 1.0.0
29 changes: 29 additions & 0 deletions preview/helm/templates/app.yaml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions preview/helm/templates/environment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: {{ .Values.repository.name }}-{{ .Values.pr }}-preview-environment
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
5 changes: 5 additions & 0 deletions preview/helm/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr:
headRef:
tag:
repository:
name:

0 comments on commit 16c1b53

Please sign in to comment.