Skip to content

Instantly share code, notes, and snippets.

@bogdanned
Created April 18, 2022 06:32
Show Gist options
  • Save bogdanned/b91883dd1307a8aebacd55f5c172125a to your computer and use it in GitHub Desktop.
Save bogdanned/b91883dd1307a8aebacd55f5c172125a to your computer and use it in GitHub Desktop.
image: node:16
stages: ["build", "test", "staging", "deploy"]
# WARNING
# This pipeline needs the following variables set up to work:
# INSTANCE_IP = // the public IP of the AWS instance
# SECRET_KEY = // the secret key to connect to the AWS instance (.pem) file
# ENV_FILE = // the .env file for production
# the build job
build:
stage: build
script:
- npm install
- npm run lint
- npm run build
artifacts:
paths:
- build
expire_in: 1 week
# the test job
test:
stage: test
variables:
MONGOMS_VERSION: 4.2.8
MONGOMS_DEBUG: 1
MONGOMS_DOWNLOAD_URL: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
script:
- npm install
- npm run test
artifacts:
paths:
- coverage
expire_in: 1 week
.job_template: &deployment
before_script: # prepare the pipeline runner for deploy by installing ssh and node
# install ssh
- "which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )"
- eval $(ssh-agent -s)
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
- apt-get update -y
- apt-get -y install rsync
script:
- chmod 400 $SECRET_KEY
# clean up the ec32 instance
- ssh -i $SECRET_KEY ec2-user@$INSTANCE_IP 'rm -rf /home/ec2-user/api'
- ssh -i $SECRET_KEY ec2-user@$INSTANCE_IP 'mkdir /home/ec2-user/api'
# copy files to the ec2 instance
- scp -i $SECRET_KEY -r build ec2-user@$INSTANCE_IP:/home/ec2-user/api
- scp -i $SECRET_KEY package.json ec2-user@$INSTANCE_IP:/home/ec2-user/api/package.json
- scp -i $SECRET_KEY deploy.sh ec2-user@$INSTANCE_IP:/home/ec2-user/api/deploy.sh
# copy .env to the ec2 instance
- scp -i $SECRET_KEY $ENV_FILE ec2-user@$INSTANCE_IP:/home/ec2-user/api/build/.env
# run the deploy script
- ssh -i $SECRET_KEY ec2-user@$INSTANCE_IP 'cd /home/ec2-user/api && bash deploy.sh'
# run if any branch other than main is changed
staging_deploy:
stage: staging
environment:
name: staging
except:
- main
<<: *deployment
# run if main branch is changed
production_deploy:
stage: deploy
environment:
name: production
only:
- main
<<: *deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment