Skip to content

Commit

Permalink
ci: only use affected to make pipelines simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
spark-sse committed Sep 3, 2024
1 parent 89b62de commit d28598d
Showing 1 changed file with 91 additions and 104 deletions.
195 changes: 91 additions & 104 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,144 +1,131 @@
@Library('web-service-helper-lib') _

pipeline {
agent { label 'docker' }

agent any
environment {
NODE_DOCKER_IMAGE = 'node:21-bullseye'

NX_BASE='master'
NX_HEAD='HEAD'
NX_BASE = 'master'
NX_HEAD = 'HEAD'
TARGET_PREFIX = 'e-learning-by-sse/nm-self-learning'

API_VERSION = packageJson.getVersion() // package.json must be in root level in order for this to work
NX_BRANCH = env.BRANCH_NAME.replace('PR-', '')

API_VERSION = packageJson.getVersion() // package.json must be in the root level in order for this to work
NPM_TOKEN = credentials('GitHub-NPM')
}

options {
ansiColor('xterm')
}

stages {
stage("NodeJS Build") {
stages {
stage('Prepare ') {
agent {
docker {
image "${env.NODE_DOCKER_IMAGE}"
reuseNode true
args '--tmpfs /.cache -v $HOME/.npm:/.npm'
image "${NODE_DOCKER_IMAGE}"
args '-v /var/run/docker.sock:/var/run/docker.sock'
reuseNode true // This is important to enable the use of the docker socket in sidecar pattern later
}
}
environment {
NPM_TOKEN = credentials('GitHub-NPM')
}
steps {
sh 'git fetch origin master:master'
sh 'cp .npmrc.example .npmrc'
sh 'npm ci --force'
sh 'cp -f .npmrc.example .npmrc'
sh 'cp -f .env.example .env'
echo "TagBuild: ${buildingTag()}"
script {
if (env.BRANCH_NAME =='master') {
sh 'npm run build'
} else {
sh 'npm run build:affected'
}
}
sh 'npm ci'
}
}
stage('Tests') {
stage('Pipeline') {
environment {
POSTGRES_DB = 'SelfLearningDb'
POSTGRES_USER = 'username'
POSTGRES_PASSWORD = 'password'
DATABASE_URL = "postgresql://${env.POSTGRES_USER}:${env.POSTGRES_PASSWORD}@db:5432/${env.POSTGRES_DB}"
}
steps {
script {
withPostgres([ dbUser: env.POSTGRES_USER, dbPassword: env.POSTGRES_PASSWORD, dbName: env.POSTGRES_DB ])
.insideSidecar("${NODE_DOCKER_IMAGE}", '--tmpfs /.cache -v $HOME/.npm:/.npm') {
sh 'npm run prisma db push'
if (env.BRANCH_NAME =='master') {
sh 'npm run test:ci:full'
} else {
sh 'npm run test:ci:affected'
parallel {
stage('Master') {
when {
branch 'master'
}
steps {
// This line enables distribution
// The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
// sh "npx nx-cloud start-ci-run --distribute-on='3 linux-medium-js' --stop-agents-after='e2e-ci'"
script {
withPostgres([dbUser: env.POSTGRES_USER, dbPassword: env.POSTGRES_PASSWORD, dbName: env.POSTGRES_DB])
.insideSidecar("${NODE_DOCKER_IMAGE}", '--tmpfs /.cache -v $HOME/.npm:/.npm') {
sh 'npm run prisma:seed'
sh "npx nx-cloud record -- nx format:check"
sh "npx nx affected --base=HEAD~1 -t lint test build e2e-ci"
}
}
ssedocker {
create {
target "${env.TARGET_PREFIX}:unstable"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"
}
publish {}
}
sh 'npm run prisma:seed'
}
}
}
post {
success {
script {
if (env.BRANCH_NAME == 'master') {
// Test Results
junit 'output/test/junit*.xml'
// Coverage
discoverReferenceBuild()
recordCoverage(qualityGates: [[metric: 'LINE', threshold: 1.0], [metric: 'BRANCH', threshold: 1.0]],
tools: [[parser: 'COBERTURA', pattern: 'output/test/coverage/cobertura-coverage.xml'], [parser: 'JUNIT', pattern: 'output/test/junit*.xml']],
sourceDirectories: [[path: 'libs'], [path: 'apps/site/pages'], [path: 'apps/site/components']])
post {
success {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
staging02ssh "bash /opt/update-compose-project.sh selflearn-staging"
}
}
}
}
}
}
stage('Publish Tagged Release') {
when {
buildingTag()
}
steps {
ssedocker {
create {
target "${env.TARGET_PREFIX}:latest"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"

stage('PR') {
when {
changeRequest()
}
publish {
tag "${env.API_VERSION}"
environment {
VERSION = "${env.API_VERSION}.${env.BRANCH_NAME.split('_')[-1]}"
}
}
}
}
stage('Publish and Deploy Unstable') {
when {
branch 'master'
}
steps {
ssedocker {
create {
target "${env.TARGET_PREFIX}:unstable"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"
steps {
// This line enables distribution
// The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
// sh "npx nx-cloud start-ci-run --distribute-on='3 linux-medium-js' --stop-agents-after='e2e-ci'"
script {
withPostgres([dbUser: env.POSTGRES_USER, dbPassword: env.POSTGRES_PASSWORD, dbName: env.POSTGRES_DB])
.insideSidecar("${NODE_DOCKER_IMAGE}", '--tmpfs /.cache -v $HOME/.npm:/.npm') {
sh 'npm run prisma:seed'
sh "npx nx-cloud record -- nx format:check"
sh "npx nx affected --base origin/${env.CHANGE_TARGET} -t lint test build e2e-c"
}
}
ssedocker {
create {
target "${env.TARGET_PREFIX}:${env.VERSION}"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"
}
publish {}
}
}
publish {}
}
}
post {
success {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
staging02ssh "bash /opt/update-compose-project.sh selflearn-staging"
post {
success {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
staging02ssh "python3 /opt/selflearn-branches/demo-manager.py new-container:${env.VERSION}:${env.BRANCH_NAME} generate-html"
}
}
}
}
}
}
stage('Docker Publish Pull Requests') {
environment {
VERSION = "${env.API_VERSION}.${env.BRANCH_NAME.split('_')[-1]}"
}
when {
anyOf {
expression { env.BRANCH_NAME.endsWith("_pb") }
changeRequest() // pull requests
}
}
steps {
ssedocker {
create {
target "${env.TARGET_PREFIX}:${env.VERSION}"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"

stage('Release Tag') {
when {
buildingTag()
}
publish {}
}
}
post {
success {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
staging02ssh "python3 /opt/selflearn-branches/demo-manager.py new-container:${env.VERSION}:${env.BRANCH_NAME} generate-html"

steps {
ssedocker {
create {
target "${env.TARGET_PREFIX}:latest"
args "--build-arg NPM_TOKEN=${env.NPM_TOKEN}"
}
publish {
tag "${env.API_VERSION}"
}
}
}
}
}
Expand Down

0 comments on commit d28598d

Please sign in to comment.