Jenkins shared library and CDK constructs for AWS infrastructure. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
151 lines
4.2 KiB
Groovy
151 lines
4.2 KiB
Groovy
/**
|
|
* Build and Push Docker Image Pipeline
|
|
*
|
|
* A simple pipeline for building and pushing Docker images to Nexus.
|
|
*
|
|
* Usage in Jenkinsfile:
|
|
* ```groovy
|
|
* @Library(["spicy-automation@main"]) _
|
|
*
|
|
* buildAndPushDockerImage(
|
|
* imageName: 'my-service',
|
|
* )
|
|
* ```
|
|
*
|
|
* With all options:
|
|
* ```groovy
|
|
* @Library(["spicy-automation@main"]) _
|
|
*
|
|
* buildAndPushDockerImage(
|
|
* imageName: 'my-service',
|
|
* dockerfile: 'Dockerfile',
|
|
* context: '.',
|
|
* buildArgs: [NODE_ENV: 'production'],
|
|
* registry: 'nexus.kodeniks.com',
|
|
* repository: 'docker-hosted',
|
|
* credentialsId: 'kodeniks-nexus-repository',
|
|
* tagLatest: true,
|
|
* compareWithLatest: true,
|
|
* pruneAfter: false, // Default false to preserve build cache
|
|
* pruneCache: false, // Whether to prune build cache when pruning
|
|
* )
|
|
* ```
|
|
*/
|
|
|
|
def call(Map args = [:]) {
|
|
def config = [
|
|
imageName: args.imageName ?: args.name ?: env.JOB_NAME?.tokenize('/')?.last() ?: 'application',
|
|
dockerfile: args.dockerfile ?: 'Dockerfile',
|
|
context: args.context ?: '.',
|
|
buildArgs: args.buildArgs ?: [:],
|
|
registry: args.registry ?: 'nexus.kodeniks.com',
|
|
repository: args.repository ?: 'docker-hosted',
|
|
credentialsId: args.credentialsId ?: 'kodeniks-nexus-repository',
|
|
imageTag: args.imageTag ?: null, // null means use git SHA
|
|
tagLatest: args.tagLatest != null ? args.tagLatest : true,
|
|
compareWithLatest: args.compareWithLatest != null ? args.compareWithLatest : true,
|
|
pruneAfter: args.pruneAfter != null ? args.pruneAfter : false, // Default to false to preserve build cache
|
|
pruneCache: args.pruneCache != null ? args.pruneCache : false, // Whether to prune build cache when pruning
|
|
agentLabel: args.agentLabel ?: 'docker',
|
|
]
|
|
|
|
pipeline {
|
|
agent {
|
|
label config.agentLabel
|
|
}
|
|
|
|
stages {
|
|
stage('Checkout') {
|
|
steps {
|
|
checkout scm
|
|
script {
|
|
giteaUtils.setSuccess("checkout")
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build Docker Image') {
|
|
steps {
|
|
script {
|
|
// Use custom tag if provided, otherwise dockerUtils will use git SHA
|
|
def buildArgs = [
|
|
imageName: config.imageName,
|
|
dockerfile: config.dockerfile,
|
|
context: config.context,
|
|
buildArgs: config.buildArgs,
|
|
registry: config.registry,
|
|
repository: config.repository,
|
|
credentialsId: config.credentialsId,
|
|
]
|
|
|
|
if (config.imageTag) {
|
|
buildArgs.imageTag = config.imageTag
|
|
}
|
|
|
|
dockerUtils.buildImage(buildArgs)
|
|
dockerUtils.tagImage(buildArgs + [tagLatest: config.tagLatest])
|
|
giteaUtils.setSuccess("build")
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Push Docker Image') {
|
|
steps {
|
|
script {
|
|
def pushArgs = [
|
|
imageName: config.imageName,
|
|
registry: config.registry,
|
|
repository: config.repository,
|
|
credentialsId: config.credentialsId,
|
|
tagLatest: config.tagLatest,
|
|
compareWithLatest: config.compareWithLatest,
|
|
]
|
|
|
|
if (config.imageTag) {
|
|
pushArgs.imageTag = config.imageTag
|
|
}
|
|
|
|
def pushed = dockerUtils.pushImage(pushArgs)
|
|
|
|
if (pushed) {
|
|
echo "Successfully pushed: ${dockerUtils.getImageRefSHA(pushArgs)}"
|
|
if (config.tagLatest) {
|
|
echo "Also pushed: ${dockerUtils.getImageRefLatest(pushArgs)}"
|
|
}
|
|
} else {
|
|
echo "Image unchanged - push skipped"
|
|
}
|
|
|
|
giteaUtils.setSuccess("push")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always {
|
|
script {
|
|
if (config.pruneAfter) {
|
|
dockerUtils.prune([pruneCache: config.pruneCache])
|
|
}
|
|
}
|
|
}
|
|
success {
|
|
script {
|
|
giteaUtils.setSuccess("pipeline")
|
|
echo "Docker image build and push completed successfully!"
|
|
}
|
|
}
|
|
failure {
|
|
script {
|
|
giteaUtils.setFailed("pipeline")
|
|
echo "Build failed!"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return this
|
|
|