From cd609663d2fa9f589bb8c3d955b998fc3cf4826b Mon Sep 17 00:00:00 2001 From: Anton Mesniankin <35063671+anton-mesnyankin@users.noreply.github.com> Date: Thu, 9 May 2024 08:59:43 +0000 Subject: [PATCH] add Jenkinsfiles --- .jenkins/Jenkinsfile | 92 ++++++++++++++++ .jenkins/Jenkinsfile.pullrequest-checker | 131 +++++++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 .jenkins/Jenkinsfile create mode 100644 .jenkins/Jenkinsfile.pullrequest-checker diff --git a/.jenkins/Jenkinsfile b/.jenkins/Jenkinsfile new file mode 100644 index 0000000000..6ec0db39a7 --- /dev/null +++ b/.jenkins/Jenkinsfile @@ -0,0 +1,92 @@ +def projectName='eva-activepieces' +def autoCancelled = false + +script { + buildCause = currentBuild.getBuildCauses()[0].shortDescription + echo buildCause +} + +pipeline { + agent { + label "general" + } + parameters { + choice(name: 'GIT_SOURCE', choices: [env.GIT_SOURCE], description: 'Branch, Tag, CommitId - you need to hardcode the src at the configuration at \'Branch Specifier\'. Please revert the hardcode back after.') + choice(name: 'Env', choices: [env.Env], description: '') + booleanParam(name: 'force_rebuild', defaultValue: false, description: 'this forces docker image rebuild with cache flush') + } + triggers { + githubPush() + } + options { + timestamps() + disableConcurrentBuilds() + disableResume() + parallelsAlwaysFailFast() + buildDiscarder(logRotator(daysToKeepStr: '30')) + timeout(time: 45, unit: 'MINUTES') + } + stages { + stage('Set environment variables'){ + steps { + script { + env.TEST_COMPANY_ID="test" + if (env.Env == 'dev') { + env.PGHOST="devdb.eva.ai" + env.JIRA_ENV_CODE="development" + } + if (env.Env == 'uat') { + env.PGHOST="uatdb.eva.ai" + env.JIRA_ENV_CODE="staging" + } + if (env.Env == 'prd') { + env.PGHOST="prddb.eva.ai" + env.JIRA_ENV_CODE="production" + } + } + sh 'aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 835065412841.dkr.ecr.eu-west-1.amazonaws.com' + } + } + stage('Clean') { + steps { + sh 'rm -f *.xml *.log' + } + } + stage('Build and Publish NPM package...'){ + steps { + withCredentials([usernameColonPassword(credentialsId: 'EVA-ActivePieces', variable: 'EVA_NPMJS_CREDS')]) { + sh ''' + docker build . --build-arg CREDS=//registry.npmjs.org/:_authToken=$EVA_NPMJS_CREDS --build-arg APP_VERSION='1.0.0.'${BUILD_NUMBER} \ + -t 835065412841.dkr.ecr.eu-west-1.amazonaws.com/''' + projectName + ''':${Env}-latest-`git rev-parse HEAD | cut -c1-7`-${BUILD_NUMBER} + ''' + } + } + } + } + post { + success { + slackSend (color: '#00FF00', message: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}] Build cause: '$buildCause' (${env.BUILD_URL})") + cleanWs( + deleteDirs: true, + notFailBuild: true, + patterns: [ + [pattern: '.git/**', type: 'EXCLUDE'] + ] + ) + } + failure { + slackSend (color: '#FF0000', message: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}] Build cause: '$buildCause' @channel' (${env.BUILD_URL})") + } + always { + sh """ + docker logs ${BUILD_TAG} > ${BUILD_TAG}.log 2>&1 || true + docker rmi 835065412841.dkr.ecr.eu-west-1.amazonaws.com/${projectName}:${env.Env}-latest-`git rev-parse HEAD | cut -c1-7`-${BUILD_NUMBER} || true + if [ "${env.Env}" = "prd" ]; then + docker rmi 835065412841.dkr.ecr.eu-west-1.amazonaws.com/${projectName}:${env.Env}-latest || true + fi + docker rmi \$(docker image ls --filter=dangling=true --filter=reference=835065412841.dkr.ecr.eu-west-1.amazonaws.com/${projectName} -q) || true + """ + archiveArtifacts artifacts: '*.log', allowEmptyArchive: true + } + } +} diff --git a/.jenkins/Jenkinsfile.pullrequest-checker b/.jenkins/Jenkinsfile.pullrequest-checker new file mode 100644 index 0000000000..acd0ada0e5 --- /dev/null +++ b/.jenkins/Jenkinsfile.pullrequest-checker @@ -0,0 +1,131 @@ +def projectName='eva-activepieces' + +// Manages communication with github for reporting important status +// events. Relies on Github integration plugin https://github.com/jenkinsci/github-plugin +class GithubStatus { + + // Repository to send status e.g: '/username/repo' + String ghprbGhRepository + + // Commit hash to send status e.g: `909b76f97e` + String ghprbActualCommit + + // Build url send status e.g: `https://jenkins/build/123` + String BUILD_URL + + // Set pending status for this pull request + def setPending(script) { + this.setBuildStatusStep(script, "In Progress", "PENDING") + } + + // Set failed status for this pull request + def setFailed(script) { + this.setBuildStatusStep(script, "Complete", "FAILURE") + } + + // Set success status for this pull request + def setSuccess(script) { + this.setBuildStatusStep(script, "Complete", "SUCCESS") + } + + // Manually set PR status via GitHub integration plugin. Manual status updates are needed + // Due to pipeline stage retries not propagating updates back to Github automatically + // https://github.com/KostyaSha/github-integration-plugin/issues/102#issuecomment-299641073 + def setBuildStatusStep(script, String message, String state) { + def commitContextName = "Jenkins Build and Test" + script.step([ + $class: "GitHubCommitStatusSetter", + reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/${this.ghprbGhRepository}"], + contextSource: [$class: "ManuallyEnteredCommitContextSource", context: commitContextName], + errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]], + commitShaSource: [$class: "ManuallyEnteredShaSource", sha: this.ghprbActualCommit], + statusBackrefSource: [$class: "ManuallyEnteredBackrefSource", backref: "${this.BUILD_URL}flowGraphTable/"], + statusResultSource: [$class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ] + ]) + } +} +def githubStatus = new GithubStatus( + ghprbGhRepository: env.ghprbGhRepository, + ghprbActualCommit: env.ghprbActualCommit, + BUILD_URL: env.BUILD_URL + ) + +script { + buildCause = currentBuild.getBuildCauses()[0].shortDescription + echo buildCause +} + +pipeline { + agent { + label "general" + } + options { + timestamps() + disableResume() + parallelsAlwaysFailFast() + buildDiscarder(logRotator(daysToKeepStr: '15')) + timeout(time: 45, unit: 'MINUTES') + } + parameters { + choice(name: 'Env', choices: ['dev'], description: '') + booleanParam(name: 'force_rebuild', defaultValue: false, description: 'this forces docker image rebuild with cache flush') + } + stages { + stage('Set PENDING GitHub status') { + steps { + script { + githubStatus.setPending(this) + } + } + } + stage('Set environment variables'){ + steps { + script { + env.TEST_COMPANY_ID="test" + env.COMPANIES_LIST="test" + } + } + } + stage('Clean') { + steps { + sh 'rm -f *.xml *.log' + } + } + stage('Build Docker images...'){ + steps { + withCredentials([usernameColonPassword(credentialsId: 'EVA-ActivePieces', variable: 'EVA_NPMJS_CREDS')]) { + sh ''' + docker build . --build-arg CREDS=//npm.pkg.github.com/:_authToken=`echo $EVA_BACKEND_CRED | awk -F: '{print $2}'` \ + -t 835065412841.dkr.ecr.eu-west-1.amazonaws.com/''' + projectName + ''':${Env}-latest-`git rev-parse HEAD | cut -c1-7`-${BUILD_NUMBER} + ''' + } + } + } + } + post { + success { + script { + githubStatus.setSuccess(this) + } + cleanWs( + deleteDirs: true, + notFailBuild: true, + patterns: [ + [pattern: '.git/**', type: 'EXCLUDE'] + ] + ) + } + failure { + script { + githubStatus.setFailed(this) + } + } + always { + sh """ + docker logs ${BUILD_TAG} > ${BUILD_TAG}.log 2>&1 || true + docker rmi 835065412841.dkr.ecr.eu-west-1.amazonaws.com/${projectName}:${env.Env}-latest-`git rev-parse HEAD | cut -c1-7`-${BUILD_NUMBER} || true + """ + archiveArtifacts artifacts: '*.log', allowEmptyArchive: true + } + } +}