diff --git a/.github/workflows/build-on-pr.yml b/.github/workflows/build-on-pr.yml index aa0ac13..a4e3cf4 100644 --- a/.github/workflows/build-on-pr.yml +++ b/.github/workflows/build-on-pr.yml @@ -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 diff --git a/.github/workflows/cleanup_pull_request.yaml b/.github/workflows/cleanup_pull_request.yaml new file mode 100644 index 0000000..81a7b49 --- /dev/null +++ b/.github/workflows/cleanup_pull_request.yaml @@ -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 }} diff --git a/.github/workflows/deploy_pull_request.yaml b/.github/workflows/deploy_pull_request.yaml new file mode 100644 index 0000000..773802f --- /dev/null +++ b/.github/workflows/deploy_pull_request.yaml @@ -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/install-yq-action@v1.1.1 + 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.KUBE_CONFIG }} | 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" diff --git a/k8s/values/on-pr.yaml b/k8s/values/on-pr.yaml new file mode 100644 index 0000000..911396f --- /dev/null +++ b/k8s/values/on-pr.yaml @@ -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