Skip to content
Open
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
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CI - Build and Test

on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]

permissions: # ADD THIS BLOCK
contents: read # ADD
packages: write # ADD

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build the Docker image
run: docker build -t tutorial-app:ci .

- name: Create a shared network
run: docker network create test-net

- name: Start a PostgreSQL database
run: |
docker run -d --name test-db --network test-net \
-e POSTGRES_USER=tutorial_user \
-e POSTGRES_PASSWORD=tutorial_password \
-e POSTGRES_DB=tutorial_db \
postgres:15-alpine

- name: Wait for the database to be ready
run: |
for i in $(seq 1 30); do
if docker exec test-db pg_isready -U tutorial_user; then
echo "Database is ready!"
break
fi
echo "Waiting for database... ($i)"
sleep 2
done

- name: Run the app container
run: |
docker run -d --name test-app --network test-net \
-e DB_HOST=test-db \
-e DB_USER=tutorial_user \
-e DB_PASSWORD=tutorial_password \
-e DB_NAME=tutorial_db \
-p 8080:8080 \
tutorial-app:ci

- name: Wait for the app to start
run: sleep 15

- name: Health check (the actual test)
run: |
curl --fail http://localhost:8080/api/tutorials || exit 1
echo "API is responding correctly!"

# ===== NEW: publish image to GHCR =====
- name: Log in to GitHub Container Registry
if: github.event_name == 'push'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set lowercase image name
if: github.event_name == 'push'
run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV

- name: Push image to GHCR
if: github.event_name == 'push'
run: |
docker tag tutorial-app:ci $IMAGE:latest
docker tag tutorial-app:ci $IMAGE:${{ github.sha }}
docker push $IMAGE:latest
docker push $IMAGE:${{ github.sha }}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]
}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install --production

COPY . .

ENV NODE_ENV=production
ENV PORT=8080

EXPOSE 8080

CMD ["node", "server.js"]
8 changes: 4 additions & 4 deletions app/config/db.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
HOST: "localhost",
USER: "postgres",
PASSWORD: "123",
DB: "testdb",
HOST: process.env.DB_HOST || "localhost",
USER: process.env.DB_USER || "postgres",
PASSWORD: process.env.DB_PASSWORD || "123456",
DB: process.env.DB_NAME || "testdb",
dialect: "postgres",
pool: {
max: 5,
Expand Down
3 changes: 3 additions & 0 deletions app/routes/turorial.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = app => {
// Create a new Tutorial
router.post("/", tutorials.create);

// Create a new Tutorial
router.post("/health", tutorials.create);

// Retrieve all Tutorials
router.get("/", tutorials.findAll);

Expand Down
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

services:
backend:
build: .
container_name: tutorial-api
ports:
- "8080:8080"
environment:
DB_HOST: postgres
DB_USER: tutorial_user
DB_PASSWORD: tutorial_password
DB_NAME: tutorial_db
DB_PORT: 5432
depends_on:
- postgres

postgres:
image: postgres:15-alpine
container_name: tutorial-db
environment:
POSTGRES_USER: tutorial_user
POSTGRES_PASSWORD: tutorial_password
POSTGRES_DB: tutorial_db
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:

31 changes: 31 additions & 0 deletions render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
- type: web
name: tutorial-api
runtime: docker
dockerfilePath: ./Dockerfile
plan: free
envVars:
- key: DB_HOST
fromDatabase:
name: tutorial-db
property: host
- key: DB_PORT
fromDatabase:
name: tutorial-db
property: port
- key: DB_USER
fromDatabase:
name: tutorial-db
property: user
- key: DB_PASSWORD
fromDatabase:
name: tutorial-db
property: password
- key: DB_NAME
fromDatabase:
name: tutorial-db
property: database

databases:
- name: tutorial-db
plan: free