Skip to content

Instantly share code, notes, and snippets.

@deskoh
Last active April 30, 2024 03:04
Show Gist options
  • Save deskoh/0cb53f9d99f49196ab2f4d880c4e1310 to your computer and use it in GitHub Desktop.
Save deskoh/0cb53f9d99f49196ab2f4d880c4e1310 to your computer and use it in GitHub Desktop.
Jenkinsfile Examples
stage('Test') {
steps {
script {
sh 'npm run coverage'
}
}
post {
always {
// Publish HTML coverage
// https://jenkins.io/doc/pipeline/steps/htmlpublisher/
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'output/coverage/jest',
reportFiles: 'index.html',
reportName: 'Test Report'
]
// Using Cobertura plugin
// https://jenkins.io/doc/pipeline/steps/cobertura/
cobertura coberturaReportFile: 'output/coverage/jest/cobertura-coverage.xml', maxNumberOfBuilds: 0
cobertura (
coberturaReportFile: 'output/coverage/jest/cobertura-coverage.xml',
autoUpdateStability: false,
failUnhealthy: false,
failUnstable: false,
maxNumberOfBuilds : 0,
onlyStable: false,
sourceEncoding: 'ASCII',
zoomCoverageChart: false
)
}
}
}
stage('Test') {
steps {
script {
sh 'npm run lint'
}
}
post {
always {
// Warnings Next Generation Plugin
// https://jenkins.io/doc/pipeline/steps/warnings-ng/
// https://github.com/jenkinsci/warnings-ng-plugin/blob/master/doc/Documentation.md
// https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md
recordIssues enabledForFailure: true, tools: [esLint()]
recordIssues enabledForFailure: true, tool: esLint()
recordIssues (
enabledForFailure: true
tool: esLint()
)
}
}
}
Jenkinsfile examples
pipeline {
agent { label 'linux' }
stages {
stage('Build') {
agent {
docker {
image 'node:alpine'
reuseNode true
}
}
stages {
stage('Install Dependencies') {
steps {
echo 'Installing dependencies...'
sh 'npm ci'
}
}
stage('Static Analysis') {
parallel {
stage('Lint') {
steps {
echo 'Linting...'
// catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
catchError {
sh 'npm run eslint -- -f checkstyle -o eslint.xml'
}
}
post {
always {
// Warnings Next Generation Plugin
recordIssues enabledForFailure: true, tools: [esLint(pattern: 'eslint.xml')]
}
}
}
stage('Typecheck') {
steps {
echo 'Type Checking...'
sh 'npx tsc -p . --noEmit'
}
}
}
}
stage('Build') {
environment {
NODE_ENV = 'production'
}
steps {
echo 'Building..'
sh 'npm run build'
}
}
}
}
stage('Integration Tests') {
steps {
script {
docker.image('postgres:alpine').withRun('-e "POSTGRES_PASSWORD=password" -e "POSTGRES_DB=test"') {c ->
docker.image('node:alpine').inside("--link ${c.id}:db") {
// sh 'npm ci'
sh 'npm run test'
}
}
}
}
}
}
}
pipeline {
agent none
environment {
CI = true
DEPLOY_DIR = false
}
stages {
stage('Build') {
agent { label 'linux' }
stages {
// Build > Prepare
stage('Prepare') {
steps {
sh 'whoami'
sh 'node --version'
sh 'npm --version'
sh 'npm config list'
}
post {
always {
updateGitlabCommitStatus name: 'jenkins-build', state: 'running'
}
}
}
// Build > Install dependencies
stage('Install dependencies') {
steps {
sh 'npm ci'
}
}
stage('Lint and Build') {
parallel {
// Build > Lint
stage('Lint') {
steps {
// catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')
catchError {
sh 'npm run eslint -- -f checkstyle -o eslint.xml'
}
}
post {
always {
recordIssues enabledForFailure: true, tools: [esLint(id: 'eslint', name: 'ESlint ', pattern: 'eslint.xml')]
}
}
}
// Build > Build
stage('Build') {
steps {
sh 'npm run build'
}
}
}
}
}
}
stage('Integration Tests') {
// Use docker agent for integration test
agent { label 'docker' }
environment {
MONGODB_TEST_URL = 'mongodb://mongoadmin:mongopass@mongodb:27017/feathers-test?authSource=admin'
}
steps {
script {
docker.image('mongo')
.withRun('-e "MONGO_INITDB_ROOT_USERNAME=mongoadmin" -e "MONGO_INITDB_ROOT_PASSWORD=mongopass"') {c ->
docker.image('node:10-alpine').inside("--link ${c.id}:mongodb") {
sh 'npm run test'
}
}
}
}
post {
success {
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'coverage/lcov-report',
reportFiles: 'index.html',
reportName: 'Coverage Report (NYC)',
reportTitles: ''
]
cobertura (
autoUpdateHealth: false,
autoUpdateStability: false,
coberturaReportFile: 'coverage/cobertura-coverage.xml',
conditionalCoverageTargets: '70, 0, 0',
failNoReports: false,
failUnhealthy: false,
failUnstable: false,
lineCoverageTargets: '80, 0, 0',
maxNumberOfBuilds: 0,
methodCoverageTargets: '80, 0, 0',
sourceEncoding: 'ASCII',
zoomCoverageChart: false
)
}
}
}
}
post {
success {
updateGitlabCommitStatus name: 'jenkins-build', state: 'success'
}
failure {
updateGitlabCommitStatus name: 'jenkins-build', state: 'failed'
}
}
}
pipeline {
agent {
label 'docker'
}
stages {
stage('Copy server definitions') {
steps {
script {
docker.image('webserver').inside() {
sh 'mkdir -p ./webserver/src'
sh 'cp -r /webserver/src/* ./webserver/src/ '
}
echo 'Replacing webserver includes...'
sh "find src/. -type f -name '*.ts' -print0 | xargs -0 sed -i 's|../../webserver|../webserver|g'"
}
}
}
stage('Integration Tests') {
steps {
script {
docker.image('postgres:alpine').withRun('-e "POSTGRES_PASSWORD=password" -e "POSTGRES_DB=test"') {c ->
docker.image('node:alpine').inside("--link ${c.id}:db") {
sh 'hostname'
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment