gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/ci/build_release_windows_images.ps1 (about)

     1  Set-StrictMode -Version latest
     2  $ErrorActionPreference = "Stop"
     3  $InformationPreference = "Continue"
     4  # ---------------------------------------------------------------------------
     5  # This script depends on a few environment variables that should be populated
     6  # before running the script:
     7  #
     8  # - $Env:WINDOWS_VERSION - This is the version of windows that is going to be
     9  #   used for building the Docker image. It is important for the version to match
    10  #   one of the Dockerfile suffix, for example, `nanoserver1809` for the Dockerfile
    11  #   `Dockerfile.x86_64_nanoserver1809`
    12  # - $Env:GIT_VERSON - Specify which version of Git needs to be installed on
    13  #   the Docker image. This is done through Docker build args.
    14  # - $Env:GIT_VERSON_BUILD - Specify which build is needed to download for the
    15  #   GIT_VERSION you specified.
    16  # - $Env:GIT_256_CHECKSUM - The checksum of the downloaded zip, usually found in
    17  #   the GitHub release page.
    18  # - $Env:GIT_LFS_VERSION - The Git LFS version needed to install on the
    19  #   Docker image.
    20  # - $Env:GIT_LFS_256_CHECKSUM - The checksum of the downloaded zip, usually
    21  #   found in the GitHub release page.
    22  # - $Env:IS_LATEST - When we want to tag current tag as the latest, this is usually
    23  #   used when we are tagging a release for the runner (which is not a patch
    24  #   release or RC)
    25  # - $Env:DOCKER_HUB_USER - The user we want to login with for docker hub.
    26  # - $Env:DOCKER_HUB_PASSWORD - The password we want to login with for docker hub.
    27  # - $Env:PUSH_TO_DOCKER_HUB - If set to true, it will login to the registry and
    28  #   push the tags.
    29  # ---------------------------------------------------------------------------
    30  $imagesBasePath = "dockerfiles\build\Dockerfile.x86_64"
    31  
    32  function Main
    33  {
    34      $tag = Get-Tag
    35  
    36      Build-Image $tag
    37  
    38      if (-not ($Env:PUSH_TO_DOCKER_HUB -eq "true"))
    39      {
    40          '$Env:PUSH_TO_DOCKER_HUB is not true, done'
    41          return
    42      }
    43  
    44      Connect-Registry
    45  
    46      Push-Tag $tag
    47  
    48      if ($Env:IS_LATEST -eq "true")
    49      {
    50          Add-LatestTag $tag
    51          Push-Latest
    52      }
    53  
    54      Disconnect-Registry
    55  }
    56  
    57  function Get-Tag
    58  {
    59      $revision = & 'git' rev-parse --short=8 HEAD
    60  
    61      return "x86_64-$revision-$Env:WINDOWS_VERSION"
    62  }
    63  
    64  function Build-Image($tag)
    65  {
    66      Write-Information "Build image for x86_64_$Env:WINDOWS_VERSION"
    67  
    68      $dockerFile = "${imagesBasePath}_$Env:WINDOWS_VERSION"
    69      $context = "dockerfiles\build"
    70      $buildArgs = @(
    71          '--build-arg', "GIT_VERSION=$Env:GIT_VERSION",
    72          '--build-arg', "GIT_VERSION_BUILD=$Env:GIT_VERSION_BUILD",
    73          '--build-arg', "GIT_256_CHECKSUM=$Env:GIT_256_CHECKSUM"
    74          '--build-arg', "GIT_LFS_VERSION=$Env:GIT_LFS_VERSION"
    75          '--build-arg', "GIT_LFS_256_CHECKSUM=$Env:GIT_LFS_256_CHECKSUM"
    76      )
    77  
    78      & 'docker' build -t "gitlab/gitlab-runner-helper:$tag" --force-rm $buildArgs -f $dockerFile $context
    79      if ($LASTEXITCODE -ne 0) {
    80          throw ("Failed to build docker image" )
    81      }
    82  }
    83  
    84  function Push-Tag($tag)
    85  {
    86      Write-Information "Push $tag"
    87  
    88      & 'docker' push gitlab/gitlab-runner-helper:$tag
    89      if ($LASTEXITCODE -ne 0) {
    90          throw ("Failed to push docker image gitlab/gitlab-runner-helper:$tag" )
    91      }
    92  }
    93  
    94  function Add-LatestTag($tag)
    95  {
    96      Write-Information "Tag $tag as latest"
    97  
    98      & 'docker' tag "gitlab/gitlab-runner-helper:$tag" "gitlab/gitlab-runner-helper:x86_64-latest-$Env:WINDOWS_VERSION"
    99      if ($LASTEXITCODE -ne 0) {
   100          throw ("Failed to tag gitlab/gitlab-runner-helper:$tag as latest image" )
   101      }
   102  }
   103  
   104  function Push-Latest()
   105  {
   106      Write-Information "Push latest tag"
   107  
   108      & 'docker' push "gitlab/gitlab-runner-helper:x86_64-latest-$Env:WINDOWS_VERSION"
   109      if ($LASTEXITCODE -ne 0) {
   110          throw ("Failed to push image to registry" )
   111      }
   112  }
   113  
   114  function Connect-Registry
   115  {
   116      Write-Information 'Login docker hub'
   117  
   118      & 'docker' login --username $Env:DOCKER_HUB_USER --password $Env:DOCKER_HUB_PASSWORD
   119      if ($LASTEXITCODE -ne 0) {
   120          throw ("Failed to login Docker hub" )
   121      }
   122  }
   123  
   124  function Disconnect-Registry
   125  {
   126      Write-Information 'Logout register'
   127  
   128      & 'docker' logout
   129      if ($LASTEXITCODE -ne 0) {
   130          throw ("Failed to logout from Docker hub" )
   131      }
   132  }
   133  
   134  Try
   135  {
   136      if (-not (Test-Path env:WINDOWS_VERSION))
   137      {
   138          throw '$Env:WINDOWS_VERSION is not set'
   139      }
   140  
   141      Main
   142  } 
   143  Finally 
   144  {
   145      if (-not (Test-Path env:SKIP_CLEANUP))
   146      {
   147          Write-Information "Cleaning up the build image"
   148          $tag = Get-Tag
   149  
   150          # We don't really care if these fail or not, clean up shouldn't fail
   151          # the pipelines.
   152          & 'docker' rmi -f gitlab/gitlab-runner-helper:$tag
   153          & 'docker' image prune -f
   154      }
   155  }