Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.env.docker
dist
.git
*.log
36 changes: 29 additions & 7 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ on:
branches:
- main
jobs:
deploy:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Login ke GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build dan push image ke GHCR
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/${{ github.repository_owner }}/notes-api-express:latest
deploy-ec2:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy ke EC2 via SSH
Expand All @@ -13,22 +33,24 @@ jobs:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
envs: APP_PORT,DATABASE_URL,JWT_SECRET,CORS_ORIGIN
envs: APP_PORT,DATABASE_URL,JWT_SECRET,CORS_ORIGIN,GHCR_USER
script: |
docker pull ghcr.io/$GHCR_USER/notes-api-express:latest
cd ~/hands-on-notes-api-express
git checkout main
git pull origin main
cat > .env << EOF
cat > .env.docker << EOF
PORT=$APP_PORT
DATABASE_URL=$DATABASE_URL
JWT_SECRET=$JWT_SECRET
CORS_ORIGIN=$CORS_ORIGIN
EOF
npm ci
npm run migrate up
pm2 reload notes-api
sed -i "s|build: \.|image: ghcr.io/$GHCR_USER/notes-api-express:latest|g" docker-compose.yml
docker compose --env-file .env.docker up -d --no-deps app
docker compose --env-file .env.docker exec -T app npm run migrate up
env:
APP_PORT: ${{ secrets.APP_PORT }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
CORS_ORIGIN: ${{ secrets.CORS_ORIGIN }}
CORS_ORIGIN: ${{ secrets.CORS_ORIGIN }}
GHCR_USER: ${{ github.repository_owner }}
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:24-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 5000
CMD ["node", "app.js"]
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
app:
build: .
ports:
- "5000:5000"
env_file:
- .env.docker
depends_on:
- db
restart: unless-stopped
db:
image: postgres:16-alpine
ports:
- "5433:5432"
environment:
POSTGRES_DB: notesapp
POSTGRES_USER: notesuser
POSTGRES_PASSWORD: password_anda
volumes:
- postgres_data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
postgres_data:
Loading