-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathJenkinsfile
97 lines (87 loc) · 1.84 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
pipeline {
agent any
environment {
TF_IN_AUTOMATION = 'true'
AWS_SHARED_CREDENTIALS_FILE='/root/.aws/credentials'
}
stages {
stage('Init TF') {
steps {
sh '''
ls -al
sed -i "s|/home/dungpham/.aws/credentials|/root/.aws/credentials|g" main.tf
cat main.tf
terraform init
'''
}
}
stage('Plan TF') {
steps {
sh '''
terraform plan
'''
}
}
stage('Validate TF') {
input {
message "Do you want to apply this Plan?"
ok "Apply Plan"
}
steps {
echo 'Plan Accepted'
}
}
stage('Apply TF') {
steps {
sh '''
terraform apply -auto-approve
'''
}
}
stage('Print Inventory') {
steps {
sh '''
echo $(terraform output -json ec2_public_ip) | awk -F'"' '{print $2}' > aws_hosts
cat aws_hosts
'''
}
}
stage('Wait EC2') {
steps {
sh '''
aws ec2 wait instance-status-ok --region ap-southeast-1 --instance-ids `$(terraform output -json ec2_id_test) | awk -F'"' '{print $2}'`
'''
}
}
stage('Validate Ansible') {
input {
message "Do you want to run Ansible Playbook?"
ok "Run Ansible"
}
steps {
echo "Ansible Accepted"
}
}
stage('Run Ansible') {
steps {
ansiblePlaybook(credentialsId: 'ec2.ssh.key ', inventory: 'aws_hosts', playbook: 'ansible/docker.yml')
}
}
stage('Validate Destroy') {
input {
message "Do you want to destroy Terraform Infra?"
ok "Destroy"
}
steps {
echo "Destroy Accepted"
}
}
stage('Destroy TF') {
steps {
sh '''
terraform destroy -auto-approve
'''
}
}
}
}