Created
April 15, 2020 18:16
-
-
Save dylangolow/880295fc6863c77398c68ea90421b200 to your computer and use it in GitHub Desktop.
GitLab CD Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Node docker image on which this would be run | |
image: node:latest | |
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 | |
# Add lint job later | |
#lint: | |
# stage: test | |
# script: | |
# - npm run lint | |
# test job | |
#test: | |
# stage: test | |
# script: | |
# - npm run test | |
# ToDo: Add coverage | |
# ToDo: Add lint job | |
deployToAWS: | |
before_script: | |
#This command is run before actual stages start running | |
- eval $(ssh-agent -s) | |
- echo -n "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null | |
- mkdir -p ~/.ssh | |
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' | |
environment: | |
name: Staging | |
url: http://api.yoursite.com:5000 | |
only: | |
# - development | |
- demo-app | |
stage: deploy | |
script: | |
# - bash deploy/deploy.sh | |
- npm i -g pm2 | |
- pm2 deploy ecosystem.config.js production setup | |
- pm2 deploy ecosystem.config.js production |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# alternative deployment without pm2 | |
# any future command that fails will exit the script | |
set -e | |
##echo "got here" | |
# Lets write the public key of our aws instance | |
eval $(ssh-agent -s) | |
##echo "in between" | |
#ssh-add <(echo "$PRIVATE_KEY") | |
echo "$PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null | |
##echo "got here2" | |
# ** 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. | |
chmod 700 -R ./deploy | |
./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 -v -i ~/.ssh/test ec2-user@${server} 'bash -s' < ./deploy/updateAndRestart.sh | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
##!/bin/bash | |
# | |
## any future command that fails will exit the script | |
#set -e | |
# | |
## Delete the old repo | |
rm -rf /home/ec2-user/api | |
# | |
## clone the repo again | |
git clone git@gitlab.com:REPO -b development --single-branch /home/ec2-user/api | |
# | |
cd /home/ec2-user/api | |
echo "Running npm install" | |
npm install | |
echo "in update" | |
# Deploy | |
echo "Setup target server directories" | |
npm install pm2@latest -g | |
pm2 deploy ecosystem.config.js production setup 2>&1 || true | |
echo "make deploy" | |
pm2 deploy ecosystem.config.js production update | |
##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/ec2-user/.nvm/nvm.sh | |
# | |
## stop the previous pm2 | |
#pm2 kill && npm remove pm2 -g | |
# | |
##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/ec2-user/api | |
# | |
##install npm packages | |
#echo "Running npm install" | |
#npm install | |
#echo "in update" | |
# | |
##Restart the node server | |
#npm start | |
##pm2 start ./dist/app.js --watch --ignore-watch="node_modules" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment