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

     1  package com.sap.piper.versioning
     2  
     3  class DockerArtifactVersioning extends ArtifactVersioning {
     4      protected DockerArtifactVersioning(script, configuration) {
     5          super(script, configuration)
     6      }
     7  
     8      def getVersion() {
     9          if(configuration.artifactType == 'appContainer' && configuration.dockerVersionSource == 'appVersion'){
    10              //replace + sign if available since + is not allowed in a Docker tag
    11              if (script.commonPipelineEnvironment.getArtifactVersion()){
    12                  return script.commonPipelineEnvironment.getArtifactVersion().replace('+', '_')
    13              }else{
    14                  throw new IllegalArgumentException("No artifact version available for 'dockerVersionSource: appVersion' -> executeBuild needs to run for the application artifact first to set the appVersion attribute.'")
    15              }
    16          } else if (configuration.dockerVersionSource == 'FROM') {
    17              def version = getVersionFromDockerBaseImageTag(configuration.filePath)
    18              if (version) {
    19                  return  getVersionFromDockerBaseImageTag(configuration.filePath)
    20              } else {
    21                  throw new IllegalArgumentException("No version information available in FROM statement")
    22              }
    23          } else {
    24              def version = getVersionFromDockerEnvVariable(configuration.filePath, configuration.dockerVersionSource)
    25              if (version) {
    26                  return version
    27              } else {
    28                  throw new IllegalArgumentException("ENV variable '${configuration.dockerVersionSource}' not found.")
    29              }
    30          }
    31      }
    32  
    33      @Override
    34      def setVersion(version) {
    35          def dockerVersionDir = (configuration.dockerVersionDir?dockerVersionDir:'')
    36          script.dir(dockerVersionDir) {
    37              script.writeFile file:'VERSION', text: version
    38          }
    39      }
    40  
    41      def getVersionFromDockerEnvVariable(filePath, envVarName) {
    42          def lines = script.readFile(filePath).split('\n')
    43          def version = ''
    44          for (def i = 0; i < lines.size(); i++) {
    45              if (lines[i].startsWith('ENV') && lines[i].split(' ')[1] == envVarName) {
    46                  version = lines[i].split(' ')[2]
    47                  break
    48              }
    49          }
    50          echo("Version from Docker environment variable ${envVarName}: ${version}")
    51          return version.trim()
    52      }
    53  
    54      def getVersionFromDockerBaseImageTag(filePath) {
    55          def lines = script.readFile(filePath).split('\n')
    56          def version = null
    57          for (def i = 0; i < lines.size(); i++) {
    58              if (lines[i].startsWith('FROM') && lines[i].indexOf(':') > 0) {
    59                  def imageParts = lines[i].split(':')
    60                  version = imageParts[imageParts.size()-1]
    61                  if (version.contains('/')) {
    62                      script.error "[${getClass().getSimpleName()}] FROM statement does not contain an explicit image version: ${lines[i]} "
    63                  }
    64                  break
    65              }
    66          }
    67          echo("Version from Docker base image tag: ${version}")
    68          return version.trim()
    69      }
    70  }