Commit ee26aa89 authored by Abhinav Dhasmana's avatar Abhinav Dhasmana
Browse files

sample boiler plate for CI/CD demo

parents
module.exports = {
"extends": "airbnb-base",
"rules": {
"no-console": 0
}
};
\ No newline at end of file
node_modules
\ No newline at end of file
image: node:8.9.0
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- npm install
stages:
- test
- deploy
# lint and test are two different jobs in the same stage.
# This allows us to run these two in parallel and making build faster
lint:
stage: test
script:
- npm run lint
test:
stage: test
script:
- npm run test
deploy:dev:
stage: deploy
script:
- bash deploy/deploy.sh
only:
- master
# Sample boiler plate for CI/CD demo
\ No newline at end of file
#!/bin/bash
# any future command that fails will exit the script
set -e
# Lets write the public key of our aws instance
eval $(ssh-agent -s)
echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
# ** Alternative approach
# echo -e "$PRIVATE_KEY" > /root/.ssh/id_rsa
# chmod 600 /root/.ssh/id_rsa
# ** End of alternative approach
# disable the host key checking.
./deploy/disableHostKeyChecking.sh
# we have already setup the DEPLOYER_SERVER in our gitlab settings which is a
# comma seperated values of ip addresses.
DEPLOY_SERVERS=$DEPLOY_SERVERS
# lets split this string and convert this into array
# In UNIX, we can use this commond to do this
# ${string//substring/replacement}
# our substring is "," and we replace it with nothing.
ALL_SERVERS=(${DEPLOY_SERVERS//,/ })
echo "ALL_SERVERS ${ALL_SERVERS}"
# Lets iterate over this array and ssh into each EC2 instance
# Once inside.
# 1. Stop the server
# 2. Take a pull
# 3. Start the server
for server in "${ALL_SERVERS[@]}"
do
echo "deploying to ${server}"
ssh ubuntu@${server} 'bash -s' < ./deploy/updateAndRestart.sh
done
# This the the prompt we get whenever we ssh into the box and get the message like this
#
# The authenticity of the host 'ip address' cannot be verified....
#
# Below script will disable that prompt
# note ">>". It creates a file if it does not exits.
# The file content we want is below
#
# Host *
# StrictHostKeyChecking no
#
# any future command that fails will exit the script
set -e
mkdir -p ~/.ssh
touch ~/.ssh/config
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" >> ~/.ssh/config
\ No newline at end of file
#!/bin/bash
# any future command that fails will exit the script
set -e
# Delete the old repo
rm -rf /home/ubuntu/hapiBoilerPlate/
# clone the repo again
git clone git@gitlab.com:abhinavdhasmana/hapiBoilerPlate.git
#source the nvm file. In an non
#If you are not using nvm, add the actual path like
# PATH=/home/ubuntu/node/bin:$PATH
source /home/ubuntu/.nvm/nvm.sh
#pm2 needs to be installed globally as we would be deleting the repo folder.
# this needs to be done only once as a setup script.
npm install pm2 -g
# starting pm2 daemon
pm2 status
cd /home/ubuntu/hapiBoilerPlate
#install npm packages
echo "Running npm install"
npm install
#Restart the node server
npm run restart
\ No newline at end of file
This diff is collapsed.
{
"name": "hapiboilerplate",
"version": "1.0.0",
"description": "This is a demo hapi 17 server",
"main": "index.js",
"scripts": {
"start": "pm2 start src/server.js -i 0",
"stop": "pm2 stop all",
"restart": "pm2 restart src/server.js",
"test": "NODE_ENV=test && lab -v -c",
"lint": "eslint . --ignore-path .gitignore"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/abhinavdhasmana/hapiBoilerPlate.git"
},
"keywords": [
"hapi"
],
"author": "Abhinav Dhasmana",
"license": "ISC",
"bugs": {
"url": "https://gitlab.com/abhinavdhasmana/hapiBoilerPlate/issues"
},
"homepage": "https://gitlab.com/abhinavdhasmana/hapiBoilerPlate#README",
"dependencies": {
"hapi": "^17.3.1",
"pm2": "^2.10.2"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.11.0",
"lab": "^15.4.1"
}
}
const ping = require('./ping');
module.exports = [].concat(ping);
module.exports = {
method: 'GET',
path: '/ping',
handler: () => 'pong',
};
const Hapi = require('hapi');
const routes = require('./routes/');
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000,
});
const provision = async () => {
server.route(routes);
};
const startForTest = async () => {
await provision();
};
// Start the server
const start = async () => {
try {
await provision();
await server.start();
} catch (err) {
console.log(err);
process.exit(1);
}
console.log('Server running at:', server.info.uri);
};
if (!module.parent) {
start();
} else {
startForTest();
}
module.exports = server;
const Chai = require('chai');
const Lab = require('lab');
exports.lab = Lab.script();
const { lab } = exports;
const Server = require('../../src/server');
const { expect } = Chai;
lab.experiment('Test for /ping route', () => {
lab.test('should return pong', async () => {
const options = {
method: 'GET',
url: '/ping',
};
const response = await Server.inject(options);
expect(response.statusCode).to.eqls(200);
expect(response.result).to.eqls('pong');
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment