Skip to content

Commit b7ededc

Browse files
committed
Setup GitHub Action and Jenkins
1 parent f8353d8 commit b7ededc

5 files changed

Lines changed: 156 additions & 2 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Deploy LMS
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository_owner }}/lms-web
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Log in to the Container registry
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ${{ env.REGISTRY }}
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Extract metadata (tags, labels) for Docker
31+
id: meta
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
35+
tags: |
36+
type=raw,value=latest
37+
type=sha
38+
39+
- name: Build and push Docker image
40+
uses: docker/build-push-action@v5
41+
with:
42+
context: .
43+
file: deployment/Dockerfile
44+
push: true
45+
tags: ${{ steps.meta.outputs.tags }}
46+
labels: ${{ steps.meta.outputs.labels }}
47+
48+
deploy:
49+
needs: build-and-push
50+
runs-on: ubuntu-latest
51+
steps:
52+
- name: Checkout repository
53+
uses: actions/checkout@v4
54+
55+
- name: Deploy to VPS
56+
uses: appleboy/ssh-action@master
57+
with:
58+
host: ${{ secrets.SERVER_IP }}
59+
username: ${{ secrets.SERVER_USER }}
60+
key: ${{ secrets.SSH_PRIVATE_KEY }}
61+
script: |
62+
cd /srv/apps/lms.musfiqdehan.com
63+
64+
# Pull latest code
65+
git pull origin main
66+
67+
# Login to GHCR (optional if images are public, but good practice)
68+
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
69+
70+
# Pull the latest image
71+
docker compose -f deployment/docker-compose.yml pull web
72+
73+
# Restart services
74+
docker compose -f deployment/docker-compose.yml up -d
75+
76+
# Run migrations
77+
docker compose -f deployment/docker-compose.yml exec -T web python manage.py migrate
78+
79+
# Collect static files
80+
docker compose -f deployment/docker-compose.yml exec -T web python manage.py collectstatic --noinput
81+
82+
# Cleanup old images
83+
docker image prune -f

Jenkinsfile

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
pipeline {
2+
agent any
3+
4+
environment {
5+
REGISTRY = 'ghcr.io'
6+
IMAGE_NAME = 'musfiqdehan/lms-web'
7+
IMAGE_TAG = 'latest'
8+
SERVER_IP = "${env.SERVER_IP}"
9+
SERVER_USER = "${env.SERVER_USER}"
10+
APP_PATH = "${env.APP_PATH}"
11+
GITHUB_TOKEN = "${env.GITHUB_TOKEN}"
12+
GITHUB_USERNAME = "${env.GITHUB_USERNAME}"
13+
}
14+
15+
stages {
16+
stage('Checkout') {
17+
steps {
18+
checkout scm
19+
}
20+
}
21+
22+
stage('Build & Push Image') {
23+
steps {
24+
script {
25+
docker.withRegistry("https://${REGISTRY}", 'ghcr-login') {
26+
def customImage = docker.build("${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}", "-f deployment/Dockerfile .")
27+
customImage.push()
28+
customImage.push('latest')
29+
}
30+
}
31+
}
32+
}
33+
34+
stage('Deploy to VPS') {
35+
steps {
36+
sshagent(['vps-ssh-key']) {
37+
sh """
38+
ssh -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} << 'EOF'
39+
cd ${APP_PATH}
40+
41+
# Pull latest code
42+
git pull origin main
43+
44+
# Login and pull latest image
45+
echo "${env.GITHUB_TOKEN}" | docker login ghcr.io -u ${env.GITHUB_USERNAME} --password-stdin
46+
docker compose -f deployment/docker-compose.yml pull web
47+
48+
# Restart services
49+
docker compose -f deployment/docker-compose.yml up -d
50+
51+
# Maintenance
52+
docker compose -f deployment/docker-compose.yml exec -T web python manage.py migrate
53+
docker compose -f deployment/docker-compose.yml exec -T web python manage.py collectstatic --noinput
54+
55+
# Cleanup
56+
docker image prune -f
57+
EOF
58+
"""
59+
}
60+
}
61+
}
62+
}
63+
64+
post {
65+
success {
66+
echo 'Deployment successful!'
67+
}
68+
failure {
69+
echo 'Deployment failed. Please check the logs.'
70+
}
71+
}
72+
}

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

deployment/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
web:
3+
image: ghcr.io/musfiqdehan/lms-web:latest
34
build:
45
context: ..
56
dockerfile: deployment/Dockerfile

runtime.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)