-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
75 lines (64 loc) · 3.65 KB
/
Jenkinsfile
File metadata and controls
75 lines (64 loc) · 3.65 KB
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
#!/bin/groovy
pipeline {
agent any
stages {
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.boyutyazilim.com/api/auth"
def jwtObject = new groovy.json.JsonSlurper().parseText(jwtResponse.getContent())
env.JWTTOKEN = "Bearer ${jwtObject.jwt}"
}
}
echo "${env.JWTTOKEN}"
}
}
stage('Delete old Stack') {
steps {
script {
// Get all stacks
String existingStackId = ""
if("true") {
def stackResponse = httpRequest httpMode: 'GET', ignoreSslErrors: true, url: "https://portainer.boyutyazilim.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 == "WORDPRESS") {
existingStackId = stack.Id
}
}
}
if(existingStackId?.trim()) {
// Delete the stack
def stackURL = """
https://portainer.boyutyazilim.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.boyutyazilim.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": "WORDPRESS", "SwarmID": "$swarmInfo.ID", "RepositoryURL": "https://github.com/BytTeam/wordpress", "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.boyutyazilim.com/api/stacks?method=repository&type=1&endpointId=1", customHeaders:[[name:"Authorization", value: env.JWTTOKEN ], [name: "cache-control", value: "no-cache"]]
}
}
}
}
}
}