-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.groovy
55 lines (53 loc) · 2.07 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
// build function for multiple dockerfiles
// build(docker repo Name , Version , CredentialId from jenkins , list of ImageName and the corresponding DockerFile path)
def build(String repoName ,String version ,String credId , List<List> imageName_dockerfilelocation_pair){
withCredentials([
usernamePassword(
credentialsId: "$credId",
usernameVariable: "USER",
passwordVariable: "PASSWORD"
)
]){
sh "echo $PASSWORD | docker login -u $USER --password-stdin"
for (pair in imageName_dockerfilelocation_pair){
sh "echo building ${pair[0]} image ..."
sh "docker build ${pair[1]} -t $repoName/${pair[0]}:$version"
sh "docker build ${pair[1]} -t $repoName/${pair[0]}:latest"
}
}
}
// push function for multiple dockerfiles
// push(docker repo Name , Version , CredentialId from jenkins , list of ImageNames)
def push(String repoName ,String version ,String credId , List<List> imageNames){
withCredentials([
usernamePassword(
credentialsId: "$credId",
usernameVariable: "USER",
passwordVariable: "PASSWORD"
)
]){
sh "echo $PASSWORD | docker login -u $USER --password-stdin"
for (imageName in imageNames){
sh "echo pushing $imageName ..."
sh "docker push $repoName/$imageName:$version"
sh "docker push $repoName/$imageName:latest"
}
}
}
// commit and push changes to a git repo
// git_push( Git repo url without "https://" , CredentialId from jenkins , commit message , targeted branch)
def git_push(String url , String credId , String commitMsg, String branch){
echo "pushing to $branch ..."
withCredentials([
usernamePassword(
credentialsId:"$credId",
usernameVariable:'USER',
passwordVariable:'TOKEN'
)]){
sh "git remote set-url origin https://${USER}:${TOKEN}@$url"
sh "git status;git add ."
sh "git commit -m \"${commitMsg}\""
sh "git push origin HEAD:$branch"
}
}
return this