Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/cleanup-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Cleanup PR Deployment

on:
pull_request:
types:
- closed

jobs:
cleanup:
runs-on: ubuntu-latest

steps:
- name: Set variables
run: |
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF##*/}}"
BRANCH_SAFE=${BRANCH//\//-}
echo "NAMESPACE=tsd-$BRANCH_SAFE" >> $GITHUB_ENV

- name: Set up Kubeconfig
run: |
echo "${{ secrets.KUBECONFIG }}" > kubeconfig
echo "KUBECONFIG=$(pwd)/kubeconfig" >> $GITHUB_ENV

- name: Delete Namespace
run: |
kubectl --kubeconfig="${KUBECONFIG}" delete namespace ${{ env.NAMESPACE }} --ignore-not-found
85 changes: 0 additions & 85 deletions .github/workflows/deploy-to-k8.yml

This file was deleted.

208 changes: 208 additions & 0 deletions .github/workflows/deploy-to-k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
name: Deploy to Kubernetes

on:
push:
branches:
- main
- develop
pull_request:
types: [ opened, synchronize, reopened ]
branches:
- develop

jobs:
setup:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
outputs:
repo: ${{ steps.set-vars.outputs.repo }}
tag: ${{ steps.set-vars.outputs.tag }}
api_url: ${{ steps.set-vars.outputs.api_url }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set variables
id: set-vars
run: |
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF##*/}}"
echo "repo=${GITHUB_REPOSITORY,,}" >> $GITHUB_OUTPUT
if [[ "$BRANCH" == "main" ]]; then
echo "tag=latest" >> $GITHUB_OUTPUT
echo "api_url=https://api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_OUTPUT
elif [[ "$BRANCH" == "develop" ]]; then
echo "tag=develop" >> $GITHUB_OUTPUT
echo "api_url=https://staging.api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_OUTPUT
else
BRANCH_SAFE=${BRANCH//\//-}
echo "tag=$BRANCH_SAFE" >> $GITHUB_OUTPUT
echo "api_url=https://$BRANCH_SAFE.api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_OUTPUT
fi

build-client:
needs: setup
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

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

- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-client
key: ${{ runner.os }}-client-${{ github.sha }}
restore-keys: |
${{ runner.os }}-client-

- name: Build and push client image
uses: docker/build-push-action@v3
with:
context: ./client
file: ./client/Dockerfile
push: true
tags: ghcr.io/${{ needs.setup.outputs.repo }}/client:${{ needs.setup.outputs.tag }}
build-args: API_URL=${{ needs.setup.outputs.api_url }}
platforms: linux/amd64

build-server:
needs: setup
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

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

- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache-server
key: ${{ runner.os }}-server-${{ github.sha }}
restore-keys: |
${{ runner.os }}-server-

- name: Build and push server image
uses: docker/build-push-action@v3
with:
context: ./server
file: ./server/Dockerfile
push: true
tags: ghcr.io/${{ needs.setup.outputs.repo }}/server:${{ needs.setup.outputs.tag }}
build-args: API_URL=${{ needs.setup.outputs.api_url }}
platforms: linux/amd64

deploy:
needs:
- build-client
- build-server
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set variables
run: |
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF##*/}}"
if [[ "$BRANCH" == "main" ]]; then
echo "NAMESPACE=tsd-prod" >> $GITHUB_ENV
echo "IMAGE_TAG=lastest" >> $GITHUB_ENV
echo "VALUES_FILE=./infrastructure/whiteboard-app/production.values.yaml" >> $GITHUB_ENV
echo "CLIENT_URL=whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "SERVER_URL=api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "AUTH_URL=auth.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
elif [[ "$BRANCH" == "develop" ]]; then
echo "NAMESPACE=tsd-staging" >> $GITHUB_ENV
echo "IMAGE_TAG=develop" >> $GITHUB_ENV
echo "VALUES_FILE=./infrastructure/whiteboard-app/staging.values.yaml" >> $GITHUB_ENV
echo "CLIENT_URL=staging.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "SERVER_URL=staging.api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "AUTH_URL=staging.auth.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
else
BRANCH_SAFE=${BRANCH//\//-}
echo "NAMESPACE=tsd-$BRANCH_SAFE" >> $GITHUB_ENV
echo "IMAGE_TAG=$BRANCH_SAFE" >> $GITHUB_ENV
echo "VALUES_FILE=./infrastructure/whiteboard-app/pullrequest.values.yaml" >> $GITHUB_ENV
echo "CLIENT_URL=$BRANCH_SAFE.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "SERVER_URL=$BRANCH_SAFE.api.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
echo "AUTH_URL=$BRANCH_SAFE.auth.whiteboard.student.k8s.aet.cit.tum.de" >> $GITHUB_ENV
fi

- name: Set up Kubeconfig
run: |
echo "${{ secrets.KUBECONFIG }}" > kubeconfig
echo "KUBECONFIG=$(pwd)/kubeconfig" >> $GITHUB_ENV

- name: Install Helm
uses: azure/setup-helm@v3

- name: Deploy App with Helm
run: |
helm upgrade whiteboard ./infrastructure/whiteboard-app/ \
-f ${{ env.VALUES_FILE }} \
-n ${{ env.NAMESPACE }} \
--create-namespace \
--install \
--atomic \
--kubeconfig ${{ env.KUBECONFIG }} \
--set namespace="${{ env.NAMESPACE }}" \
--set server.image.tag="${{ env.IMAGE_TAG }}" \
--set client.image.tag="${{ env.IMAGE_TAG }}" \
--set client.url="${{ env.CLIENT_URL }}" \
--set server.url="${{ env.SERVER_URL }}" \
--set auth.url="${{ env.AUTH_URL }}"

comment-pr:
needs: deploy
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Comment on Pull Request with URLs
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const clientUrl = `https://${process.env.CLIENT_URL}`;
const serverUrl = `https://${process.env.SERVER_URL}`;
const authUrl = `https://${process.env.AUTH_URL}`;

// Check existing comments to avoid duplicates
const { data: comments } = await github.rest.issues.listComments({
...context.repo,
issue_number: prNumber,
});

const commentExists = comments.some(comment =>
comment.body.includes('### Deployment URL')
);

if (!commentExists) {
const body = `
### Deployment URL
- [${clientUrl}](${clientUrl})
- [${serverUrl}](${serverUrl})
- [${authUrl}](${authUrl})
`;

await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body,
});
}
8 changes: 2 additions & 6 deletions client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ RUN npm ci

COPY . .

ARG ENV=production
RUN if [ "$ENV" = "staging" ]; then \
cp .env.staging .env; \
elif [ "$ENV" = "production" ]; then \
cp .env.prod .env; \
fi
ARG API_URL
ENV NEXT_PUBLIC_API_URL=${API_URL}

RUN npm run build

Expand Down
Loading
Loading