-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.groovy
73 lines (65 loc) · 2.95 KB
/
script.groovy
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
def incrementDataSeedJobVersion(){
echo "Incrementing the Application Version"
def currentVersion = sh(script: "grep 'const version' main.go | awk '{print \$NF}' | tr -d '\"'", returnStdout: true).trim()
// Incrementing the Version
def newVersion = incrementVersion(currentVersion)
// Updating the Version in the Source Code
sh "sed -i 's/const version = \"$currentVersion\"/const version = \"$newVersion\"/' main.go"
// Commit the Changes
sh "git remote add oumayma [email protected]:Cloudees/data-seed-job.git"
sh "git checkout main"
sh "git commit -am 'Increment Version to $newVersion'"
// Setting the New Version as an Environment Variable for Later Use
env.IMAGE_VERSION = newVersion
}
def incrementVersion(currentVersion) {
def versionParts = currentVersion.split("\\.")
def newPatchVersion = versionParts[2].toInteger() + 1
return "${versionParts[0]}.${versionParts[1]}.$newPatchVersion"
}
def buildGoBinary() {
echo "Compiling and Building the Application..."
sh "go build -o data-seed-job-${IMAGE_VERSION}"
}
def buildDockerImage() {
echo "Building the Docker Image..."
sh "docker build -t oumaymacharrad/data-seed-job:${IMAGE_VERSION} ."
}
def pushToDockerHub() {
withCredentials([usernamePassword(credentialsId: "Docker-Hub-Credentials", passwordVariable: "PASS", usernameVariable: "USER")]) {
echo "Pushing the Docker Image to Docker Hub..."
sh "echo $PASS | docker login -u $USER --password-stdin"
sh "docker push oumaymacharrad/data-seed-job:${IMAGE_VERSION}"
}
}
def trivyScan(){
echo "Running Trivy Security Scan..."
sh "trivy image --format template --template '@/usr/local/share/trivy/templates/html.tpl' -o TrivyReport.html oumaymacharrad/data-seed-job:${IMAGE_VERSION} --scanners vuln"
}
def pushToDeploymentGitHub() {
echo "Pushing to Deployment GitHub..."
sh "git clone [email protected]:Cloudees/deployment.git"
def currentVersion = sh(script: "grep 'image: oumaymacharrad/data-seed-job' deployment/microservices/playlist/deployment-playlist.yaml | awk -F: '{print \$3}' | cut -d'@' -f1", returnStdout: true).trim()
env.CURRENT_VERSION = currentVersion
sh "sed -i 's|image: oumaymacharrad/data-seed-job:$CURRENT_VERSION|image: oumaymacharrad/data-seed-job:$IMAGE_VERSION|' deployment/microservices/playlist/deployment-playlist.yaml"
sh "sed -i 's|image: oumaymacharrad/data-seed-job:$CURRENT_VERSION|image: oumaymacharrad/data-seed-job:$IMAGE_VERSION|' deployment/microservices/videos/deployment-videos.yaml"
sh """
cd deployment
git commit -am 'Increment Version to ${IMAGE_VERSION}'
"""
sshagent(credentials: ['Private-Key']) {
sh """
cd deployment
git push origin main
"""
}
sh "rm -rf deployment"
}
def gitpush(){
// Push the Changes to GitHub
sshagent (credentials: ["Private-Key"]) {
sh "git push oumayma main"
sh "git remote remove oumayma"
}
}
return this