Skip to content

Commit

Permalink
ci: add deploy on pr
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed Jun 9, 2024
1 parent 1e28577 commit 4a61cf1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/build-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version-file: .node-version
node-version-file: .node-version

- name: Install Yarn
run: npm install -g yarn
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/cleanup_pull_request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'Cleanup Pull Request'
on:
pull_request:
types: [closed]

jobs:
cleanup-pr:
name: "Cleanup Pull Request"
runs-on: ubuntu-latest
continue-on-error: true
permissions:
contents: read
packages: write
actions: write
pull-requests: write

steps:
- name: Kubernetes login
uses: actions-hub/kubectl@master
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}

- name: Delete Helm Chart
run: |
helm delete project-website-pr-${{ github.event.number }} --namespace project-website-pr-${{ github.event.number }}
- name: Delete Namespace
if: always()
run: |
kubectl delete namespace project-website-pr-${{ github.event.number }}
85 changes: 85 additions & 0 deletions .github/workflows/deploy_pull_request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: "Deploy Pull Request"
on:
pull_request:
types: [labeled]

jobs:
deploy_pr:
name: "Deploy Pull Request"
runs-on: ubuntu-latest
if: ${{ github.event.label.name == 'deploy' }}
permissions:
contents: read
packages: write
actions: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

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

- name: set lower case owner name
run: |
echo "REPO_LC=${REPO,,}" >>${GITHUB_ENV}
env:
REPO: "${{ github.repository }}"

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./.docker/Dockerfile.dev
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/${{ env.REPO_LC }}-dev:pr-${{ github.event.number }}

- name: "Setup yq"
uses: dcarbone/[email protected]
with:
version: "v4.42.1"
force: true

- name: Change values in on-pr.yaml
run: |
yq -i '
.ingress.hosts[0].name = "pr-${{ github.event.number }}.website.dev.green-ecolution.de" |
.deployment.image.tag = "pr-${{ github.event.number }}" |
.deployment.name = "project-website-pr-${{ github.event.number }}" |
.deployment.namespace = "project-website-pr-${{ github.event.number }}"
' ./k8s/values/on-pr.yaml
- name: Kubernetes login
uses: azure/k8s-set-context@v4
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }} | base64 -d

- name: Install Helm Chart
run: |
helm upgrade project-website-pr-${{ github.event.number }} ./k8s \
--install \
--namespace project-website-pr-${{ github.event.number }} \
--create-namespace \
--wait \
--values ./k8s/values.yaml \
--values ./k8s/values/on-pr.yaml \
- name: Post comment to Pull Request
uses: thollander/actions-comment-pull-request@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
message: |
"Deployment of PR #${{ github.event.number }} successful :rocket: \br\br You can visit it at https://pr-${{ github.event.number }}.website.dev.green-ecolution.de"
25 changes: 25 additions & 0 deletions k8s/values/on-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
deployment:
namespace: project-website-pr
name: project-website-pr
replicaCount: 1
image:
repository: ghcr.io/smartcityflensburg/project-website-dev
pullPolicy: Always
tag: "latest"
ingress:
enabled: true
className: "traefik"
annotations:
cert-manager.io/cluster-issuer: cert-manager
kubernetes.io/ingress.class: traefik
hosts:
- name: pr.website.dev.green-ecolution.de
paths:
- path: /
pathType: Prefix
backend:
serviceName: project-website
servicePort: 80
tls:
enabled: true
secretName: dev-green-ecolution-de-tls

0 comments on commit 4a61cf1

Please sign in to comment.