-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
92 lines (92 loc) · 2.35 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
pipeline {
agent any
options {
buildDiscarder logRotator(artifactDaysToKeepStr: '30', artifactNumToKeepStr: '7', daysToKeepStr: '30', numToKeepStr: '7')
disableConcurrentBuilds()
disableResume()
}
parameters{
booleanParam(name: "FORCE_INIT", defaultValue: false)
booleanParam(name: "FORCE_DESTROY", defaultValue: false)
}
stages {
stage("Load Configurations"){
steps{
load "jenkins/env/${env.JOB_NAME}.groovy"
dir("jenkins/config"){
git(
url: "$INFRA_CONFIG_GIT_URL",
branch: "main",
credentialsId: "$INFRA_CONFIG_GIT_CREDENTIAL_ID"
)
}
sh "mv jenkins/config/terraform.tfvars ."
sh "mv jenkins/config/backend.tfvars ."
sh "mv jenkins/config/envs.groovy ./jenkins/env"
sh "rm -rf jenkins/config"
load "jenkins/env/envs.groovy"
}
}
stage("Init"){
when{
anyOf{
equals(actual: currentBuild.number,expected: 1)
expression{
return params.FORCE_INIT
}
}
}
steps{
sh "terraform init -backend-config=backend.tfvars"
}
}
stage("Create Workspace & Switch to it"){
when{
equals(actual: currentBuild.number,expected: 1)
}
steps{
sh "terraform workspace new `echo ${env.JOB_NAME} | cut -d \"-\" -f 2` || true"
sh "terraform workspace select `echo ${env.JOB_NAME} | cut -d \"-\" -f 2`"
}
}
stage("Deploy IaC"){
when{
expression{
return !params.FORCE_DESTROY
}
}
steps {
sh "terraform apply -auto-approve"
}
}
stage("Deploy CaC"){
when{
expression{
return !params.FORCE_DESTROY
}
}
steps {
sh "terraform show -json | python3 -m maasta"
ansiblePlaybook (
become: true,
colorized: true,
credentialsId: "$ANSIBLE_SSH_KEY_CREDENTIAL",
disableHostKeyChecking: true,
inventory: "inventory.yaml",
playbook: "deploy.yaml"
)
archiveArtifacts artifacts: 'inventory.yaml', followSymlinks: false
}
}
stage("Destroy"){
when{
expression{
return params.FORCE_DESTROY
}
}
steps{
sh "terraform destroy -auto-approve"
}
}
}
}