forked from SofiaNeogalaxy/nginx-cicd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
100 lines (91 loc) · 3.54 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
pipeline {
agent any
parameters {
choice(
name: 'Terraform_Action',
choices: "apply\ndestroy",
description: 'Create or destroy the infrastructure'
)
choice(
name: 'Environment',
choices: "staging\nproduction",
description: 'Environment to setup the infrastructure'
)
}
stages {
stage('Git Checkout') {
steps {
git branch: 'main', credentialsId: 'jenkins_pk', url: '[email protected]:0xsudo/nginx-devops-project.git'
}
}
stage('Docker Build') {
steps {
script {
if (params.Terraform_Action == "apply") {
sh 'docker build -f nginx/Dockerfile -t kaokakelvin/nginx-image:latest -t kaokakelvin/nginx-image:""$BUILD_NUMBER"" --no-cache .'
}
}
}
}
stage('Docker Publish') {
steps {
script {
if (params.Terraform_Action == "apply") {
withDockerRegistry([credentialsId: "devopsrole-dockerhub", url: ""]) {
sh 'docker push kaokakelvin/nginx-image:""$BUILD_NUMBER""'
sh 'docker push kaokakelvin/nginx-image:latest'
}
}
}
}
}
stage('Terraform') {
steps {
script {
if (params.Terraform_Action == "apply" && params.Environment == "staging") {
sh 'terraform -chdir=./terraform/static-web init'
sh 'terraform -chdir=./terraform/static-web apply --auto-approve'
}
if (params.Terraform_Action == "apply" && params.Environment == "production") {
retry(count: 3) {
sh 'terraform -chdir=./terraform/k8s init'
sh 'terraform -chdir=./terraform/k8s apply --auto-approve'
}
}
if (params.Terraform_Action == "destroy" && params.Environment == "staging") {
sh 'terraform -chdir=./terraform/static-web destroy --auto-approve'
}
if (params.Terraform_Action == "destroy" && params.Environment == "production") {
sh 'terraform -chdir=./terraform/k8s destroy --auto-approve'
}
}
}
}
stage('Environment Setup') {
steps {
script {
if (params.Terraform_Action == "apply" && params.Environment == "staging") {
sh './envt_setup.sh'
}
}
}
}
stage('Ansible'){
steps {
script {
if (params.Terraform_Action == "apply" && params.Environment == "staging") {
retry(count: 5) {
// sh 'ansible-playbook -i ansible/inventory-aws_ec2.yaml -i ansible/all_servers_aws_ec2 ansible/ec2-playbook -vvv'
ansiblePlaybook installation: 'ansible', playbook: 'ansible/ec2-playbook', inventory: 'ansible/all_servers_aws_ec2', extras: '--inventory ansible/inventory-aws_ec2.yaml -vvv'
}
}
}
}
}
}
post {
always {
sh 'docker logout'
}
}
}