-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
78 lines (68 loc) · 2.53 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
pipeline {
agent any
environment {
DOCKER_IMAGE = 'docker.io/abdullahabaza/weather_app:latest'
// ANSIBLE_KEY = '/tmp/VMs_Shared_ppk_ID' // Single SSH key for all servers
}
stages {
stage('Checkout Code') {
steps {
git branch: 'main', credentialsId: 'github_cred', url: 'https://github.com/AbdullahAbaza/WeatherApp_CICD.git'
}
}
stage('Dockerize') {
steps {
sh 'docker compose build'
}
}
stage('Push to Docker Hub') {
steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub_cred', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker push ${DOCKER_IMAGE}'
sh 'docker logout'
}
}
}
// stage('Prepare SSH Key') {
// steps {
// withCredentials([sshUserPrivateKey(credentialsId: 'VMs_Shared_ppk_ID', keyFileVariable: 'KEY_FILE')]) {
// sh """
// cp \$KEY_FILE ${env.ANSIBLE_KEY}
// chmod 600 ${env.ANSIBLE_KEY}
// """
// }
// }
// }
// stage('Run Ansible Playbook Deploy') {
// steps {
// sh """
// cd ./Ansible/
// ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i inventory playbook.yml -v \
// --private-key ${env.ANSIBLE_KEY}
// """
// }
// }
stage("Run Ansible Playbook to Deploy using Jenkins Plugin") {
steps {
ansiblePlaybook credentialsId: 'VMs_Shared_ppk_ID',
disableHostKeyChecking: true,
installation: 'Ansible',
inventory: './Ansible/inventory',
playbook: './Ansible/playbook.yml'
}
}
// stage('Cleanup SSH Key') {
// steps {
// sh """
// rm -f ${env.ANSIBLE_KEY}
// """
// }
// }
}
// post {
// always {
// sh "rm -f ${env.ANSIBLE_KEY}" // Clean up SSH key even if the job fails
// }
// }
}