-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
62 lines (56 loc) · 1.92 KB
/
Jenkinsfile
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
pipeline {
agent any
environment {
SERVER_IP = credentials('prod-server-ip')
}
stages {
stage('Setup') {
steps {
sh '''
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
'''
}
}
stage('verify installation') {
steps {
sh '''
pip freeze
'''
}
}
stage('Test') {
steps {
sh '''
echo "test complete"
. venv/bin/activate
pytest test_app.py
'''
}
}
stage('Package code') {
steps {
sh '''
zip -r myapp.zip ./* -x '*.git*'
ls -lart
'''
}
}
stage('Deploy to Prod') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'ssh-key', keyFileVariable: 'MY_SSH_KEY', usernameVariable: 'username')]) {
sh '''
# Copy the application zip file and the deployment script to the EC2 instance
scp -i $MY_SSH_KEY -o StrictHostKeyChecking=no myapp.zip ${username}@${SERVER_IP}:/home/ec2-user/
scp -i $MY_SSH_KEY -o StrictHostKeyChecking=no script.sh ${username}@${SERVER_IP}:/home/ec2-user/
# Execute the deployment script on the EC2 instance
ssh -i $MY_SSH_KEY -o StrictHostKeyChecking=no ${username}@${SERVER_IP} << EOF
chmod +x /home/ec2-user/script.sh
/home/ec2-user/script.sh
'''
}
}
}
}
}