-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
85 lines (82 loc) · 2.13 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
pipeline {
agent any
environment {
// Using returnStdout
gitHash = """${sh(
returnStdout: true,
script: 'git rev-parse HEAD | tr -d "\n"'
)}"""
dockerRegistry = 'harshaisgud/splitcamelcase'
dockerHubCredsID = 'ed3c7524-9c90-4b6f-9d04-625f6fe1e59f'
MY_KUBECONFIG = credentials('kubeconfig')
deploymentNamespace = 'camelcase'
}
stages {
stage('Test Application') {
agent {
docker {
image 'python:alpine3.8'
}
}
steps {
sh '''
python3 -m venv env
source ./env/bin/activate
pip install -r requirements.txt
python3 app_test.py
'''
// TODO: Stash and unstash env folder so that pip install does not need to run twice
}
}
stage('Build Image') {
steps {
echo 'Starting to build docker image'
script {
sh 'docker build . -f Dockerfile --build-arg GITHASH=$gitHash -t $dockerRegistry:$gitHash'
}
}
}
stage('Push Image') {
steps {
echo 'Pushing Image into Registry'
withCredentials([usernamePassword(credentialsId: dockerHubCredsID, passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker login -u $username -p $password'
sh 'docker push $dockerRegistry:$gitHash'
}
}
}
stage('Terraform init') {
steps {
echo 'Deploying Application'
script {
sh 'cd ./terraform && terraform init'
}
}
}
stage('Terraform Apply') {
steps {
echo 'Deploying Application'
script {
sh '''
export TF_VAR_namespace=$deploymentNamespace
export TF_VAR_tag=$gitHash
cd ./terraform && terraform apply --auto-approve
'''
}
}
}
stage('Teardown') {
steps {
echo 'Tearing Down Workspace'
script {
sh '''
docker rmi $dockerRegistry:$gitHash
unset gitHash
unset dockerRegistry
unset dockerHubCredsID
'''
}
}
}
}
}