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