# Use the docker runner
image: node:6

before_script:
# Install yarn
# ref: https://yarnpkg.com/zh-Hans/docs/install#linux-tab
- curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
- echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
- apt-get update -y
- apt-get install yarn -y
# install ssh-agent
# ref: https://docs.gitlab.com/ee/ci/ssh_keys/README.html
- 'which ssh-agent || ( apt-get install openssh-client -y )'
# run ssh-agent
- eval $(ssh-agent -s)
# add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
# - ssh-add <(echo "$SSH_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
- pwd
- which nodejs
- nodejs -v
- which npm
- npm -v
- which yarn
- yarn --version
- yarn config set cache-folder .yarn

cache:
  key: "$CI_BUILD_REF_NAME"
  paths:
  - node_modules/
  - .yarn/

# jobs sequense pipeline
stages:
- test
- build
- deploy

# test job: lint, test project
test:
  tags:
  - node-6
  script:
  - yarn install
  - npm run lint
  - npm run test
  stage: test

# build job: build project and upload artifacts for deploy
build:
	# It would upload artifacts to gitlab and next stage for deploy
  artifacts:
    name: "${CI_BUILD_REF_NAME}_${CI_BUILD_REF}"
    expire_in: 1 day
    paths:
    - _public/
  tags:
  - node-6
  script:
  - yarn install
  - npm run build
  stage: build

# deploy job
deploy:
  tags:
  - node-6
  script:
  - yarn install
	# You should add deploy script here
  environment:
    name: demo
    url: http://demo.25sprout
  stage: deploy
  only:
  - master
