forked from cloud-native-toolkit/ibm-garage-iteration-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
126 lines (116 loc) · 3.69 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* This is a Jenkins pipeline that relies on the Jenkins kubernetes plugin to dynamically provision agents for
* the build containers.
*
* This pipeline expects three resources to be available in the namespace when it runs. See
* terraform/scripts/create-pipeline-resources.sh for more information
*
* The cloudName variable is set dynamically based on the existance/value of env.CLOUD_NAME which allows this pipeline
* to run in both Kubernetes and OpenShift environments.
*/
def buildAgentName(String jobNameWithNamespace, String buildNumber, String namespace) {
def jobName = removeNamespaceFromJobName(jobNameWithNamespace, namespace);
if (jobName.length() > 55) {
jobName = jobName.substring(0, 55);
}
return "a.${jobName}${buildNumber}".replaceAll('_', '-').replaceAll('/', '-').replaceAll('-.', '.');
}
def removeNamespaceFromJobName(String jobName, String namespace) {
return jobName.replaceAll(namespace + '-', '');
}
def toolsImage="ibmgaragecloud/cli-tools:0.1.1"
def buildLabel = buildAgentName(env.JOB_NAME, env.BUILD_NUMBER, env.NAMESPACE);
def namespace = env.NAMESPACE ?: "dev"
def cloudName = env.CLOUD_NAME == "openshift" ? "openshift" : "kubernetes"
def workingDir = "/home/jenkins/agent"
podTemplate(
label: buildLabel,
cloud: cloudName,
yaml: """
apiVersion: v1
kind: Pod
spec:
serviceAccountName: jenkins
volumes:
- name: terraform-tfvars
configMap:
name: terraform-tfvars
containers:
- name: setup-image
image: alpine:3.9.5
tty: true
command: ["/bin/sh"]
workingDir: ${workingDir}
env:
- name: HOME
value: ${workingDir}
volumeMounts:
- name: terraform-tfvars
mountPath: /etc/settings
- name: tools-image
image: ${toolsImage}
tty: true
command: ["/bin/bash"]
workingDir: ${workingDir}/terraform
env:
- name: HOME
value: ${workingDir}
- name: IBMCLOUD_API_KEY
valueFrom:
secretKeyRef:
name: terraform-credentials
key: "ibmcloud.api.key"
- name: TF_VAR_ibmcloud_api_key
valueFrom:
secretKeyRef:
name: terraform-credentials
key: "ibmcloud.api.key"
- name: IAAS_CLASSIC_USERNAME
valueFrom:
secretKeyRef:
name: terraform-credentials
key: "classic.username"
- name: IAAS_CLASSIC_API_KEY
valueFrom:
secretKeyRef:
name: terraform-credentials
key: "classic.api.key"
"""
) {
node(buildLabel) {
container(name: 'setup-image') {
checkout scm
stage('Copy settings') {
sh '''
set +x
cp -v /etc/settings/* ./terraform/settings
echo "*** Terraform settings copied"
echo ""
echo "-- environment.tfvars --"
cat ./terraform/settings/environment.tfvars
echo ""
echo "-- vlan.tfvars --"
cat ./terraform/settings/vlan.tfvars
'''
}
}
container(name: 'tools-image', shell: '/bin/bash') {
stage('Provision cluster') {
sh '''
set +x
./runTerraform.sh --force
'''
}
stage('Validate cluster') {
sh '''
echo "Nothing to do, yet"
'''
}
stage('Destroy cluster') {
sh '''
echo "Nothing to do, yet"
'''
}
}
}
}