Skip to content

Commit

Permalink
chore: add runners for deployment and base image builds (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
nully0x authored Nov 20, 2024
1 parent 0dc37c0 commit 5a318d3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ PORT=3001
TEST_REPO_URL=https://github.com/nully0x/test-repo
TEST_REPO_BRANCH=main
TEST_REPO_PATH=tests
BASE_URL=
48 changes: 48 additions & 0 deletions .github/workflows/staging-build-base-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build Base Images

on:
workflow_dispatch: # Manual trigger
push:
paths:
- "baseImages/**"
branches:
- main

jobs:
build-base-images:
name: Build Base Images on Droplet
runs-on: ubuntu-latest
strategy:
matrix:
image:
- { name: "rust", tag: "rs-base", path: "baseImages/rust" }
- {
name: "typescript",
tag: "ts-base",
path: "baseImages/typescript",
}

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.STAGING_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.STAGING_DROPLET_IP }} >> ~/.ssh/known_hosts
# Create directory and copy Dockerfile
- name: Copy Dockerfile to Droplet
run: |
ssh ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }} "mkdir -p ~/base-images/${{ matrix.image.name }}"
scp ${{ matrix.image.path }}/Dockerfile ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }}:~/base-images/${{ matrix.image.name }}/Dockerfile
# Build the image on the droplet
- name: Build Base Image
run: |
ssh ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }} << 'EOF'
cd ~/base-images/${{ matrix.image.name }}
docker build -t ${{ matrix.image.tag }}:latest .
EOF
66 changes: 66 additions & 0 deletions .github/workflows/staging-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Deploy to DigitalOcean

on:
push:
branches:
- main

jobs:
deploy:
name: Build and Deploy Node.js App
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "18"

- name: Configure SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.STAGING_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.STAGING_DROPLET_IP }} >> ~/.ssh/known_hosts
- name: Install Dependencies and Build
run: |
yarn install
yarn build
ls dist # Debug: Check if dist directory exists and its contents
# Create necessary directories first
- name: Create Remote Directories
run: |
ssh ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }} "mkdir -p ~/node-app/dist"
# Then copy files
- name: Copy Built Files to Droplet
run: |
scp -r dist/* ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }}:~/node-app/dist/
scp package.json ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }}:~/node-app/
scp yarn.lock ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }}:~/node-app/
- name: Set Up .env File
run: |
ssh ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }} "echo '${{ secrets.STAGING_RUNNER_SECRET_FILE }}' > ~/node-app/.env"
- name: Install Dependencies and Start App
run: |
ssh ${{ secrets.STAGING_DROPLET_USER }}@${{ secrets.STAGING_DROPLET_IP }} << 'EOF'
cd ~/node-app
yarn install --production
ls dist # Debug: Check if dist directory exists on server
if pm2 list | grep -q node-app; then
echo "Restarting existing app..."
pm2 restart node-app
else
echo "Starting new app..."
pm2 start dist/server.js --name node-app
fi
pm2 save
EOF

0 comments on commit 5a318d3

Please sign in to comment.