github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/DockerUtils.groovy (about)

     1  package com.sap.piper
     2  
     3  class DockerUtils implements Serializable {
     4  
     5      private static Script script
     6  
     7      DockerUtils(Script script) {
     8          this.script = script
     9      }
    10  
    11      public boolean withDockerDaemon() {
    12          def returnCode = script.sh script: 'docker ps -q > /dev/null', returnStatus: true
    13          return (returnCode == 0)
    14      }
    15  
    16      public boolean onKubernetes() {
    17          return (Boolean.valueOf(script.env.ON_K8S))
    18      }
    19  
    20      public String getRegistryFromUrl(dockerRegistryUrl) {
    21          URL url = new URL(dockerRegistryUrl)
    22          return "${url.getHost()}${(url.getPort() != -1) ? ':' + url.getPort() : ''}"
    23      }
    24  
    25      public String getProtocolFromUrl(dockerRegistryUrl) {
    26          URL url = new URL(dockerRegistryUrl)
    27          return url.getProtocol()
    28  
    29          //return dockerRegistryUrl.split(/:\/\//)[0]
    30      }
    31  
    32      public void moveImage(Map source, Map target) {
    33          //expects source/target in the format [image: '', registryUrl: '', credentialsId: '']
    34          def sourceDockerRegistry = source.registryUrl ? "${getRegistryFromUrl(source.registryUrl)}/" : ''
    35          def sourceImageFullName = sourceDockerRegistry + source.image
    36          def targetDockerRegistry = target.registryUrl ? "${getRegistryFromUrl(target.registryUrl)}/" : ''
    37          def targetImageFullName = targetDockerRegistry + target.image
    38  
    39          if (!withDockerDaemon()) {
    40              if (source.credentialsId) {
    41                  script.withCredentials([
    42                      script.usernamePassword(credentialsId: source.credentialsId, passwordVariable: 'src_password', usernameVariable: 'src_userid'), 
    43                      script.usernamePassword(credentialsId: target.credentialsId, passwordVariable: 'password', usernameVariable: 'userid')
    44                  ]) {
    45                      skopeoMoveImage(sourceImageFullName, script.src_userid, script.src_password, targetImageFullName, script.userid, script.password)
    46                  }
    47              } else {
    48                  script.withCredentials([
    49                      script.usernamePassword(credentialsId: target.credentialsId, passwordVariable: 'password', usernameVariable: 'userid')
    50                  ]) {
    51                      skopeoMoveImage(sourceImageFullName, '', '', targetImageFullName, script.userid, script.password)
    52                  }
    53              }
    54          }
    55          //else not yet implemented here - available directly via containerPushToRegistry
    56  
    57      }
    58  
    59      private void skopeoMoveImage(sourceImageFullName, sourceUserId, sourcePassword, targetImageFullName, targetUserId, targetPassword) {
    60          if (sourceUserId && sourcePassword) {
    61              script.sh "skopeo copy --multi-arch=all --src-tls-verify=false --src-creds=${BashUtils.quoteAndEscape(sourceUserId)}:${BashUtils.quoteAndEscape(sourcePassword)} --dest-tls-verify=false --dest-creds=${BashUtils.quoteAndEscape(targetUserId)}:${BashUtils.quoteAndEscape(targetPassword)} docker://${sourceImageFullName} docker://${targetImageFullName}"
    62          } else {
    63              script.sh "skopeo copy --multi-arch=all --src-tls-verify=false --dest-tls-verify=false --dest-creds=${BashUtils.quoteAndEscape(targetUserId)}:${BashUtils.quoteAndEscape(targetPassword)} docker://${sourceImageFullName} docker://${targetImageFullName}"
    64          }
    65      }
    66  }