From 40d307c2164bfbc085b97004d398384b50007b10 Mon Sep 17 00:00:00 2001 From: Raviraj Subramanian Date: Tue, 29 Mar 2022 22:12:21 +0530 Subject: [PATCH] #34 Added jenkins pipeline --- Jenkinsfile | 142 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 8 +++ 2 files changed, 150 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..5698971 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,142 @@ +#!groovy + +def runNPM(command) { + def NODE_VERSION = 14; + utilObj = new Utils(); + envVersion = utilObj.getEnvVersion(NODE_VERSION); + utilObj.runCmd(command, envVersion) +} + +def uploadAndInvalidate(environment) { + def STATIC_ASSETS = [ + release: [ + bucketName: credentials('DOCS_S3_BUCKET_NAME_PROD'), + cdnDistributionId: credentials('DOCS_S3_BUCKET_NAME'), + profile: 'prod' + ], + staging: [ + bucketName: credentials('DOCS_S3_BUCKET_NAME_STAGING'), + profile: 'staging' + ] + ] + + uploadAssetsToS3('docs/dist', "s3://${STATIC_ASSETS[environment].bucketName}/api-sdk", 'us-east-1', true, false, 86400, STATIC_ASSETS[environment].profile) + if (environment == 'release') { + invalidateCDN(STATIC_ASSETS[environment].cdnDistributionId, '\"/*\"', STATIC_ASSETS[environment].profile) + } +} + +def publishToNpm(environment){ + if (environment == 'release') { + runNPM('npm run dopublish'); + } else { + runNPM('npm run dopublish-dry-run'); + } +} + +pipeline { + + agent any + + environment { + NPM_TOKEN = credentials('NPM_TOKEN') + } + + parameters { + choice(choices: 'None\nstaging\nrelease\ndeploydocs', description: 'Deploys docs to S3 bucket', name: 'deployTo') + } + + stages { + + stage('Checkout & Setup') { + steps { + checkout scm + runNPM('npm ci') + } + } + + stage('Run unit tests') { + steps { + runNPM('npm test') + } + } + + stage ('Remove target folder') { + steps { + sh 'rm -rf dist' + sh 'rm -rf docs/dist' + } + } + + stage ('Build SDK') { + steps { + runNPM('npm run build:lib') + } + } + + stage ('Build Docs') { + steps { + runNPM('npm run build:docs') + } + } + + stage ('NPM publish dry-run') { + when { + expression { + params.deployTo == 'staging' || params.deployTo == 'release' + } + } + steps { + publishToNpm('staging') + } + } + + stage ('Deploy docs to Staging') { + when { + expression { + params.deployTo == 'staging' && BRANCH_NAME == 'main' + } + } + steps { + uploadAndInvalidate('staging') + } + } + + stage ('Deploy to Release') { + when { + expression { + params.deployTo == 'release' && BRANCH_NAME == 'main' + } + } + steps { + uploadAndInvalidate('release') + publishToNpm('release') + } + } + + stage ('Deploy docs only') { + when { + expression { + params.deployTo == 'deploydocs' && BRANCH_NAME == 'main' + } + } + steps { + uploadAndInvalidate('release') + } + } + + } + + post { + always { + echo 'Freshworks API SDK job finished' + deleteDir() + } + success { + echo 'Freshworks API SDK job successful' + } + failure { + echo 'Freshworks API SDK job failed' + } + } +} diff --git a/package-lock.json b/package-lock.json index b4613b3..d7e2350 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15310,6 +15310,14 @@ } } }, + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + } + }, "string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz",