Skip to content

Merge pull request #16 from Smaug6739/main #545

Merge pull request #16 from Smaug6739/main

Merge pull request #16 from Smaug6739/main #545

name: Build & deploy to VPS
on:
push:
branches:
- deploy
jobs:
build_and_deploy:
runs-on: ubuntu-latest
env:
INPUT_HOST: ${{ secrets.SSH_HOST }}
INPUT_REMOTE: /home/smaug/sites/alexandrie
SSH_USER: ${{ secrets.SSH_USER }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup bun
uses: oven-sh/setup-bun@v1
- name: Install dependencies
run: |
cd frontend
bun install
cd ../backend
bun install
- name: Build backend
run: |
cd backend
bun run build
- name: Build frontend
run: |
cd frontend
bun run build
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22' # Change to your desired Go version
- name: Build Go application
run: | # Build the Go application and name it cdn_app
cd cdn # Change to your Go application directory
go mod download
GOOS=linux GOARCH=amd64 go build -o cdn_app
- name: Set up SSH
run: |
mkdir -p ~/.ssh
touch ~/.ssh/known_hosts
chmod 700 ~/.ssh
chmod 600 ~/.ssh/known_hosts
ssh-keyscan -t ed25519 "${INPUT_HOST}" >> ~/.ssh/known_hosts
echo -e "${DEPLOY_SSH_KEY}" > deploy_key
chmod 600 deploy_key
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
- name: Create Archives
run: |
tar -czf frontend.tar.gz frontend/.output frontend/.env.production
tar -czf backend.tar.gz -C backend package.json bun.lockb -C dist .
tar -czf cdn.tar.gz cdn/cdn_app # Archive the Go application binary
- name: Transfer Archives
run: |
scp -i deploy_key ecosystem.config.js ${SSH_USER}@"${INPUT_HOST}":"${INPUT_REMOTE}"
scp -i deploy_key frontend.tar.gz ${SSH_USER}@"${INPUT_HOST}":"${INPUT_REMOTE}"
scp -i deploy_key backend.tar.gz ${SSH_USER}@"${INPUT_HOST}":"${INPUT_REMOTE}"
scp -i deploy_key cdn.tar.gz ${SSH_USER}@"${INPUT_HOST}":"${INPUT_REMOTE}"
- name: Extract Archives on Remote
run: |
ssh -i deploy_key ${SSH_USER}@"${INPUT_HOST}" "cd ${INPUT_REMOTE} && tar -xzf frontend.tar.gz && mkdir -p backend && tar -xzf backend.tar.gz -C backend && tar -xzf cdn.tar.gz && rm frontend.tar.gz backend.tar.gz cdn.tar.gz"
- name: Install dependencies for backend
run: |
ssh -i deploy_key ${SSH_USER}@"${INPUT_HOST}" "source ~/.bashrc && cd ${INPUT_REMOTE} && cd backend && bun install --production"
- name: Start application
run: |
ssh -i deploy_key ${SSH_USER}@"${INPUT_HOST}" "cd ${INPUT_REMOTE} && NODE_ENV='production' DATABASE_PASSWORD='${{ secrets.DATABASE_PASSWORD }}' DATABASE_USER='${{ secrets.DATABASE_USER }}' JWT_SECRET='${{ secrets.JWT_SECRET }}' pm2 start ecosystem.config.js --update-env"
env:
DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
DATABASE_USER: ${{ secrets.DATABASE_USER }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}