Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devops intern fullstack-assignmnet #41

Closed
wants to merge 7 commits into from
Closed
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
90 changes: 90 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
pipeline {
// Define the agent where the pipeline will run. 'any' means it can run on any available agent.
agent any

// Define environment variables to be used in the pipeline.
environment {
BACKEND_IMAGE = "YOUR_USERNAME/backend" // Docker image
FRONTEND_IMAGE = "YOUR_USERNAME/frontend" // Docker image
DOCKER_CREDENTIALS_ID = "dockerhub" // Jenkins credential ID
SONAR_SCANNER_HOME = tool 'sonarqube' // SonarQube Scanner tool as configured in Jenkins
SONAR_PROJECT_KEY = "demo" // SonarQube project key
SONAR_HOST_URL = "http://34.174.208.0:9000" // SonarQube server URL
SONAR_AUTH_TOKEN = credentials('sonarqube') // Jenkins credential ID for SonarQube authentication token
}

// Define the stages of the pipeline.
stages {
stage('Checkout Code') {
steps {
script {
// Checkout the code from the version control system (e.g., Git).
checkout scm
}
}
}

stage('Build Backend') {
steps {
script {
dir('backend') {
// Install the backend dependencies listed in the dependencies.txt file.
sh 'pip install -r dependencies.txt'
}
}
}
}

stage('Build Frontend') {
steps {
script {
dir('frontend') {
// Install the frontend dependencies using npm.
sh 'npm install'
// Build the frontend project.
sh 'npm run build'
}
}
}
}

stage('SonarQube Analysis') {
steps {
script {
// Run SonarQube analysis on the backend code.
withSonarQubeEnv('sonarqube') { // 'sonarqube' is the name of the SonarQube server configuration in Jenkins.
sh "${SONAR_SCANNER_HOME}/bin/sonar-scanner " +
"-Dsonar.projectKey=${SONAR_PROJECT_KEY} " +
"-Dsonar.sources=./backend " +
"-Dsonar.host.url=${SONAR_HOST_URL} " +
"-Dsonar.login=${SONAR_AUTH_TOKEN}"
}
}
}
}

stage('Build Backend Docker Image') {
steps {
script {
// Build the backend Docker image from the Dockerfile in the ./backend directory.
docker.build("${BACKEND_IMAGE}", "./backend")
}
}
}

stage('Build Frontend Docker Image') {
steps {
script {
// Build the frontend Docker image from the Dockerfile in the ./frontend directory.
docker.build("${FRONTEND_IMAGE}", "./frontend")
}
}
}

stage('Push Backend Image to Docker Hub') {
steps {
script {
// Push the backend Docker image to Docker Hub.
docker.withRegistry('https://index.docker.io/v1/', DOCKER_CREDENTIALS_ID) {
docker.image(BACKEND_IMAGE).

3 changes: 3 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dockerfile
.env.example
env.example
33 changes: 33 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Use a lightweight Python image
FROM python:3.10-alpine

# Set the working directory for the container
WORKDIR /app/backend

# Copy the Python dependency file into the container
COPY dependencies.txt .

# Install system dependencies required for your application
RUN apk update && \
apk add --no-cache gcc musl-dev && \
pip install --no-cache-dir gunicorn

RUN pip install --no-cache-dir -r dependencies.txt

# Copy the entire project into the container
COPY . .

# Copy the wait-for-sqlite.sh script into the container and make it executable
COPY wait-for-sqlite.sh /wait-for-sqlite.sh
RUN chmod +x /wait-for-sqlite.sh

# Copy the entrypoint.sh script into the container and make it executable
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

RUN python manage.py collectstatic --noinput

EXPOSE 8000

# Set the container's entrypoint to the entrypoint.sh script
ENTRYPOINT ["/entrypoint.sh"]
8 changes: 4 additions & 4 deletions backend/dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
aiohttp==3.8.5
aiohttp==3.9.0
aiosignal==1.3.1
asgiref==3.7.2
async-timeout==4.0.3
Expand All @@ -18,13 +18,13 @@ djangorestframework==3.14.0
filelock==3.12.4
flake8==6.1.0
freezegun==1.2.2
frozenlist==1.4.0
frozenlist==1.4.1
h11==0.14.0
identify==2.5.30
idna==3.4
isort==5.12.0
mccabe==0.7.0
multidict==6.0.4
multidict==6.0.5
mypy-extensions==1.0.0
nodeenv==1.8.0
openai==0.28.1
Expand All @@ -49,4 +49,4 @@ tzdata==2023.3
urllib3==2.0.5
uvicorn==0.27.1
virtualenv==20.24.5
yarl==1.9.2
yarl==1.9.8rc0
12 changes: 12 additions & 0 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# Main entry point script for the Docker container

# Wait for SQLite
/wait-for-sqlite.sh python manage.py migrate

# Collect static files
python manage.py collectstatic --noinput

# Start Django application with Gunicorn
gunicorn backend.wsgi:application --bind 0.0.0.0:8000
11 changes: 11 additions & 0 deletions backend/wait-for-sqlite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Script to wait for SQLite to be ready

set -e

until [ -e /app/backend/db.sqlite3 ]; do
>&2 echo "+ SQLite is not ready"
sleep 1
done

>&2 echo "+ SQLite is ready - exec command"
exec "$@"
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3'

services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- '3000:3000'
volumes:
- ./frontend:/app/frontend
- node_modules:/app/frontend/node_modules
depends_on:
- backend
networks:
custom_network:
aliases:
- fullstack_assignment_react.local

backend:
build:
context: ./backend
dockerfile: Dockerfile
environment:
- DJANGO_SETTINGS_MODULE=backend.settings
ports:
- '8000:8000'
volumes:
- ./backend:/app/backend
- ./static:/app/backend/static
- ./backend/db.sqlite3:/app/backend/db.sqlite3
networks:
custom_network:
aliases:
- fullstack_assignment_django.local

networks:
custom_network:
driver: bridge

volumes:
node_modules:
driver: local
3 changes: 3 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dockerfile
node_modules
npm-debug.log
17 changes: 17 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

# Build application
RUN npm run build

EXPOSE 3000

# Run the application
CMD ["npm","run", "start"]
Loading