-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tagging functionality to template
- Loading branch information
Showing
1 changed file
with
54 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,70 @@ | ||
def gitVersion() { | ||
desc = sh(script: "git describe --tags --long --dirty", returnStdout: true)?.trim() | ||
parts = desc.split('-') | ||
assert parts.size() in [3, 4] | ||
dirty = (parts.size() == 4) | ||
tag = parts[0] | ||
count = parts[1] | ||
sha = parts[2] | ||
if (count == '0' && !dirty) { | ||
return tag | ||
} | ||
return sprintf( '%1$s.dev%2$s+%3$s', [tag, count, sha.substring(1)]) | ||
} | ||
|
||
def isTag() { | ||
commit = getCommit() | ||
if (commit) { | ||
desc = sh(script: "git describe --tags --long ${commit}", returnStdout: true)?.trim() | ||
match = desc =~ /.+-[0-9]+-g[0-9A-Fa-f]{6,}$/ | ||
result = !match | ||
match = null | ||
return result | ||
} | ||
return false | ||
} | ||
node('docker') { | ||
node('docker') { | ||
stage('Checkout') { | ||
checkout scm | ||
try { | ||
env.VERSION = gitVersion() | ||
echo "Version = ${env.VERSION}" | ||
env.ISTAG = isTag() | ||
echo "Tag = ${env.ISTAG}" | ||
} | ||
catch(Exception e) { | ||
echo "could not read git version! Make sure that you do not make a shallow clone only..." | ||
} | ||
} | ||
stage('Build Image') { | ||
|
||
stage('Image Name') { | ||
withCredentials([ | ||
usernamePassword(credentialsId: 'docker public', | ||
usernameVariable: 'USERNAME', | ||
passwordVariable: 'PASSWORD')]) { | ||
def builtImage = docker.build("${USERNAME}/{{cookiecutter.project_slug}}",".") | ||
def imageName = "${USERNAME}/{{cookiecutter.project_slug}}" | ||
env.DOCKERIMAGENAME = (env.ISTAG=="true") ? "${imageName}:${env.VERSION}" : "${imageName}" | ||
} | ||
echo "Docker Image = ${env.DOCKERIMAGENAME}" | ||
} | ||
|
||
stage('Build Image') { | ||
def builtImage = docker.build(env.DOCKERIMAGENAME,".") | ||
} | ||
|
||
stage('Push Image') { | ||
docker.withRegistry('','docker public') { | ||
withCredentials([ | ||
usernamePassword(credentialsId: 'docker public', | ||
usernameVariable: 'USERNAME', | ||
passwordVariable: 'PASSWORD')]) { | ||
def builtImage = docker.build("${USERNAME}/{{cookiecutter.project_slug}}",".") | ||
builtImage.push() | ||
} | ||
def builtImage = docker.build(env.DOCKERIMAGENAME,".") | ||
builtImage.push() | ||
} | ||
} | ||
} | ||
|
||
def getCommit() { | ||
return sh(script: 'git rev-parse HEAD', returnStdout: true)?.trim() | ||
} | ||
|
||
def gitDescription() { | ||
return sh(script: "git describe --tags --long --dirty", returnStdout: true)?.trim() | ||
} | ||
|
||
def gitVersion() { | ||
desc = gitDescription() | ||
parts = desc.split('-') | ||
assert parts.size() in [3, 4] | ||
dirty = (parts.size() == 4) | ||
tag = parts[0] | ||
count = parts[1] | ||
sha = parts[2] | ||
if (count == '0' && !dirty) { | ||
return tag | ||
} | ||
return sprintf( '%1$s.dev%2$s+%3$s', [tag, count, sha.substring(1)]) | ||
} | ||
|
||
def isTag() { | ||
desc = gitDescription() | ||
parts = desc.split('-') | ||
assert parts.size() in [3, 4] | ||
dirty = (parts.size() == 4) | ||
tag = parts[0] | ||
count = parts[1] | ||
sha = parts[2] | ||
return (count == '0' && !dirty) | ||
} |