diff --git a/300moves-jenkinsfile b/300moves-jenkinsfile new file mode 100644 index 0000000..b1cd8c1 --- /dev/null +++ b/300moves-jenkinsfile @@ -0,0 +1,92 @@ +pipeline { + agent any + environment { + ENV = credentials("300Moves_${env.BRANCH_NAME}_env") + Server_ip = credentials("300Moves_${env.BRANCH_NAME}_server_ip") + CREDENTIALS = credentials("gitlab-token") + } + stages{ + stage("cleanup"){ + steps { + deleteDir() + } + } + stage ('Git clone'){ + steps{ + checkout scm + } + + } + stage('SonarQube Analysis') { + when { + branch 'dev' + } + steps { + script { + scannerHome = tool 'Zapbuildsonarqube' + } + withSonarQubeEnv('sonar-global') { + sh "${scannerHome}/bin/sonar-scanner \ + -D sonar.token=${SONAR_TOKEN} \ + -D sonar.projectKey=300moves-dev \ + -D sonar.sources=. \ + -D sonar.host.url=http://sonarqube.zapbuild.in:9000/" + } + } + post { + always { + script { + def sonarqubeUrl = env.getProperty('sonarqube_url_id') + + emailext subject: '[${JOB_NAME}] - Sonarqube Scanning', + body: "Sonarqube scanning has been completed successfully. Click [here](${sonarqubeUrl}=300moves-dev) to check status", + to: 'lalitmohan@zapbuild.com', + replyTo: 'pc@zapbuild.com', + from: "sonarqube@zapbuild.com" + } + } + } + } + + + + stage ("Build"){ + when { + not { + branch 'master' + } + } + steps { + sh 'echo "300Moves_${BRANCH_NAME}_env"' + sh 'cp $ENV .' + sh 'zip -r website.zip .' + } + } + stage ("Deploy on DevServer"){ + steps{ + script{ + if (env.BRANCH_NAME == 'dev'){ + sh 'echo "dev server"' + sh 'scp website.zip deploy.sh jenkins@$Server_ip:/var/www/html/jenkindeploy/300moves/${BRANCH_NAME}' + sh 'ssh jenkins@$Server_ip "cd /var/www/html/jenkindeploy/300moves/${BRANCH_NAME} && chmod 755 deploy.sh && sh deploy.sh"' + } else if (env.BRANCH_NAME == 'qa') { + // Deploy to qa server + sh 'echo "qa deployment"' + sh 'scp website.zip deploy.sh jenkins@$Server_ip:/var/www/html/jenkindeploy/300moves/${BRANCH_NAME}' + sh 'ssh jenkins@$Server_ip "cd /var/www/html/jenkindeploy/300moves/${BRANCH_NAME} && chmod 755 deploy.sh && sh deploy.sh"' + } else if (env.BRANCH_NAME == 'master') { + // Deploy to stage server + sh 'echo "Live deploymebnt"' + sh 'ssh ubuntu@$Server_ip "cd /var/www/html/300moves/public_html/ && git pull https://${CREDENTIALS_USR}:${CREDENTIALS_PSW}@gitlab.zapbuild.com/zapbuild/cwvr.git master"' + sh 'scp ${ENV} ubuntu@$Server_ip:/var/www/html/300moves/public_html/' + sh 'ssh ubuntu@$Server_ip "cd /var/www/html/300moves/public_html/ && docker-compose up --build -d"' + } + } + //sh 'echo "Deployment"' + //sh 'scp website.zip deploy.sh jenkins@$Server_ip:/var/www/html/jenkindeploy/300moves/${BRANCH_NAME}' + //sh 'ssh jenkins@$Server_ip "cd /var/www/html/jenkindeploy/300moves/${BRANCH_NAME} && chmod 755 deploy.sh && sh deploy.sh"' + } + } + } + +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8bbc4e5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Node.js runtime as the base image +FROM node:14 + +# Set the working directory in the container +WORKDIR /usr/src/app + +# Copy package.json and package-lock.json (if available) to the working directory +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the rest of the application code to the working directory +COPY . . + +# Expose the port the app runs on +EXPOSE 3000 + +# Command to run the application +CMD ["node", "server.js"] diff --git a/Jenkinsfile b/Jenkinsfile index 84530c2..2909977 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,28 +1,116 @@ +def VAR = null pipeline { agent any + environment{ + DEV_ENV = 'devserver_ip' + // VAR = null + } + // options { + // skipDefaultCheckout(true) + // } stages { + stage('Set Environment') { + steps { + script { + def branchName = env.BRANCH_NAME + sh "echo '${env.BRANCH_NAME}'" + if (branchName == 'dev') { + sh "echo 'DEV==>'" + VAR = 'development' + echo "var======> ${VAR}" + //ENV = credentials('DEV_ENV') + } else if (branchName == 'qa') { + sh "echo 'QA==>'" + VAR = 'quality analyst' + //ENV = credentials('QA_ENV') + echo "var======> ${VAR}" + } else { + error "Branch not supported" + } + } + } + } stage('Install Dependencies') { steps { - sh 'npm install' + sh 'echo "Install Dependencies ${VAR} ==>> ${DEV_ENV} ${GIT_URL}"' + script{ + echo "Current branch: ${VAR} Dunkins_${env.BRANCH_NAME}_env" + echo "Git repo: ${env.GIT_URL}" + } } } + stage('Checkout') { + steps { + // Automatically checks out the source code for the branch that triggered the build + checkout scm + } + } + + // stage('Git Clone') { + // steps { + // // Git clone step + // script{ + // // git branch: "${env.BRANCH_NAME}", credentialsId: 'git-hub', URL: "${env.GIT_URL}" + // git branch: "${env.BRANCH_NAME}", credentialsId: 'git-hub', URL: 'https://github.com/ashirwad8858/test-nodejs-app-jenkins-pipline.git' + + // } + // } + // } - stage('Test') { + stage('Test DEV') { + when { + branch 'dev' + } + steps { + sh 'echo "testing application..."' + } + } + stage('Test QA') { + when { + branch 'qa' + } steps { sh 'echo "testing application..."' } } + stage('Docker Build') { + steps { + // Docker build step + script { + app = docker.build("myapp:1") + } + } + } stage("Deploy application") { - steps { - sh 'echo "deploying application..."' - } + steps { + // Deploy step + script { + if (env.BRANCH_NAME == 'dev') { + // Deploy to dev server + sh 'echo "dev deploymebnt"' + } else if (env.BRANCH_NAME == 'qa') { + // Deploy to qa server + sh 'echo "qa deploymebnt"' + } else if (env.BRANCH_NAME == 'stage') { + // Deploy to stage server + sh 'echo "stage deploymebnt"' + } + } + } } } + + + + + + + } diff --git a/README.md b/README.md index c25a886..805cec1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Build and Deploy nodejs application on EC2 instance - Freestyle +# QA Build and Deploy nodejs application on EC2 instance - Freestyle # Pre-requisites @@ -28,7 +28,6 @@ Github URL: https://github.com/ravdy/nodejs.git Using simple "hello world" application from the [nodejs.org](https://nodejs.org/en/docs/guides/getting-started-guide/) website - ## On Jenkins GUI 1. Create the new FreeStyle Project diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/deploy.sh @@ -0,0 +1 @@ + diff --git a/dunkins-jenkinsfile b/dunkins-jenkinsfile new file mode 100644 index 0000000..17a3ef1 --- /dev/null +++ b/dunkins-jenkinsfile @@ -0,0 +1,67 @@ +pipeline { + agent any + environment { + ENV = credentials("300Moves_${env.BRANCH_NAME}_env") + Server_ip = credentials("300Moves_${env.BRANCH_NAME}_server_ip") + } + stages{ + stage("cleanup"){ + steps { + deleteDir() + } + } + stage ('Git clone'){ + steps{ + checkout scm + } + + } + stage('SonarQube Analysis') { + when { + branch 'dev' + } + steps { + script { + scannerHome = tool 'Zapbuildsonarqube' + } + withSonarQubeEnv('sonar-global') { + sh "${scannerHome}/bin/sonar-scanner \ + -D sonar.token=${SONAR_TOKEN} \ + -D sonar.projectKey=300moves-dev \ + -D sonar.sources=. \ + -D sonar.host.url=http://sonarqube.zapbuild.in:9000/" + } + } + post { + always { + script { + def sonarqubeUrl = env.getProperty('sonarqube_url_id') + + emailext subject: '[${JOB_NAME}] - Sonarqube Scanning', + body: "Sonarqube scanning has been completed successfully. Click [here](${sonarqubeUrl}=300moves-dev) to check status", + to: 'lalitmohan@zapbuild.com', + replyTo: 'pc@zapbuild.com', + from: "sonarqube@zapbuild.com" + } + } + } + } + + + + stage ("Build"){ + steps { + sh 'cp $ENV .' + sh 'zip -r website.zip .' + } + } + stage ("Deploy on DevServer"){ + steps{ + sh 'echo "Deployment"' + // sh 'scp website.zip deploy.sh jenkins@$Server_ip:/var/www/jenkindeploy/300moves/$ENV' + // sh 'ssh jenkins@$Server_ip "cd /var/www/jenkindeploy/300moves/$ENV && chmod 755 deploy.sh && sh deploy.sh"' + } + } + } + +}