Skip to content

Instantly share code, notes, and snippets.

@pravintamil
Forked from superjose/.gitlab-ci.yml
Created April 5, 2019 12:27
Show Gist options
  • Save pravintamil/86990d85ce8c3ae7c74d0ede40a64b87 to your computer and use it in GitHub Desktop.
Save pravintamil/86990d85ce8c3ae7c74d0ede40a64b87 to your computer and use it in GitHub Desktop.
This is an example of a .gitlab-ci.yml that is required for Continuous Integration on GitLab projects.
# Reference: https://www.exclamationlabs.com/blog/continuous-deployment-to-npm-using-gitlab-ci/
# GitLab uses docker in the background, so we need to specify the
# image versions. This is useful because we're freely to use
# multiple node versions to work with it. They come from the docker
# repo.
# Uses NodeJS V 9.4.0
image: node:9.4.0
# And to cache them as well.
cache:
paths:
- node_modules/
- .yarn
# We tell GitLab to install all the packages
# before running anything.
# Docker images come with yarn preinstalled
before_script:
- apt-get update -qq && apt-get install
# You specify the stages. Those are the steps that GitLab will go through
# Order matters.
stages:
- build
- test
- staging
- openMr
- production
Build:
stage: build
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install
script:
- npm run build
Test:
stage: test
tags:
- node
before_script:
- yarn config set cache-folder .yarn
- yarn install
script:
# Installs Chrome
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
- apt-get update
- apt-get install google-chrome-stable -y
# Runs the tests.
- npm run test:karma-headless
Deploy to Staging:
stage: staging
tags:
- node
before_script:
# Generates to connect to the AWS unit the SSH key.
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
# Sets the permission to 600 to prevent a problem with AWS
# that it's too unprotected.
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- bash ./gitlab-deploy/.gitlab-deploy.staging.sh
environment:
name: staging
# Exposes a button that when clicked take you to the defined URL:
url: http://ec2-13-59-173-91.us-east-2.compute.amazonaws.com:3001
# Remember to have the PRIVATE_TOKEN generated. This is only needed to be done once per project and not per user.
# Once you add it (Needs Master priviliges) as a Secret Variable, it should work.
Open Merge Request:
# Got it from here: https://gitlab.com/tmaier/gitlab-auto-merge-request/blob/develop/.gitlab-ci.yml
image: tmaier/gitlab-auto-merge-request
stage: openMr
tags:
- node
script:
- bash ./gitlab-deploy/auto-merge-request.sh # The name of the script
Deploy to Production:
stage: production
tags:
- node
before_script:
# Generates to connect to the AWS unit the SSH key.
- mkdir -p ~/.ssh
- echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
# Sets the permission to 600 to prevent a problem with AWS
# that it's too unprotected.
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- bash ./gitlab-deploy/.gitlab-deploy.prod.sh
environment:
name: production
# Exposes a button that when clicked take you to the defined URL:
url: http://ec2-13-59-173-91.us-east-2.compute.amazonaws.com:81
when: manual
# !/bin/bash
# Get servers list:
set - f
# Variables from GitLab server:
# Note: They can't have spaces!!
string=$DEPLOY_SERVER
array=(${string//,/ })
# Iterate servers for deploy and pull last commit
# Careful with the ; https://stackoverflow.com/a/20666248/1057052
for i in "${!array[@]}"; do
echo "Deploy project on server ${array[i]}"
ssh ubuntu@${array[i]} "cd ./Pardo/vr && git stash && git checkout $CI_BUILD_REF_NAME && git stash && git pull origin master && sudo yarn install && sudo npm run production"
done
# !/bin/bash
# Get servers list:
set - f
# Variables from GitLab server:
# Note: They can't have spaces!!
string=$DEPLOY_SERVER
array=(${string//,/ })
# Iterate servers for deploy and pull last commit
# Careful with the ; https://stackoverflow.com/a/20666248/1057052
for i in "${!array[@]}"; do
echo "Deploy project on server ${array[i]}"
ssh ubuntu@${array[i]} "cd ./Staging/vr && git stash && git checkout $CI_BUILD_REF_NAME && git stash && git pull && sudo yarn install && sudo npm run staging"
done
#!/usr/bin/env bash
set -e
# Gotten from:
# https://about.gitlab.com/2017/09/05/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/
# This shall automatically create a merge request right after the build has been pushed.
# Added some touches from: https://gitlab.com/tmaier/gitlab-auto-merge-request/blob/develop/merge-request.sh
if [ -z "$PRIVATE_TOKEN" ]; then
echo "PRIVATE_TOKEN not set"
echo "Please set the GitLab Private Token as PRIVATE_TOKEN"
exit 1
fi
# Extract the host where the server is running, and add the URL to the APIs
[[ $CI_PROJECT_URL =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"
# Look which is the default branch
TARGET_BRANCH=`curl --silent "${HOST}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" | jq --raw-output '.default_branch'`;
# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"remove_source_branch\": true,
\"title\": \"WIP: ${CI_COMMIT_REF_NAME}\",
\"assignee_id\":\"${GITLAB_USER_ID}\"
}";
# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
--header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
--header "Content-Type: application/json" \
--data "${BODY}";
echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME} and assigned to you";
exit;
fi
echo "No new merge request opened";
{
"name": "pardo",
"version": "0.5.0",
"description": "VR Portion of the application",
"main": "build/server/server.js",
"scripts": {
"staging": "npm-run-all build start:staging",
"production": "npm-run-all build start:prod",
"dev": "node_modules/.bin/npm-run-all --parallel gulp webpack:dev",
"start": "sudo NODE_ENV=prod node ./build/server/server.js",
"build": "node_modules/.bin/npm-run-all --parallel webpack:prod gulp:build",
"lint:js": "",
"lint.ts": "",
"start:dev": "set NODE_ENV=dev && node ./build/server/server.js",
"start:staging": " NODE_ENV='staging' forever restart ./build/server/server.js || NODE_ENV='staging' forever start ./build/server/server.js",
"start:prod": " NODE_ENV='prod' forever restart ./build/server/server.js || NODE_ENV='prod' forever start ./build/server/server.js",
"start:staging-win": "set NODE_ENV=staging && forever restart ./build/server/server.js || forever start ./build/server/server.js",
"start:prod-win-ps": "set NODE_ENV=prod && forever restart ./build/server/server.js || set NODE_ENV=prod forever start ./build/server/server.js",
"stop": "forever stop ./build/server/server.js",
"webpack:dev": "webpack --config config/webpack.dev.js",
"webpack:prod": "webpack --config config/webpack.prod.js -p",
"gulp": "gulp",
"gulp:build": "gulp ts",
"gulp:nodemon": "gulp nodemon",
"test": "node_modules/.bin/npm-run-all --parallel test:watch test:karma",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:karma": "node_modules/.bin/karma start karma.conf.js",
"test:karma-headless": "node_modules/.bin/karma start ./config/karma-headless.conf.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lance13c/Pardo.git"
},
"keywords": [
"pardo"
],
"author": "Pardo Labs",
"license": "ISC",
"bugs": {
"url": "https://github.com/lance13c/Pardo/issues"
},
"jest": {
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"setupFiles": [
"./test/jestsetup.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
},
"homepage": "https://github.com/lance13c/Pardo#readme",
"dependencies": {
"@types/jasmine": "^2.8.3",
"aframe": "^0.7.1",
"aframe-click-drag-component": "^3.0.1",
"aframe-react": "^4.3.0",
"awesome-typescript-loader": "^3.4.0",
"font-awesome": "^4.7.0",
"forever": "^0.15.3",
"jasmine-core": "^2.8.0",
"nunjucks-html-loader": "^1.1.0",
"react": "^16.1.1",
"react-dom": "^16.1.1",
"webpack": "^3.8.1",
"webpack-cdn-plugin": "^1.7.1",
"webpack-merge": "^4.1.1"
},
"devDependencies": {
"@types/express": "^4.0.35",
"@types/karma": "^1.7.3",
"@types/node": "^8.0.53",
"@types/react": "^16.0.25",
"@types/react-dom": "^16.0.3",
"@types/three": "^0.84.30",
"babel-cli": "^6.26.0",
"babel-core": "^6.24.1",
"babel-eslint": "^8.2.1",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.1",
"body-parser": "^1.18.2",
"clean-webpack-plugin": "^0.1.16",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.0",
"eslint": "^4.9.0",
"eslint-config-airbnb": "16.1.0",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-jsx-a11y": "6.0.2",
"eslint-plugin-mocha": "^4.11.0",
"eslint-plugin-react": "7.4.0",
"express": "^4.16.2",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"fs": "0.0.1-security",
"gulp": "^3.9.1",
"gulp-babel": "^7.0.0",
"gulp-nodemon": "^2.2.1",
"gulp-typescript": "^3.2.3",
"gulp-watch": "^4.3.11",
"html-loader": "^0.5.1",
"html-webpack-plugin": "^2.30.1",
"image-webpack-loader": "^3.4.2",
"jasmine": "^2.8.0",
"jest": "^22.0.4",
"jest-cli": "^22.0.4",
"jest-environment-node-debug": "^2.0.0",
"karma": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-edge-launcher": "^0.4.2",
"karma-firefox-launcher": "^1.1.0",
"karma-jasmine": "^1.1.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "^0.0.32",
"karma-webpack": "^2.0.9",
"npm-run-all": "^4.1.2",
"nunjucks": "^3.0.1",
"nunjucks-loader": "^2.4.8",
"path": "^0.12.7",
"pngquant": "^1.3.0",
"react-test-renderer": "^16.2.0",
"style-loader": "^0.19.0",
"superagent": "^3.8.1",
"ts-jest": "^21.2.3",
"tslint": "^5.8.0",
"tslint-config-airbnb-base": "^0.1.0",
"typescript": "^2.6.1",
"webpack-dev-server": "^2.9.4",
"webpack-node-externals": "^1.6.0",
"yarn": "^1.3.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment