Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate API check for updates #4869

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions buildenv/jenkins/API_check
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!groovy

timestamps{
stage('Setup') {
def CHECK_LABEL = params.CHECK_LABEL ?: "ci.role.test&&hw.arch.x86&&sw.os.linux"
println "CHECK_LABEL: ${CHECK_LABEL}"
node(CHECK_LABEL) {
//echo ("clone the SCM GIT repo")
try {
def gitConfig = scm.getUserRemoteConfigs().get(0)
timeout(time: 1, unit: 'HOURS') {
forceCleanWS()
}
checkout scm: [$class: 'GitSCM',
branches: [[name: "${scm.branches[0].name}"]],
extensions: [
[$class: 'CleanBeforeCheckout'],
[$class: 'CloneOption'],
[$class: 'RelativeTargetDirectory', relativeTargetDir: 'aqa-tests']],
userRemoteConfigs: [[url: "${gitConfig.getUrl()}"]]
]
automateAPICheck()
} catch (Exception e) {
// build result may not be updated correctly at the moment (see https://issues.jenkins.io/browse/JENKINS-56402)
// if there is an exception, set currentBuild.result to ABORTED/FAILURE
if (e.toString().contains("The job is completed without further action")){
println("JCKUpdater script returned exit code 2, as no new update available. The job is completed without further action.")
currentBuild.result = "SUCCESS"
} else if (e.toString().contains("FlowInterruptedException")) {
currentBuild.result = 'ABORTED'
println("Exception: " + e.toString())
} else {
currentBuild.result = 'FAILURE'
println("Exception: " + e.toString())
}
}
}
}
}
def forceCleanWS() {
try {
cleanWs disableDeferredWipeout: true, deleteDirs: true
} catch (Exception e) {
echo 'Exception: ' + e.toString()
//cleanWs has issue to delete workspace that contains non-ASCII filename in TKG output https://issues.jenkins.io/browse/JENKINS-33478
//cannot delete workspace directly. Otherwise, Jenkins job will abort due to missing workspace
sh "rm -rf ${env.WORKSPACE}/aqa-tests/TKG"
// call cleanWs() again
cleanWs disableDeferredWipeout: true, deleteDirs: true
}
}

def automateAPICheck() {
echo ("checking if new JCK material is available .....")
def ARTIFACTORY_API_URL_OPTION = params.ARTIFACTORY_API_URL ? "-au ${params.ARTIFACTORY_API_URL}": ""
stage('API_CHECK') {
withCredentials([
usernamePassword(credentialsId: "${params.ARTIFACTORY_CREDENTIALS}",
usernameVariable: 'ARTIFACTORY_USER', passwordVariable: 'ARTIFACTORY_CREDENTIALS')
]){
// def currentDirectory = sh(script: 'pwd', returnStdout: true).trim()
dir("${WORKSPACE}/aqa-tests/jck/jck-semiauto-updater"){
def SCRIPT ="./update-check.sh ${ARTIFACTORY_API_URL_OPTION} -at \"\$ARTIFACTORY_CREDENTIALS\" "
def tempOutputFile = "temp_output.txt"
def EXITCODE = sh(script: "${SCRIPT} > ${tempOutputFile}", returnStatus: true)

if (EXITCODE == 0) {
def scriptOutput = readFile(tempOutputFile).trim()
def jdkList = [:]
testJobs = [:]
echo ("Automate API script output:")
echo scriptOutput

jdkList= getJDKList(scriptOutput)
jdkList.each { key, value ->
println("JDK number: $key")
def CHILD_JOB_NAME="JCK_Sync"
def BUILD_URL="${JENKINS_URL}job/${CHILD_JOB_NAME}/"
def CHILD_PARAMS = []
def JCK_GIT_REPO = "[email protected]:runtimes/JCK${key}-unzipped.git"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script is also used by other vendors (e.g. Adoptium), not only ibm, so the link repo cannot be hardcoded. Suggest a root location be passed into the script (jenkins env var) and JCK_GIT_REPO be constructed.

params.each { param ->
def variable = param.value.toString()
if (param.key == "JDK_VERSION") {
variable = "${key}"
CHILD_PARAMS << string(name: param.key, value: variable)
} else if (param.key == "JCK_GIT_REPO") {
variable = "${JCK_GIT_REPO}"
CHILD_PARAMS << string(name: param.key, value: variable)
} else {
if (variable == "true" || variable == "false") {
CHILD_PARAMS << booleanParam(name: param.key, value: variable.toBoolean())
} else {
CHILD_PARAMS << string(name: param.key, value: variable)
}
}
}
//def BUILD_INFO = build job: CHILD_JOB_NAME, parameters: CHILD_PARAMS
testJobs["JCK_Sync${key}"] = {
build job: CHILD_JOB_NAME, parameters: CHILD_PARAMS
}
}
parallel testJobs
} else {
error ("script failed with exit code ${EXITCODE}")
}
}
}
}
}

def getJDKList(scriptOutput)
{
def pattern = /JDK=(.+)/
def matcher = (scriptOutput =~ pattern)
def stringWithoutJDK
if (matcher.find()) {
stringWithoutJDK = matcher.group(1).trim()
}
def commaSeparatedValues = stringWithoutJDK?.split(',')
def keyValueMap = [:]
commaSeparatedValues?.each { value ->
def spaceSeparatedValues = value.trim().split(/\s+/)
def key = spaceSeparatedValues[0]
def remainingPart = spaceSeparatedValues[1..-1].join(' ')
keyValueMap[key] = remainingPart
}
return keyValueMap
}
82 changes: 82 additions & 0 deletions jck/jck-semiauto-updater/update-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# File to store the previous state
PREVIOUS_STATE_FILE="/home/jenkins/previous_state.txt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't assume that the system where this would be run will have a /home/jenkins folder. If appropriate, would the use of ${WORKSPACE} be better? Or maybe setting to the location of the script if the file is only intended for temporary use.


function list_folders_depth {
local folder_url="$1"
local depth="$2"
if [ "$depth" -eq 0 ]; then
return
fi

local children=$(curl -X GET -ks -H X-JFrog-Art-Api:${ARTIFACTORY_TOKEN} $folder_url | grep -o '"uri" : "[^"]*"' | awk -F': "' '{print $2}' | sed 's/"$//' | grep -v "$folder_url$")
for child in $children; do
if [ "$depth" -eq 1 ]; then
echo "$folder_url$child"
else
local child_url="${folder_url}${child}"
list_folders_depth "$child_url" "$((depth - 1))"
fi
done
}

function compare_states {

# Read the previous state from the file, or create an empty file if it doesn't exist
if [ -e "$PREVIOUS_STATE_FILE" ]; then
PREVIOUS_STATE=$(cat "$PREVIOUS_STATE_FILE")
else
PREVIOUS_STATE=""
fi

# Retrieve the current state of the repository
CURRENT_STATE=$(list_folders_depth "$ARTIFACTORY_API_URL" 3)
# Calculate the difference between the initial state and current state
added_folders=$(comm -13 <(echo "$PREVIOUS_STATE" | sort) <(echo "$CURRENT_STATE" | sort))

if [ -n "$added_folders" ]; then

JDK_LIST=()
IFS=$'\n'
for folder_entry in $added_folders; do
JDK=$(echo "$folder_entry" | awk -F'/' '{print $(NF-2)}')
UPDATE=$(echo "$folder_entry" | awk -F'/' '{print $(NF)}')
JDK_LIST+=("$JDK $UPDATE")
done
jdk_values=$(IFS=,; echo "${JDK_LIST[*]}")
echo "Update available for JDK= $jdk_values"
else
echo "No new folders detected."
fi

# Store the current state as the new previous state
echo "$CURRENT_STATE" > "$PREVIOUS_STATE_FILE"
}

parseCommandLineArgs()
{
while [ $# -gt 0 ] && [[ ."$1" = .-* ]] ; do
opt="$1";
shift;
case "$opt" in

"--artifactory_token" | "-at")
ARTIFACTORY_TOKEN="$1"; shift;;

"--artifactory_url" | "-au")
ARTIFACTORY_API_URL="$1"; shift;;

*) echo >&2 "Invalid option: ${opt}"; echo "This option was unrecognized."; usage; exit 1;
esac
done
}

parseCommandLineArgs "$@"

if [ "$ARTIFACTORY_TOKEN" != "" ] && [ "$ARTIFACTORY_API_URL" != "" ] ; then
compare_states
else
echo "Please provide missing arguments"
exit 1
fi