2021-08-05 16:10:31 +02:00
import groovy.json.*
import java.util.*
2022-10-18 14:53:03 +02:00
import com.cloudbees.groovy.cps.NonCPS
2021-08-05 16:10:31 +02:00
IMAGES_DIR = 'images'
2022-10-18 14:53:03 +02:00
TAG_REST_ENDPOINT = "https://api.github.com/repos/${params.DEBEZIUM_REPOSITORY}/tags"
STREAMS_TO_BUILD_COUNT = params . STREAMS_TO_BUILD_COUNT . toInteger ( )
TAGS_PER_STREAM_COUNT = params . TAGS_PER_STREAM_COUNT . toInteger ( )
2021-08-05 16:10:31 +02:00
GIT_CREDENTIALS_ID = 'debezium-github'
DOCKER_CREDENTIALS_ID = 'debezium-dockerhub'
2021-11-23 06:24:36 +01:00
QUAYIO_CREDENTIALS_ID = 'debezium-quay'
2021-08-05 16:10:31 +02:00
class Version implements Comparable {
int major
int minor
int micro
String classifier
def Version ( major , minor ) {
this . major = major
this . minor = minor
this . micro = - 1
}
Version ( name ) {
def parts = name . split ( '\\.' )
major = Integer . parseInt ( parts [ 0 ] )
minor = Integer . parseInt ( parts [ 1 ] )
micro = Integer . parseInt ( parts [ 2 ] )
classifier = parts [ 3 ]
}
@NonCPS
def majorMinor ( ) {
new Version ( major , minor )
}
@Override
@NonCPS
def String toString ( ) {
def sb = new StringBuilder ( )
if ( major ! = - 1 ) {
sb . append ( major )
}
if ( minor ! = - 1 ) {
sb . append ( '.' )
sb . append ( minor )
}
if ( micro ! = - 1 ) {
sb . append ( '.' )
sb . append ( micro )
}
if ( classifier ! = null ) {
sb . append ( '.' )
sb . append ( classifier )
}
sb . toString ( )
}
@Override
@NonCPS
boolean equals ( o ) {
( o = = null ) ? false : toString ( ) = = o . toString ( )
}
@Override
@NonCPS
int hashCode ( ) {
toString ( ) . hashCode ( )
}
@Override
@NonCPS
int compareTo ( o ) {
int d = major - o . major
if ( d ! = 0 ) {
return d
}
d = minor - o . minor
if ( d ! = 0 ) {
return d
}
d = micro - o . micro
if ( d ! = 0 ) {
return d
}
if ( classifier ! = null ) {
return classifier . compareTo ( o . classifier )
}
return 0
}
}
// Map keyed by stream - e.g 1.5 and containing a list of tags per-stream
versions = [ : ] as TreeMap
// The most recent streams to be built
streamsToBuild = null
// The most resent stable stream representing latest tag
stableStream = null
@NonCPS
2021-10-01 15:56:51 +02:00
def initVersions ( username , password ) {
2021-08-05 16:10:31 +02:00
TAG_REST_ENDPOINT . toURL ( ) . openConnection ( ) . with {
doOutput = true
setRequestProperty ( 'Content-Type' , 'application/json' )
2021-10-01 15:56:51 +02:00
setRequestProperty ( 'Authorization' , 'Basic ' + Base64 . encoder . encodeToString ( "$username:$password" . getBytes ( java . nio . charset . StandardCharsets . UTF_8 ) ) )
2021-08-05 16:10:31 +02:00
def json = new JsonSlurper ( ) . parse ( new StringReader ( content . text ) )
json . each {
def current = new Version ( it . name . substring ( 1 ) )
def stream = current . majorMinor ( )
versionList = versions [ stream ]
if ( versionList = = null ) {
versionList = [ ]
versions [ stream ] = versionList
}
versionList . add ( current )
}
}
echo "Versions: $versions"
// Order the tags per stream form the most recent one
versions . each { k , v - >
Collections . sort ( v )
Collections . reverse ( v )
}
// Find the most recent streams and the latest stable
streamsToBuild = [ versions . lastKey ( ) ]
for ( int i = 1 ; i < STREAMS_TO_BUILD_COUNT ; i + + ) {
streamsToBuild . add ( versions . lowerKey ( streamsToBuild [ - 1 ] ) )
}
stableStream = streamsToBuild . find {
def tags = versions [ it ]
tags . size ( ) > 0 & & tags [ 0 ] . classifier = = 'Final'
}
}
node ( 'Slave' ) {
2023-01-04 13:38:34 +01:00
catchError {
2021-08-05 16:10:31 +02:00
stage ( 'Initialize' ) {
dir ( '.' ) {
deleteDir ( )
}
checkout ( [ $class : 'GitSCM' ,
2022-10-18 14:53:03 +02:00
branches : [ [ name: params . IMAGES_BRANCH ] ] ,
2021-08-05 16:10:31 +02:00
doGenerateSubmoduleConfigurations: false ,
extensions : [ [ $class : 'RelativeTargetDirectory' , relativeTargetDir: IMAGES_DIR ] ] ,
submoduleCfg : [ ] ,
2022-10-18 14:53:03 +02:00
userRemoteConfigs : [ [ url: "https://$params.IMAGES_REPOSITORY" , credentialsId: GIT_CREDENTIALS_ID ] ]
2021-08-05 16:10:31 +02:00
]
)
2021-10-01 15:56:51 +02:00
withCredentials ( [ usernamePassword ( credentialsId: GIT_CREDENTIALS_ID , passwordVariable: 'GIT_PASSWORD' , usernameVariable: 'GIT_USERNAME' ) ] ) {
initVersions ( GIT_USERNAME , GIT_PASSWORD )
}
2021-08-05 16:10:31 +02:00
withCredentials ( [ usernamePassword ( credentialsId: DOCKER_CREDENTIALS_ID , passwordVariable: 'DOCKER_PASSWORD' , usernameVariable: 'DOCKER_USERNAME' ) ] ) {
sh "" "
docker login - u $DOCKER_USERNAME - p $DOCKER_PASSWORD
"" "
}
2021-11-23 06:24:36 +01:00
withCredentials ( [ string ( credentialsId: QUAYIO_CREDENTIALS_ID , variable: 'USERNAME_PASSWORD' ) ] ) {
def credentials = USERNAME_PASSWORD . split ( ':' )
sh "" "
set + x
docker login - u $ { credentials [ 0 ] } - p $ { credentials [ 1 ] } quay . io
"" "
}
2022-10-05 12:42:23 +02:00
dir ( IMAGES_DIR ) {
sh "" "
. / setup - local - builder . sh
docker run - - privileged - - rm tonistiigi / binfmt - - install all
"" "
}
sh ""
2021-08-05 16:10:31 +02:00
}
stage ( 'master' ) {
echo "Building images for streams $streamsToBuild"
dir ( IMAGES_DIR ) {
2022-10-05 12:42:23 +02:00
sh "" "
DEBEZIUM_VERSIONS = \ "${streamsToBuild.join(' ')}\" LATEST_STREAM=\"$stableStream\" . / build - all - multiplatform . sh
"" "
2021-08-05 16:10:31 +02:00
}
}
for ( stream in streamsToBuild ) {
for ( tag in versions [ stream ] . take ( TAGS_PER_STREAM_COUNT ) ) {
echo "Building images for tag $tag"
stage ( tag . toString ( ) ) {
dir ( IMAGES_DIR ) {
sh "" "
git checkout v $tag
git fetch origin $IMAGES_BRANCH : $IMAGES_BRANCH
2022-10-05 12:42:23 +02:00
git checkout $IMAGES_BRANCH build - all - multiplatform . sh build - debezium - multiplatform . sh build - mongo - multiplatform . sh build - postgres - multiplatform . sh
RELEASE_TAG = $tag . / build - debezium - multiplatform . sh $stream $MULTIPLATFORM_PLATFORMS
2021-08-05 16:10:31 +02:00
git reset - - hard
"" "
}
}
}
}
}
2023-01-04 13:38:34 +01:00
mail to: params . MAIL_TO , subject: "${env.JOB_NAME} run #${env.BUILD_NUMBER} finished" , body: "Run ${env.BUILD_URL} finished with result: ${currentBuild.currentResult}"
2021-08-05 16:10:31 +02:00
}