Skip to content

Feat: Preview environments #113

Feat: Preview environments

Feat: Preview environments #113

# This is a basic workflow to help you get started with Actions
name: CI/CD For docker deploy
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
pull_request:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build-push-docker:
permissions:
deployments: write
runs-on: ubuntu-latest
environment: dev
steps:
- uses: actions/checkout@v2
- name: start deployment
uses: bobheadxi/deployments@v1
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: preview-${{ github.event.pull_request.number }}
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_SERVER_KEY }}
name: id_rsa # optional
known_hosts: ${{ secrets.KNOWN_HOSTS }}
- name: Deploy to Server
run: |
BUILD_ARGS="--build-arg NEXT_PUBLIC_VERCEL_URL=gh-${GITHUB_PR_NUMBER}.fredkiss.dev --build-arg GITHUB_REDIRECT_URI=https://gh.fredkiss.dev/api/auth/callback --build-arg SESSION_SECRET=${{ secrets.SESSION_SECRET }} --build-arg DATABASE_URL=${{ secrets.DEV_LOCAL_POSTGRES_DB_URL }} --build-arg REMOTE_DATABASE_URL=${{ secrets.DEV_REMOTE_POSTGRES_DB_URL }} --build-arg GITHUB_CLIENT_ID=${{ secrets.GH_CLIENT_ID }} --build-arg GITHUB_SECRET=${{ secrets.GH_SECRET }} --build-arg GITHUB_PERSONAL_ACCESS_TOKEN=${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} --build-arg REDIS_HTTP_USERNAME=${{ secrets.REDIS_HTTP_USERNAME }} --build-arg REDIS_HTTP_PASSWORD=${{ secrets.REDIS_HTTP_PASSWORD }} --build-arg KV_PREFIX=__gh_next__cache_dev_"
GIT_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
GITHUB_BRANCH_SLUG="${GIT_BRANCH//[^a-zA-Z0-9]/-}"
echo branch-slug=$GITHUB_BRANCH_SLUG
ssh -p $DEPLOY_PORT $DEPLOY_USER@$DEPLOY_DOMAIN "
source ~/.zshrc
set -e -o errexit
cd $DEPLOY_DIR
echo 'Login to docker registry...'
echo ${{ secrets.DCR_PASSWD }} | docker login --username=fredkiss3 --password-stdin dcr.fredkiss.dev
echo 'succesfully logged in to registry dcr.fredkiss.dev ✅'
echo use node version 20
nvm use 20
echo Pulling latest version...
echo git branch=$GIT_BRANCH
git fetch origin $GIT_BRANCH
git checkout $GIT_BRANCH
git pull origin $GIT_BRANCH
echo 'Build with docker (and cache)...🔄'
export DOCKER_BUILDKIT=1
# Build & push docker image
docker buildx build --push ${BUILD_ARGS} -f ./docker/Dockerfile.dev -t dcr.fredkiss.dev/gh-next:pr-${GITHUB_PR_NUMBER} .
echo 'Docker build successful ✅'
cd pr-preview-workflow
bun install --frozen-lockfile
bun run index.ts --pr-id ${GITHUB_PR_NUMBER} \
--pr-branch ${GITHUB_BRANCH_SLUG} \
--caddy-config-path ${{ secrets.CADDY_CONFIG_DIR }} \
--reload-caddy --reload-docker
"
env:
DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
DEPLOY_DIR: ${{ secrets.DEPLOY_DIR }}
DEPLOY_DOMAIN: ${{ secrets.DEPLOY_DOMAIN }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
- name: update deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: ${{ steps.deployment.outputs.env }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: https://gh-${{ github.event.pull_request.number }}.gh.fredkiss.dev