Skip to content

feat(add): Add simple PR preview environments #8

feat(add): Add simple PR preview environments

feat(add): Add simple PR preview environments #8

Workflow file for this run

name: Build and deploy Preview Environment
on:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
env:
TAG: preview-pr${{ github.event.number }}
ARGOCD_APP_NAME: tgno-preview-pr${{ github.event.number }}
KUSTOMIZE_REPO: https://github.com/gathering/k8s-iac.git
KUSTOMIZE_PATH: tgno/preview-frontend
ARGOCD_VERSION: 2.12.6
DOMAIN: tgno-pr${{ github.event.number }}.preview.tg.no
concurrency:
group: preview-${{ github.event.number }}
cancel-in-progress: true
jobs:
# Build docker image and push it to registry
build:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
outputs:
digest: ${{ steps.docker_build_push.outputs.digest }}
permissions:
contents: read
id-token: write
packages: write
pull-requests: write
steps:
- 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: Format vars
run: echo "REPO_SLUG=$(echo $GITHUB_REPOSITORY | sed 's/[^a-zA-Z0-9]/-/g' )" >> $GITHUB_ENV
- name: Log in to the Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build astro image for amd64
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
build-args: |
APP_ENV=development
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ env.TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
setup-preview-environment:
needs: build
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Login to ArgoCD
uses: clowdhaus/argo-cd-action@v2.2.0
with:
version: ${{ env.ARGOCD_VERSION }}
command: login
options: >-
${{ secrets.ARGOCD_URL }}
--username ${{ secrets.ARGOCD_USERNAME }}
--password ${{ secrets.ARGOCD_PASSWORD }}
--grpc-web
--skip-test-tls
- name: Create ArgoCD Application
uses: clowdhaus/argo-cd-action@v2.2.0
with:
version: ${{ env.ARGOCD_VERSION }}
command: app create
options: >-
${{ env.ARGOCD_APP_NAME }}
--project tg-systems
--repo ${{ env.KUSTOMIZE_REPO }}
--path ${{ env.KUSTOMIZE_PATH }}
--dest-namespace ${{ env.ARGOCD_APP_NAME }}
--dest-server https://kubernetes.default.svc
--sync-option CreateNamespace=true
--revision tgno-preview
--upsert
--grpc-web
--kustomize-image ghcr.io/${{ github.repository }}:${{ env.TAG }}
--kustomize-replica frontend=1
- name: Sync ArgoCD Application
uses: clowdhaus/argo-cd-action@v2.2.0
with:
version: ${{ env.ARGOCD_VERSION }}
command: app sync
options: >-
${{ env.ARGOCD_APP_NAME }}
--prune
--async
--grpc-web
- uses: marocchino/sticky-pull-request-comment@v2
with:
header: setup-preview-environment
message: |
### Preview Environment created
${{ github.sha }} live at https://${{ env.DOMAIN }}
ghcr.io/${{ github.repository }}:${{ env.TAG }} (${{needs.build.outputs.digest}})
[ArgoCD Application](https://${{ secrets.ARGOCD_URL }}/applications/${{ env.ARGOCD_APP_NAME }})