@@ -32,7 +32,7 @@ def get_sonarqube_unresolved_issues(report_task_file){
3232def getVersion (){
3333 node(){
3434 checkout scm
35- return readTOML( file : ' pyproject.toml' )[' project' ]
35+ return readTOML( file : ' pyproject.toml' )[' project' ][ ' version ' ]
3636 }
3737}
3838
@@ -51,7 +51,7 @@ def call(){
5151 booleanParam(name : ' TEST_PACKAGES' , defaultValue : true , description : ' Test Python packages by installing them and running tests on the installed package' )
5252 booleanParam(name : ' DEPLOY_PYPI' , defaultValue : false , description : ' Deploy to pypi' )
5353 booleanParam(name : ' DEPLOY_DOCS' , defaultValue : false , description : ' ' )
54- booleanParam(name : ' DEPLOY_TO_PRODUCTION ' , defaultValue : false , description : ' Deploy to Production Server ' )
54+ booleanParam(name : ' DEPLOY_DOCKER_IMAGE ' , defaultValue : false , description : ' Create and deploy docker image to Docker registry ' )
5555 }
5656 stages {
5757 stage(' Building and Testing' ){
@@ -579,15 +579,18 @@ def call(){
579579 when{
580580 anyOf {
581581 equals expected : true , actual : params. DEPLOY_PYPI
582- equals expected : true , actual : params. DEPLOY_TO_PRODUCTION
582+ equals expected : true , actual : params. DEPLOY_DOCKER_IMAGE
583583 }
584584 }
585+ options{
586+ lock(' deploy_getmarcapi' )
587+ }
585588 stages{
586589 stage(' Additional Deploy' ) {
587590 when{
588591 anyOf {
589592 equals expected : true , actual : params. DEPLOY_PYPI
590- equals expected : true , actual : params. DEPLOY_TO_PRODUCTION
593+ equals expected : true , actual : params. DEPLOY_DOCKER_IMAGE
591594 }
592595 }
593596 parallel{
@@ -641,7 +644,7 @@ def call(){
641644 }
642645 stage(' Deploy Docker' ){
643646 when{
644- equals expected : true , actual : params. DEPLOY_TO_PRODUCTION
647+ equals expected : true , actual : params. DEPLOY_DOCKER_IMAGE
645648 beforeAgent true
646649 beforeInput true
647650 }
@@ -659,59 +662,51 @@ def call(){
659662 }
660663 steps{
661664 script{
662- withCredentials([string(credentialsId : ' ALMA_API_KEY' , variable : ' API_KEY' )]) {
663- writeFile(
664- file : ' api.cfg' ,
665- text : ''' [ALMA_API]
666- API_DOMAIN=https://api-na.hosted.exlibrisgroup.com
667- API_KEY=${API_KEY}
668- '''
669- )
670- }
665+ def registryUrl
666+ def localImageName
667+ def build_args
668+ def remoteRegistryImageName
671669 configFileProvider([configFile(fileId : ' getmarc_deployapi' , variable : ' CONFIG_FILE' )]) {
672- def CONFIG = readJSON(file : CONFIG_FILE )[' deploy' ]
673- def build_args = CONFIG [' docker' ][' build' ][' buildArgs' ]. collect{" --build-arg=${ it} " }. join(' ' )
674- docker. withRegistry(CONFIG [' docker' ][' server' ][' registry' ], ' jenkins-nexus' ){
675- def dockerImage = docker. build(" ${ IMAGE_NAME} :${ DOCKER_TAG} " , " ${ build_args} ." )
676- dockerImage. push()
677- dockerImage. push(' latest' )
670+ try {
671+ def CONFIG = readJSON(file : CONFIG_FILE )[' deploy' ]
672+ build_args = CONFIG [' docker' ][' build' ][' buildArgs' ]. collect{" --build-arg=${ it} " }. join(' ' )
673+ registryUrl = CONFIG [' docker' ][' server' ][' registry' ]
674+ remoteRegistryImageName = " ${ registryUrl.replace('http://', '').replace('https://', '')} /${ IMAGE_NAME} :${ DOCKER_TAG} "
675+ localImageName = " ${ IMAGE_NAME} :${ DOCKER_TAG} "
676+
677+ } catch (e){
678+ error """ ======================================================
679+ Config file is not valid
680+ ------------------------------------------------------
681+ Details:
682+
683+ ${ e.message}
684+ ------------------------------------------------------
685+ The config file must be a JSON file and be in the following format.
686+
687+ {
688+ "deploy": {
689+ "docker": {
690+ "build": {
691+ "buildArgs": []
692+ },
693+ "server": {
694+ "registry": "FILL THIS OUT WITH YOUR DOCKER REGISTRY URL"
695+ }
696+ }
697+ }
698+ }
699+
700+ ======================================================
701+ """
678702 }
679- }
680- }
681- }
682- }
683- stage(' Deploy to Production server' ){
684- agent{
685- label ' linux && docker'
686- }
687- input {
688- message ' Deploy to live server?'
689- parameters {
690- string defaultValue : ' getmarc2' , description : ' Name of Docker container to use' , name : ' CONTAINER_NAME' , trim : true
691- booleanParam defaultValue : true , description : ' Remove any containers with the same name first' , name : ' REMOVE_EXISTING_CONTAINER'
692- }
693- }
694- options{
695- timeout(time : 1 , unit : ' DAYS' )
696- retry(3 )
697- }
698- steps{
699- script{
700- configFileProvider([configFile(fileId : ' getmarc_deployapi' , variable : ' CONFIG_FILE' )]) {
701- def CONFIG = readJSON(file : CONFIG_FILE ). deploy
702- docker. withServer(CONFIG . docker. server. apiUrl, ' DOCKER_TYKO' ){
703- if (REMOVE_EXISTING_CONTAINER == true ){
704- sh(
705- label :" Stopping ${ CONTAINER_NAME} if exists" ,
706- script : " docker stop ${ CONTAINER_NAME} " ,
707- returnStatus : true
708- )
709- }
710- docker. withRegistry(CONFIG . docker. server. registry, ' jenkins-nexus' ){
711- def imageName = CONFIG . docker. server. registry. replace(' http://' , ' ' ) + " /${ IMAGE_NAME} :${ DOCKER_TAG} "
712- def containerPortsArg = CONFIG . docker. container. ports. collect{" -p ${ it} " }. join(' ' )
713- docker. image(imageName). run(" ${ containerPortsArg} --name ${ CONTAINER_NAME} --rm" )
714- }
703+ docker. withRegistry(registryUrl, ' jenkins-nexus' ){
704+ def dockerImage = docker. build(localImageName, " ${ build_args} ." )
705+ sh(label : ' Uploading docker images to registry' ,
706+ script : """ docker tag ${ localImageName} ${ remoteRegistryImageName}
707+ docker push ${ remoteRegistryImageName}
708+ """
709+ )
715710 }
716711 }
717712 }
@@ -720,6 +715,11 @@ def call(){
720715 }
721716 }
722717 }
718+ post{
719+ always{
720+ milestone 2
721+ }
722+ }
723723 }
724724 }
725725 }
0 commit comments