-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
108 lines (97 loc) · 3.56 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env groovy
library identifier: 'jenkins-shared-library@master', retriver: modernSCM(
[
$class: 'GitSCMSource',
remote: 'https://github.com/mahmoudk1000/jenkins-shared-library.git',
]
)
pipeline {
agent any
environment {
IMAGE_NAME = "mahmoudk1000/app:v1.0"
ANSIBLE_SERVER = ""
}
stages {
stage('Test App') {
steps {
script {
echo 'Testing application...'
dir('src') {
sh 'python3 -m venv venv'
sh 'source venv/bin/activate'
sh 'pip install -r requirements.txt'
sh 'python -m pytest app.py'
}
}
}
}
stage('Build App') {
steps {
script {
echo 'Building application...'
sh 'python ./src/setup.py sdist'
}
}
}
stage('Build Dokcer Image') {
steps {
script {
echo 'Build docker image...'
dockerLogin()
buildDockerImage(env.IMAGE_NAME, "Dockerfile")
dockerPush(env.IMAGE_NAME)
}
}
}
stage('Provision ec2 Instance using Terraform') {
steps {
script {
dir('terraform') {
sh 'terraform init'
sh 'terraform apply --auto-approve'
}
}
}
}
stage('Configure ec2 Instance using Ansbile') {
steps {
stage('Copy necessary file to Ansbile') {
script {
echo "Copy necessary files to Ansbile control node"
sshagent(['ansible-server-key']) {
sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root"
withCredentials([sshUserPrivateKey(credetialsId: 'ec2-server-key', keyFileVariable: 'keyfile', userVariable: 'user')]) {
sh 'scp $keyfile root@$ANSIBLE_SERVER:/root/ssh-key.pem'
}
}
}
}
stage('Executing Ansbile play-book') {
script{
echo "Run Ansbile play-book"
def remote = [:]
remote.name = "ansible-server"
remote.host = env.ANSIBLE_SERVER
remote.allowAnyHosts = true
withCredentials([sshUserPrivateKey(credetialsId: 'ansible-server-key', keyFileVariable: 'keyfile', userVariable: 'user')]) {
remote.user = user
remote.identityFile = keyfile
sshScript remote: remote, script: "./ansible/prepare_ansible_server.sh"
sshCommand remote: remote, command: "ansible-playbook deploy_app.yaml"
}
}
}
}
}
stage('Deply') {
steps {
script {
echo 'Deploy application image to server...'
sshagent(['ec2-server-key']) {
sh "scp server Dockerfile ${ec2Instance}:/home/ec2-user"
}
}
}
}
}
}