forked from ameersami/ci-cd-pipeline-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
113 lines (102 loc) · 4.85 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
#!/bin/groovy
pipeline {
agent any
tools {
nodejs 'recent node'
}
stages {
stage('Prepare') {
steps {
script {
sh 'npm install yarn -g'
sh 'yarn install'
}
}
}
stage('Test') {
steps {
script {
sh 'yarn test'
}
}
}
stage('Build') {
steps {
script {
sh 'yarn build'
}
}
}
stage('Get JWT Token') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'Portainer', usernameVariable: 'PORTAINER_USERNAME', passwordVariable: 'PORTAINER_PASSWORD')]) {
def json = """
{"Username": "$PORTAINER_USERNAME", "Password": "$PORTAINER_PASSWORD"}
"""
def jwtResponse = httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', validResponseCodes: '200', httpMode: 'POST', ignoreSslErrors: true, consoleLogResponseBody: true, requestBody: json, url: "https://portainer.<yourdomain>.com/api/auth"
def jwtObject = new groovy.json.JsonSlurper().parseText(jwtResponse.getContent())
env.JWTTOKEN = "Bearer ${jwtObject.jwt}"
}
}
echo "${env.JWTTOKEN}"
}
}
stage('Build Docker Image on Portainer') {
steps {
script {
// Build the image
withCredentials([usernamePassword(credentialsId: 'Github', usernameVariable: 'GITHUB_USERNAME', passwordVariable: 'GITHUB_PASSWORD')]) {
def repoURL = """
https://portainer.<yourdomain>.com/api/endpoints/1/docker/build?t=reactApp:latest&remote=https://$GITHUB_USERNAME:$GITHUB_PASSWORD@github.com/$GITHUB_USERNAME/react-bolierplate.git&dockerfile=Dockerfile&nocache=true
"""
def imageResponse = httpRequest httpMode: 'POST', ignoreSslErrors: true, url: repoURL, validResponseCodes: '200', customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
}
}
}
}
stage('Delete old Stack') {
steps {
script {
// Get all stacks
String existingStackId = ""
if("true") {
def stackResponse = httpRequest httpMode: 'GET', ignoreSslErrors: true, url: "https://portainer.<yourdomain>.com/api/stacks", validResponseCodes: '200', consoleLogResponseBody: true, customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
def stacks = new groovy.json.JsonSlurper().parseText(stackResponse.getContent())
stacks.each { stack ->
if(stack.Name == "BOILERPLATE") {
existingStackId = stack.Id
}
}
}
if(existingStackId?.trim()) {
// Delete the stack
def stackURL = """
https://portainer.<yourdomain>.com/api/stacks/$existingStackId
"""
httpRequest acceptType: 'APPLICATION_JSON', validResponseCodes: '204', httpMode: 'DELETE', ignoreSslErrors: true, url: stackURL, customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
}
}
}
}
stage('Deploy new stack to Portainer') {
steps {
script {
def createStackJson = ""
// Stack does not exist
// Generate JSON for when the stack is created
withCredentials([usernamePassword(credentialsId: 'Github', usernameVariable: 'GITHUB_USERNAME', passwordVariable: 'GITHUB_PASSWORD')]) {
def swarmResponse = httpRequest acceptType: 'APPLICATION_JSON', validResponseCodes: '200', httpMode: 'GET', ignoreSslErrors: true, consoleLogResponseBody: true, url: "https://portainer.<yourdomain>.com/api/endpoints/1/docker/swarm", customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
def swarmInfo = new groovy.json.JsonSlurper().parseText(swarmResponse.getContent())
createStackJson = """
{"Name": "BOILERPLATE", "SwarmID": "$swarmInfo.ID", "RepositoryURL": "https://github.com/$GITHUB_USERNAME/react-bolierplate", "ComposeFilePathInRepository": "docker-compose.yml", "RepositoryAuthentication": true, "RepositoryUsername": "$GITHUB_USERNAME", "RepositoryPassword": "$GITHUB_PASSWORD"}
"""
}
if(createStackJson?.trim()) {
httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_JSON', validResponseCodes: '200', httpMode: 'POST', ignoreSslErrors: true, consoleLogResponseBody: true, requestBody: createStackJson, url: "https://portainer.<yourdomain>.com/api/stacks?method=repository&type=1&endpointId=1", customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
}
}
}
}
}
}