diff --git a/Jenkins/Jenkinsfile b/Jenkins/Jenkinsfile new file mode 100644 index 0000000..d701b31 --- /dev/null +++ b/Jenkins/Jenkinsfile @@ -0,0 +1,19 @@ +node { + def commit_id + stage('Preparation') { + checkout scm + sh "git rev-parse --short HEAD > .git/commit-id" + commit_id = readFile('.git/commit-id').trim() + } + stage('test') { + def myTestContainer = docker.image('node:16') + myTestContainer.pull() + myTestContainer.inside { + sh 'npm install' + } + } + stage('Final'){ + echo 'Pipeline complete' + } + +} diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..64a8314 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,45 @@ +pipeline{ + agent any + + environment { + BRANCH_NAME = "${GIT_BRANCH}" + } + + stages { + stage('Install') { + tools { + nodejs 'node16' + } + steps { + sh 'npm install' + } + } + stage('Sonar'){ + when { + expression { + BRANCH_NAME == 'main' + } + } + tools { + nodejs 'node16' + } + steps{ + script{ + echo 'running Sonar stage ...' + } + script { + sonarqubeScannerHome = tool 'sonar' + } + withSonarQubeEnv('sonar') { + sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dsonar.host.url=http://sonarqube:9000 -Dsonar.qualitygate.wait=true" + } + } + } + stage('Deploy') { + steps { + echo 'Deploying....' + } + } + + } +} diff --git a/index.js b/index.js index 880e2a0..c521005 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +//entry point for the app const express = require("express"); require("dotenv").config(); const app = express(); diff --git a/jenkins/sonar.sh b/jenkins/sonar.sh new file mode 100644 index 0000000..50d577d --- /dev/null +++ b/jenkins/sonar.sh @@ -0,0 +1,8 @@ +export SONAR_TOKEN=sqp_7c6df26ff26031fa62f4c57849b23896bb7291cc +export SONAR_SCANNER_VERSION=4.7.0.2747 +export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-linux +curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip +unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/ +export PATH=$SONAR_SCANNER_HOME/bin:$PATH +export SONAR_SCANNER_OPTS="-server" +echo "sonar variables been set" diff --git a/package.json b/package.json index ccd8219..193789f 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "start": "node index.js", + "sonar": "node sonar-project.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", @@ -13,5 +14,8 @@ "dotenv": "^14.1.0", "ejs": "^3.1.6", "express": "^4.17.2" + }, + "devDependencies": { + "sonarqube-scanner": "^3.0.1" } } diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..663039d --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,7 @@ +sonar.projectKey=nodejs +sonar.projectName=nodejsSample + +# we keep tests in magic directories within the main source tree +sonar.exclusions=*.properties +# restrict allowed global variables to only those we use +sonar.javascript.environments=node