---
# System prepended metadata

title: gitlab CI/CD

---

# gitlab CI/CD

### gitlab.com 設定
```
gitlab > settings > CI/CD > Variables 
Add Variables 

KEY DEPLOY_SERVER 
VALUE 要部署運作遊戲的伺服器IP

KEY SSH_PRIVATE_KEY 
VALUE 要部署運作遊戲的伺服器上產生的私鑰
```

### gitlab-ci.yml
```
image: node:10.15.3
before_script:
    #  ssh-agent
    - 'which ssh-agent || ( apk update && apk add openssh-client)'
    # 背景執行
    - eval $(ssh-agent -s)
    # 將key 寫入 deploy.key 文件
    - echo "$SSH_PRIVATE_KEY > deploy.key"
    # 配置较低權限
    - chmod 600 deploy.key
    # 讓 agent 使用 key
    - ssh-add deploy.
    - mkdir -p ~/.ssh    
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
stages:
  - build
  - test
  - stg-deploy
cache:
  paths:
    - node_modules/

install_dependencies:
  stage: build
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

execute_test:
  stage: test
  script:
    - npm i -g mocha
    - npm i -g expect
    - npm test

excute_deploy:
  stage: stg-deploy
  script:  
    # 連線到server 刪掉舊檔 git pull 新檔，pm2 重啟服務
    - ssh cash@$DEPLOY_SERVER "rm -rf mini-game && git colone git@gitlab.com:arionlabs/gameserver/mini-game.git && pm2 restart all"
```





