From 50024d40af1ad594558b092977e4e699fe4255aa Mon Sep 17 00:00:00 2001 From: Inggih Wicaksono Date: Fri, 26 Jun 2026 09:38:00 +0700 Subject: [PATCH 1/2] feat: add Docker configuration --- .dockerignore | 5 +++++ Dockerfile | 7 +++++++ docker-compose.yml | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e7d01c2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +.env.docker +dist +.git +*.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a217e3a --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..82bf720 --- /dev/null +++ b/docker-compose.yml @@ -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: From 6029abf9fcb6ffdfe0b8a1e93965184d1c7cbf44 Mon Sep 17 00:00:00 2001 From: Inggih Wicaksono Date: Mon, 29 Jun 2026 11:21:27 +0700 Subject: [PATCH 2/2] ci: update cd.yml to build and deploy Docker image --- .github/workflows/cd.yml | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 23a14a4..0c7dcad 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -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 @@ -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 }} \ No newline at end of file + CORS_ORIGIN: ${{ secrets.CORS_ORIGIN }} + GHCR_USER: ${{ github.repository_owner }} \ No newline at end of file