Skip to content

Commit

Permalink
Correction on quarkus-platform job (apache#655)
Browse files Browse the repository at this point in the history
* Correction on quarkus-platform job

* corrections

* updates

* update

* correction

* use shared lib for squash commits

* corrected seed test script

* update gha

* test
  • Loading branch information
radtriste authored Oct 17, 2022
1 parent 3aef481 commit 0c3b310
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 96 deletions.
25 changes: 11 additions & 14 deletions .ci/actions/dsl-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ inputs:
repository:
description: author/repository to test
required: false
reference:
description: reference to test
required: false
jobs-folder-path:
description: folder path where to find the jobs.groovy file
required: false
default: .ci/jenkins/dsl
main-config-file-repo:
description: author/repository to find the main config file
required: false
main-config-file-ref:
description: reference to find the main config file
required: false
main-config-file-path:
description: path to find the main config file
required: false
branch-config-name:
description: branch config name to look for
branch-config-file-repo:
description: author/repository to find the branch config file
required: false
branch-config-file-path:
description: path to find the branch config file
required: false
base-branch:
description: base branch for the checks
Expand Down Expand Up @@ -72,15 +69,15 @@ runs:
working-directory: dsl-tests-dir
env:
DSL_DEFAULT_TEST_REPO: ${{ inputs.repository }}
DSL_DEFAULT_TEST_REF: ${{ inputs.reference }}

DSL_DEFAULT_MAIN_CONFIG_FILE_REPO: ${{ inputs.main-config-file-repo }}
DSL_DEFAULT_MAIN_CONFIG_FILE_REF: ${{ inputs.main-config-file-ref }}
DSL_DEFAULT_MAIN_CONFIG_FILE_PATH: ${{ inputs.main-config-file-path }}
DSL_DEFAULT_FALLBACK_MAIN_CONFIG_FILE_REPO: ${{ inputs.main-config-file-repo }}

DSL_DEFAULT_BRANCH_CONFIG_NAME: ${{ github.base_ref }}
DSL_DEFAULT_BRANCH_CONFIG_FILE_PATH: ${{ inputs.branch-config-file-path }}
DSL_DEFAULT_FALLBACK_BRANCH_CONFIG_FILE_REPO: ${{ inputs.branch-config-file-repo }}

DSL_DEFAULT_BRANCH_CONFIG_NAME: ${{ inputs.branch-config-name }}

DSL_DEFAULT_FALLBACK_PIPELINES_REF: ${{ env.BASE_BRANCH }}
DSL_DEFAULT_FALLBACK_SEED_REF: ${{ env.BASE_BRANCH }}
DSL_DEFAULT_PIPELINES_TEST_DIR: ${{ env.PIPELINES_TEST_DIR }}

DSL_DEFAULT_CURRENT_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }}
Expand Down
146 changes: 107 additions & 39 deletions .ci/jenkins/Jenkinsfile.update-quarkus-platform
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import org.jenkinsci.plugins.workflow.libs.Library
@Library('jenkins-pipeline-shared-libraries')_

import org.jenkinsci.plugins.workflow.libs.Library
import org.kie.jenkins.MavenCommand

AGENT_LABEL="kie-rhel7 && kie-mem8g && !built-in"
MVN_TOOL="kie-maven-3.8.6"
JDK_TOOL="kie-jdk11"
VERSION="${params.KOGITO_VERSION}"
COMMAND="${params.COMMAND}"
PROJECT='kogito'
FORK_GIT_AUTOR='kie-ci'
pipelinesBaseBranch="${env.GIT_BRANCH_NAME}"
quarkusBaseBranch='main'
kogitoPipelinesRepo="kogito-pipelines"
quarkusPlatformRepo="quarkus-platform"
branchCreated = false

pipeline {

agent {
label "$AGENT_LABEL"
label "${AGENT_LABEL}"
}

options{
timestamps()
timeout(time: 60, unit: 'MINUTES')
}

tools {
maven "$MVN_TOOL"
jdk "$JDK_TOOL"
maven "${BUILD_MAVEN_TOOL}"
jdk "${BUILD_JDK_TOOL}"
}

environment {
// Static env is defined into .jenkins/dsl/jobs.groovy file

KOGITO_CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
}

stages{
Expand All @@ -36,68 +36,88 @@ pipeline {
cleanWs()
}
}
stage('clone kogito-pipelines') {
stage('Initialize') {
steps{
script{
sh 'printenv'

dir(kogitoPipelinesRepo){
checkoutRepo(kogitoPipelinesRepo, 'kiegroup', pipelinesBaseBranch)
checkoutRepo(kogitoPipelinesRepo, 'kiegroup', getBuildBranch())
}

dir(quarkusPlatformRepo){
checkoutRepo(quarkusPlatformRepo, 'quarkusio', getQuarkusPlatformBranch())
githubscm.forkRepo("${FORK_GIT_AUTHOR_CREDS_ID}")
}
}
}
}
stage('clone quarkus-platform and update kogito version: stage') {
stage('stage') {
when {
expression {COMMAND == 'stage'}
expression { getCommand() == 'stage' }
}
steps {
script {
String prLink = null
String commitMsg = getCommitMessage()
String localBranch = getLocalBranchName()
dir(quarkusPlatformRepo){
checkoutRepo(quarkusPlatformRepo, 'quarkusio', quarkusBaseBranch)
githubscm.forkRepo()
githubscm.createBranch(localBranch)
branchCreated = getOrCreateGitBranch(localBranch, "${FORK_GIT_AUTHOR_CREDS_ID}")

// run update-quarkus-platform.sh with 'stage'
sh "../${kogitoPipelinesRepo}/tools/update-quarkus-platform.sh -s -v ${VERSION} -f ${FORK_GIT_AUTOR} -h ${localBranch} -n -r ${COMMAND}"
sh "${WORKSPACE}/${kogitoPipelinesRepo}/tools/update-quarkus-platform.sh -p ${getProjectName()} -s -v ${getNewVersion()} -f ${FORK_GIT_AUTHOR} -h ${localBranch} -n -r ${getCommand()}"

sh "git status"

// Add changed files, commit and open PR
prLink = commitAndCreatePR(commitMsg, { sh "git add --all" }, localBranch, quarkusBaseBranch)
def prBody = "Generated by Kogito pipelines"

if (githubscm.isThereAnyChanges()) {
githubscm.commitChanges(commitMsg, { sh "git add --all" })
githubscm.pushObject('origin', localBranch, "${FORK_GIT_AUTHOR_CREDS_ID}")
if (branchCreated) {
prLink = githubscm.createPrAsDraft(commitMsg, prBody, getQuarkusPlatformBranch(), "${FORK_GIT_AUTHOR_CREDS_ID}")
sendNotification("Draft PR was created with ${getProjectName()} to version ${getNewVersion()}.\nHere is the PR link: ${prLink}")
} else {
echo "Branch ${localBranch} was already created so assuming the PR exists alrerady ..."
sendNotification("Current PR was updated with ${getProjectName()} to version ${getNewVersion()}.")
}
} else {
println '[WARN] no changes to commit'
}
}
}
}
}
stage('update kogito version: finalize') {
stage('finalize') {
when {
expression {COMMAND == 'finalize'}
expression { getCommand() == 'finalize' }
}
steps {
script {
String commitMsg = getCommitMessage()
String localBranch = getLocalBranchName()
dir(quarkusPlatformRepo){
checkoutRepo(quarkusPlatformRepo, 'quarkusio', quarkusBaseBranch)
githubscm.forkRepo()
sh "git fetch origin"
sh "git checkout ${localBranch}"

// run update-quarkus-platform.sh with 'finalize'
sh "../${kogitoPipelinesRepo}/tools/update-quarkus-platform.sh -s -d -v ${VERSION} -f ${FORK_GIT_AUTOR} -h ${localBranch} -n -r ${COMMAND}"
// squash commits
branchName = sh (script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
mergeName = sh (script: "git merge-base main ${branchName}", returnStdout: true).trim()
sh "git reset ${mergeName}"
sh "git add -A"
sh "git commit -m \"${commitMsg}\" "
withCredentials([usernamePassword(credentialsId: ${FORK_GIT_AUTOR}, usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN')]) {
githubscm.setUserConfig(FORK_GIT_AUTOR)
sh("git config --local credential.helper \"!f() { echo username=\\$GITHUB_USER; echo password=\\$GITHUB_TOKEN; }; f\"")
sh "git push --force-with-lease"
}
sh "${WORKSPACE}/${kogitoPipelinesRepo}/tools/update-quarkus-platform.sh -p ${getProjectName()} -s -d -v ${getNewVersion()} -f ${FORK_GIT_AUTHOR} -h ${localBranch} -n -r ${getCommand()}"

githubscm.squashCommits(getQuarkusPlatformBranch(), commitMsg)

githubscm.pushObject('--force-with-lease origin', localBranch, "${FORK_GIT_AUTHOR_CREDS_ID}")
sendNotification("PR was finalized with ${getProjectName()} to version ${getNewVersion()}.")
}
}
}
}
}
post {
unsuccessful {
sendErrorNotification()
}
}
}

String commitAndCreatePR(String commitMsg, Closure precommit, String localBranch, String targetBranch) {
Expand All @@ -107,16 +127,64 @@ String commitAndCreatePR(String commitMsg, Closure precommit, String localBranch
return githubscm.createPrAsDraft(commitMsg, prBody, targetBranch)
}

/**
* Return true if the branch was created
*/
boolean getOrCreateGitBranch(String branch, String credentialsId) {
sh 'git fetch origin'
String branchRemoteResult = sh(script: "git ls-remote origin ${branch} | wc -l", returnStdout: true).trim()
if (Integer.parseInt(branchRemoteResult) > 0) {
echo "Branch ${branch} already exist ... will not create it. Checking out !"
sh "git checkout origin/${branch} -b ${branch}"
return false
} else {
echo "Branch ${branch} does not exist ... gonna create it"
githubscm.createBranch(branch)
githubscm.pushObject('origin', branch, credentialsId)
return true
}
}

void checkoutRepo(String repo, String GIT_AUTHOR, String branch) {
checkout(githubscm.resolveRepository(repo, GIT_AUTHOR, branch, false))
// need to manually checkout branch since on a detached branch after checkout command
sh "git checkout ${branch}"
}

void sendNotification(String body) {
emailext body: "${body}",
subject: "[${getBuildBranch()}] Quarkus Platform update",
to: env.KOGITO_CI_EMAIL_TO
}

void sendErrorNotification() {
sendNotification("Job #${BUILD_NUMBER} was: **${currentBuild.currentResult}**\nPlease look here: ${BUILD_URL}")
}

String getCommitMessage() {
return "bump up ${PROJECT} to ${VERSION}"
return "Bump up ${getProjectName()} to ${getNewVersion()}"
}

String getLocalBranchName() {
return "bump-${PROJECT}-${VERSION}"
return params.PR_BRANCH ?: "bump-${getProjectName()}-${getNewVersion()}"
}

String getNewVersion() {
return params.NEW_VERSION
}

String getCommand() {
return params.COMMAND
}

String getProjectName() {
return env.PROJECT_NAME
}

String getBuildBranch() {
return env.BUILD_BRANCH_NAME
}

String getQuarkusPlatformBranch() {
return env.QUARKUS_PLATFORM_BRANCH_NAME
}
14 changes: 11 additions & 3 deletions .ci/jenkins/dsl/jobs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ void setupBuildOperatorNode() {

void setupUpdateQuarkusPlatformJob() {
def jobParams = KogitoJobUtils.getBasicJobParams(this, 'update-quarkus-platform', Folder.TOOLS, "${JENKINSFILE_PATH}/Jenkinsfile.update-quarkus-platform", 'Update kogito in quarkus-platform')
KogitoJobUtils.setupJobParamsDefaultMavenConfiguration(this, jobParams)
jobParams.env.putAll([
JENKINS_EMAIL_CREDS_ID: "${JENKINS_EMAIL_CREDS_ID}",
GIT_BRANCH_NAME: "${GIT_BRANCH}",
BUILD_BRANCH_NAME: "${GIT_BRANCH}",
QUARKUS_PLATFORM_BRANCH_NAME: 'main',
PROJECT_NAME: 'kogito',
FORK_GIT_AUTHOR: "${GIT_FORK_AUTHOR_NAME}",
FORK_GIT_AUTHOR_CREDS_ID: "${GIT_FORK_AUTHOR_CREDENTIALS_ID}",
])
KogitoJobTemplate.createPipelineJob(this, jobParams)?.with {
parameters {
stringParam('KOGITO_VERSION', '', 'Kogito version to upgrade as Major.minor.micro.Final')
stringParam('NEW_VERSION', '', 'Which version to set ?')

stringParam('PR_BRANCH', '', '(Optional) Which PR branch name to use ? If none given, a name will be generated automatically.')

choiceParam('COMMAND', ['stage', 'finalize'], 'Choose if you want to use staged artifacts or released artifacts.')
}
}
}
}
6 changes: 2 additions & 4 deletions .github/workflows/jenkins-dsl-downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ jobs:
with:
repository: ${{ matrix.repository }}
main-config-file-repo: kiegroup/kogito-pipelines
main-config-file-ref: main
main-config-file-path: dsl/config/main.yaml
branch-config-name: ${{ github.base_ref }}
branch-config-file-repo: kiegroup/kogito-pipelines
base-branch: ${{ env.BASE_BRANCH }}

#############################################################################
Expand Down Expand Up @@ -328,7 +327,6 @@ jobs:
with:
repository: ${{ matrix.repository }}
main-config-file-repo: kiegroup/optaplanner
main-config-file-ref: main
main-config-file-path: .ci/jenkins/config/main.yaml
branch-config-name: ${{ github.base_ref }}
branch-config-file-repo: kiegroup/optaplanner
base-branch: ${{ env.BASE_BRANCH }}
5 changes: 3 additions & 2 deletions .github/workflows/jenkins-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ jobs:
uses: kiegroup/kogito-pipelines/.ci/actions/dsl-tests@main
with:
main-config-file-repo: kiegroup/kogito-pipelines
main-config-file-ref: main
main-config-file-path: dsl/config/main.yaml
main-config-file-path: dsl/config/main.yaml
branch-config-file-repo: kiegroup/kogito-pipelines

3 changes: 3 additions & 0 deletions dsl/config/branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ git:
bot_author:
name: bsig-gh-bot
credentials_id: bsig-gh-bot
fork_author:
name: kie-ci
credentials_id: kie-ci
jenkins_config_path: .ci/jenkins
maven:
settings_file_id: kogito_release_settings
Expand Down
Loading

0 comments on commit 0c3b310

Please sign in to comment.