Skip to content

Commit

Permalink
fix: revert back order of stages; feat: read V1/V2 metadata for Docke…
Browse files Browse the repository at this point in the history
…r image build
  • Loading branch information
vykozlov committed Aug 16, 2024
1 parent e12f288 commit bf7d083
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,10 @@ pipeline {
// Get list of AI4OS Hub repositories from "modules-catalog/.gitmodules"
MODULES_CATALOG_URL = "https://raw.githubusercontent.com/ai4os-hub/modules-catalog/master/.gitmodules"
MODULES = sh (returnStdout: true, script: "curl -s ${MODULES_CATALOG_URL}").trim()
METADATA_FILE = "metadata.json"
}

stages {
stage("Check if only metadata files have changed") {
steps {
script {
// Check if only metadata files have been changed

// Get the list of changed files
changed_files = sh (returnStdout: true, script: "git diff --name-only HEAD^ HEAD").trim()

// we need to check here if the change only affects any of the metadata files, but not the code
// we can't use "git diff --name-only HEAD^ HEAD" as it will return all files changed in the commit
// we need to check if the metadata files are present in the list of changed files

need_build = true

// Check if metadata files are present in the list of changed files
if (changed_files.contains("metadata.json") || changed_files.contains("ai4-metadata.json") || changed_files.contains("ai4-metadata.yml")) {
// Convert to an array and pop items
changed_files = changed_files.tokenize()
changed_files.removeAll(["metadata.json", "ai4-metadata.json", "ai4-metadata.yml"])
// now check if the list is empty
if (changed_files.size() == 0) {
need_build = false
}
}
}
}
}

// Let's run user tests for all repos
stage("User-defined module pipeline job") {
when {
anyOf {
expression {need_build}
triggeredBy 'UserIdCause'
}
}
steps {
//sh 'printenv'
script {
build(job: "/AI4OS-HUB-TEST/" + env.JOB_NAME.drop(10))
}
}
}

stage('Metadata tests') {
parallel {
stage('AI4OS Hub metadata V1 validation') {
Expand All @@ -82,6 +39,7 @@ pipeline {
if (!fileExists("metadata.json")) {
error("metadata.json file not found in the repository")
}
env.METADATA_FILE = "metadata.json"
}

withEnv([
Expand Down Expand Up @@ -114,6 +72,7 @@ pipeline {
if (fileExists("ai4-metadata.yml")) {
error("Both ai4-metadata.json and ai4-metadata.yml files found in the repository")
}
env.METADATA_FILE = "ai4-metadata.json"
}
withEnv([
"HOME=${env.WORKSPACE}",
Expand Down Expand Up @@ -147,6 +106,7 @@ pipeline {
// load YAML file, dump as JSON
metadata = readYaml file: "ai4-metadata.yml"
writeJSON file: "ai4-metadata-${BUILD_NUMBER}.json", json: metadata
env.METADATA_FILE = "ai4-metadata-${BUILD_NUMBER}.json"
}
withEnv([
"HOME=${env.WORKSPACE}",
Expand All @@ -171,6 +131,50 @@ pipeline {
}
}

stage("Check if only metadata files have changed") {
steps {
script {
// Check if only metadata files have been changed

// Get the list of changed files
changed_files = sh (returnStdout: true, script: "git diff --name-only HEAD^ HEAD").trim()

// we need to check here if the change only affects any of the metadata files, but not the code
// we can't use "git diff --name-only HEAD^ HEAD" as it will return all files changed in the commit
// we need to check if the metadata files are present in the list of changed files

need_build = true

// Check if metadata files are present in the list of changed files
if (changed_files.contains("metadata.json") || changed_files.contains("ai4-metadata.json") || changed_files.contains("ai4-metadata.yml")) {
// Convert to an array and pop items
changed_files = changed_files.tokenize()
changed_files.removeAll(["metadata.json", "ai4-metadata.json", "ai4-metadata.yml"])
// now check if the list is empty
if (changed_files.size() == 0) {
need_build = false
}
}
}
}
}

// Let's run user tests for all repos
stage("User-defined module pipeline job") {
when {
anyOf {
expression {need_build}
triggeredBy 'UserIdCause'
}
}
steps {
//sh 'printenv'
script {
build(job: "/AI4OS-HUB-TEST/" + env.JOB_NAME.drop(10))
}
}
}

stage("Docker build and delivery") {
when {
expression {env.MODULES.contains(env.THIS_REPO)}
Expand Down Expand Up @@ -227,10 +231,14 @@ pipeline {
}
docker_tag = docker_tag.toLowerCase()

// get docker image name from metadata.json
meta = readJSON file: "metadata.json"
image_name = meta["sources"]["docker_registry_repo"].split("/")[1]

// get docker image name from metadata.json / ai4-metadata.json
meta = readJSON file: env.METADATA_FILE
if (env.METADATA_FILE == "metadata.json") {
image_name = meta["sources"]["docker_registry_repo"].split("/")[1]
} else {
image_name = meta["links"]["docker_image"].split("/")[1]
}

// use preconfigured in Jenkins docker_repository
// XXX may confuse users? (e.g. expect xyz/myimage, but we push to ai4hub/myimage)
image = docker_repository + "/" + image_name + ":" + docker_tag
Expand Down

0 comments on commit bf7d083

Please sign in to comment.