Skip to content

Instantly share code, notes, and snippets.

@caciobanita
Forked from superjose/.gitlab-ci.yml
Created March 31, 2022 13:12
Show Gist options
  • Select an option

  • Save caciobanita/090fbc732992ca077a7a297358160a9e to your computer and use it in GitHub Desktop.

Select an option

Save caciobanita/090fbc732992ca077a7a297358160a9e 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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment