github.com/verrazzano/verrazzano@v1.7.0/release/builds/JenkinsfileForceCandidate (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  def SHORT_COMMIT_HASH
     5  def VERRAZZANO_DEV_VERSION=""
     6  def STARTED_BY_USER=""
     7  
     8  pipeline {
     9      options {
    10          skipDefaultCheckout true
    11          disableConcurrentBuilds()
    12          timestamps ()
    13      }
    14  
    15      agent {
    16         docker {
    17              image "${RELEASE_RUNNER_IMAGE}"
    18              args "${RELEASE_RUNNER_DOCKER_ARGS}"
    19              registryUrl "${RUNNER_DOCKER_REGISTRY_URL}"
    20              registryCredentialsId 'ocir-pull-and-push-account'
    21              label "internal"
    22          }
    23      }
    24  
    25      parameters {
    26          booleanParam (name: 'DRY_RUN',
    27              description: 'Indicate whether this is a DRY run, which will do some stuff but will NOT force a new candidate',
    28              defaultValue: true)
    29          string (name: 'GIT_COMMIT_TO_FORCE_AS_CANDIDATE',
    30              defaultValue: 'NONE',
    31              description: 'This is the full git commit hash to FORCE to be the latest releasable candidate for the current release version on this branch.',
    32              trim: true)
    33          booleanParam (name: 'IGNORE_PRE_RELEASE_VALIDATION_FAILURES',
    34              description: 'Ignore pre-release validation failures',
    35              defaultValue: false)
    36      }
    37  
    38      environment {
    39          OCI_OS_NAMESPACE = credentials('oci-os-namespace')
    40          OCI_OS_BUCKET="verrazzano-builds"
    41          OCI_OS_COMMIT_BUCKET="verrazzano-builds-by-commit"
    42          OCI_CLI_AUTH="api_key"
    43          OCI_CLI_TENANCY = credentials('oci-tenancy')
    44          OCI_CLI_USER = credentials('oci-user-ocid')
    45          OCI_CLI_FINGERPRINT = credentials('oci-api-key-fingerprint')
    46          OCI_CLI_KEY_FILE = credentials('oci-api-key')
    47          OCI_OS_REGION = "us-phoenix-1"
    48          OCI_REGION = "${env.OCI_OS_REGION}"
    49          TIMESTAMP = sh(returnStdout: true, script: "date +%Y%m%d%H%M%S").trim()
    50          CLEAN_BRANCH_NAME = "${env.BRANCH_NAME.replace("/", "%2F")}"
    51          FORCE_TYPE = "${params.DRY_RUN == true ? "DRY_RUN" : "FORCE_FOR_REAL"}"
    52      }
    53  
    54      stages {
    55          stage('Verify Specified Commit can be Forced') {
    56              steps {
    57                  script {
    58                      if (params.GIT_COMMIT_TO_FORCE_AS_CANDIDATE == "NONE") {
    59                          echo "Specific GIT commit was not specified, you must supply an explicit commit"
    60                          sh "exit 1"
    61                      } else {
    62                          scmCheckout()
    63                      }
    64                      echo "SCM checkout of ${env.GIT_BRANCH} at ${env.GIT_COMMIT}"
    65                      SHORT_COMMIT_HASH = sh(returnStdout: true, script: "git rev-parse --short=8 HEAD").trim()
    66                      echo "Short commit hash: $SHORT_COMMIT_HASH"
    67                  }
    68  
    69                  script {
    70                      dir ("${WORKSPACE}") {
    71                          def cleanBranchName = "${env.BRANCH_NAME.replace("/", "%2F")}"
    72                          sh """
    73                            oci --region us-phoenix-1 os object get --namespace ${OCI_OS_NAMESPACE} -bn ${OCI_OS_BUCKET} --name ${cleanBranchName}/current-dev-version.txt --file ${WORKSPACE}/current_dev_version.txt
    74                          """
    75                          def propsDevVersion = readProperties file: "current_dev_version.txt"
    76                          VERRAZZANO_DEV_VERSION = propsDevVersion['verrazzano-development-version']
    77                          println("Current dev version is ${VERRAZZANO_DEV_VERSION}")
    78  
    79                          sh """
    80                            # The existence of the versioned images.txt at the commit specific location in object storage is enough to confirm the commit and version. The update last periodics will also
    81                            # do this as well, but we are doing it here explicitly so that if the commit specified is not correct we can fail here in the initial stage
    82                            oci --region us-phoenix-1 os object head --namespace ${OCI_OS_NAMESPACE} -bn ${OCI_OS_COMMIT_BUCKET} --name ephemeral/${env.BRANCH_NAME}/${SHORT_COMMIT_HASH}/verrazzano_${VERRAZZANO_DEV_VERSION}-images.txt
    83                          """
    84                      }
    85                  }
    86              }
    87          }
    88  
    89          stage('Release Candidate Validation Checks') {
    90              steps {
    91                  script {
    92                      releaseValidationChecks()
    93                  }
    94              }
    95          }
    96  
    97          stage('Force Candidate') {
    98              when {
    99                  expression { !params.DRY_RUN }
   100              }
   101              environment {
   102                  GIT_COMMIT_USED = "${env.GIT_COMMIT}"
   103              }
   104              steps {
   105                  script {
   106                      sh """
   107                          # This will effectively use the commit specified as the last clean periodic test run. This will verify that all of the files required
   108                          # exist in object storage for that commit before copying things across. It will FAIL if that is not the case BEFORE any files are
   109                          # copied in, so if the commit doesn't have the necessary artifacts in Object Storage to do this it can't be used and won't leave us
   110                          # in a half baked state
   111                          ci/scripts/update_last_clean_periodic_test.sh ${VERRAZZANO_DEV_VERSION} ${SHORT_COMMIT_HASH} ${FORCE_TYPE}
   112                      """
   113                  }
   114              }
   115          }
   116      }
   117      post {
   118          always {
   119              script {
   120                  if (notifyInSlack()) {
   121                      slackSend ( message: "User ${STARTED_BY_USER} started run to FORCE ${env.GIT_BRANCH} at ${env.GIT_COMMIT} to be treated as a passing periodic run to allow it to be seen as the last candidate. ${env.BUILD_URL}" )
   122                  }
   123              }
   124              deleteDir()
   125          }
   126      }
   127  }
   128  
   129  def notifyInSlack() {
   130      // Always notify if we are not doing a dry run
   131      return !params.DRY_RUN
   132  }
   133  
   134  def getStartedByUser() {
   135      def startedByUser = "";
   136      def causes = currentBuild.getBuildCauses()
   137      echo "causes: " + causes.toString()
   138      for (cause in causes) {
   139          def causeString = cause.toString()
   140          echo "current cause: " + causeString
   141          def causeInfo = readJSON text: causeString
   142          if (causeInfo.userId != null) {
   143              startedByUser = causeInfo.userId
   144          }
   145      }
   146      return startedByUser
   147  }
   148  
   149  def scmCheckout() {
   150      echo "${NODE_LABELS}"
   151      echo "SCM checkout of ${params.GIT_COMMIT_TO_FORCE_AS_CANDIDATE}"
   152      def scmInfo = checkout([
   153          $class: 'GitSCM',
   154          branches: [[name: params.GIT_COMMIT_TO_FORCE_AS_CANDIDATE]],
   155          doGenerateSubmoduleConfigurations: false,
   156          extensions: [],
   157          submoduleCfg: [],
   158          userRemoteConfigs: [[url: env.SCM_VERRAZZANO_GIT_URL]]])
   159      env.GIT_COMMIT = scmInfo.GIT_COMMIT
   160      env.GIT_BRANCH = scmInfo.GIT_BRANCH
   161      echo "SCM checkout of ${env.GIT_BRANCH} at ${env.GIT_COMMIT}"
   162      // If the commit we were handed is not what the SCM says we are using, fail
   163      if (!env.GIT_COMMIT.equals(params.GIT_COMMIT_TO_FORCE_AS_CANDIDATE)) {
   164          error( "SCM didn't checkout the commit we expected. Expected: ${params.GIT_COMMIT_TO_FORCE_AS_CANDIDATE}, Found: ${scmInfo.GIT_COMMIT}")
   165      }
   166  }
   167  
   168  def releaseValidationChecks() {
   169      def built = build job: "verrazzano-prerelease-check/${CLEAN_BRANCH_NAME}",
   170          parameters: [
   171              string (name: 'COMMIT_TO_USE', value: env.GIT_COMMIT),
   172              booleanParam (name: 'IGNORE_PRE_RELEASE_VALIDATION_FAILURES', value: params.IGNORE_PRE_RELEASE_VALIDATION_FAILURES)
   173          ], wait: true, propagate: false
   174      println("Result of verrazzano-prerelease-check is ${built.result}")
   175      dir ("${WORKSPACE}") {
   176          copyArtifacts(projectName: "verrazzano-prerelease-check/${CLEAN_BRANCH_NAME}",
   177                  selector: specific("${built.number}"));
   178          def releaseStatus = readFile file: "release_status.out"
   179          currentBuild.displayName = "${currentBuild.displayName} : ${releaseStatus}"
   180      }
   181  }