Gitlab CI / CD

Integrate Onboardbase into your Gitlab CI / CD

In this guide, you will learn how to use Onboardbase to manage a project environment in a Gitlab CI config file.

📘

Requirements

  • A Service Token, which can be generated from your Onboardbase account
  • Save the service token in your Gitlab CI environment config with the name ONBOARDBASE_TOKEN
  • Some CI servers may have an error stating ENOENT ps, add sudo apt-get -y install procps to your CI script to resolve.

📘

TIP

You can authenticate the CLI with Environment variables.
Supported environment variables are:

  • ONBOARDBASE_TOKEN - A service token
  • ONBOARDBASE_PROJECT - an onboardbase project name
  • ONBOARDBASE_ENVIRONMENT - an environment in the specified project

Sample .gitlab-ci.yml integration file

The Gitlab CI configuration file below uses the before script to install Onboardbase CLI and authenticates it with a service token.

Also, it is considering deploying to a remote server; the ssh key for the server is exported to a file in /tmp/private.key and used ssh-agent to add the key.

Finally, the deploy script uses the exposed $USER and $SERVER variables from Onboardbase to deploy to the server using the gitlab-env project and staging environment:

onboardbase build -c \
  'echo "$SSH_KEY" > /tmp/private.key && chmod 400 /tmp/private.key' \
  -p "gitlab-env" -e "staging"

Below is the complete configuration file.

image: node:14

stages:
  - build
  - lint
  - test
  - deploy

cache:
  paths:
    - node_modules/

before_script:
  - which wget || ( apt-get update -y && apt-get install wget -y )
  - wget https://onboardbase-cli.fra1.digitaloceanspaces.com/apt/onboardbase.deb
  - chmod +x onboardbase.deb
  - dpkg -i ./onboardbase.deb
  - onboardbase -v
  - onboardbase config:set-token $ONBOARDBASE_TOKEN --scope /

build_and_lint_project:
  stage: build
  script:
    - yarn install && yarn lint

test_project:
  stage: test
  services:
    - postgres:latest
  script:
    - onboardbase run -c "env && yarn test" -p "gitlab-env" -e "development"

deploy_to_dev:
  stage: deploy
  script:
    - 'which rsync || ( apt-get update -y && apt-get install rsync -y )'
    # install ssh-agent
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

    - eval $(ssh-agent -s)
    # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
    - onboardbase run -c 'echo "$SSH_KEY" > /tmp/private.key && chmod 400 /tmp/private.key' -p "gitlab-env" -e "staging"
    - ssh-add /tmp/private.key

    # WARNING: use only in a docker container, if you use it with shell you will overwrite your user's ssh config
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - onboardbase build -c 'env && ssh "$USER"@"$BE_SERVER" "cd /var/www/project/app/ && git fetch && git checkout dev && git pull origin dev &&  yarn && yarn migrate && pm2 reload dev_server"' -p "gitlab-env" -e "staging"
  only:
    - dev

deploy_to_production:
  stage: deploy
  script:
    - 'which rsync || ( apt-get update -y && apt-get install rsync -y )'
    # install ssh-agent
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

    - eval $(ssh-agent -s)
    # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
    - onboardbase run -c 'echo "$SSH_KEY" > /tmp/private.key && chmod 400 /tmp/private.key' -p "gitlab-env" -e "development"
    - ssh-add /tmp/private.key

    # disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
    # WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
    - mkdir -p ~/.ssh
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - onboardbase build -c 'env && ssh "$USER"@"$BE_SERVER" "cd /var/www/project/app/ && git fetch && git checkout dev && git pull origin master &&  yarn && yarn migrate && pm2 reload prod_server"' -p "gitlab-env" -e "production"

  only:
    - master