---
# System prepended metadata

title: GitLab CI/CD
tags: [DevOps, CI/CD, III]

---

GitLab CI/CD
===

###### tags: `III` `DevOps` `CI/CD`

## Terminology
預備環境 Staging
生產 Production

---

## Gitlab Runner
- [gitlab-runner](https://hackmd.io/@derailment/gitlab-runner)
- Prometheus
http://10.22.100.231:31190/
http://10.22.100.233:31190/
![](https://i.imgur.com/P2W8Z9L.png)
- Access outside world via SSH
  * ```ssh-keygen -t rsa```
  * Paste private key (id_rsa) into ``SSH_PRIVATE_KEY`` as GitLab CI variable
  * Paste public key (id_rsa.pub) into ~/.ssh/authorized_keys on machine that will be accessed
  * Add the following snippet to job:
  ```
  before_script:
    - apk add openssh-client
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
  ```

---

## Unit Test
- [JUnit style reports](https://docs.gitlab.com/ee/ci/junit_test_reports.html)
- [CMocka](https://hackmd.io/@derailment/cmocka)
- [Enabling the JUnit test reports feature](https://docs.gitlab.com/ee/ci/junit_test_reports.html#enabling-the-junit-test-reports-feature-core-only)

---

## Code Coverage
- [gcovr](https://hackmd.io/@derailment/code-coverage)
- [覆蓋率出現在 MR 和 Jobs table](https://docs.gitlab.com/ee/ci/pipelines/settings.html#test-coverage-parsing)
- [Publish code coverage report with GitLab Pages](
https://about.gitlab.com/blog/2016/11/03/publish-code-coverage-report-with-gitlab-pages/)

---

## Badges
![](https://i.imgur.com/qtZgdrH.png)
- [Shields.io](https://shields.io/)

---

## GitLab Page
- [How to Enable GitLab Pages for GitLab CE and EE](https://www.youtube.com/watch?v=dD8c7WNcc6s&feature=youtu.be)

---

## Infrastructure Integration
* [K8s](https://hackmd.io/@derailment/k8s)
* [Add existing cluster](http://gitvepc/help/user/project/clusters/add_remove_clusters.md#existing-kubernetes-cluster)
* CoreDNS
  * [Troubleshooting of Installing GitLab Runner On K8s by GitLab GUI](https://codegleaner.github.io/2020/06/01/gitlab-runner-k8s.html)
* [Remotely Access to Cluster](https://hackmd.io/@derailment/k8s-access)
![](https://i.imgur.com/Ft3Y3QU.png)
* [Auto-Scaling](https://hackmd.io/@derailment/k8s-scale)

---

## .gitlab-ci.yml
- [gitlab/lib/gitlab/ci/templates](https://gitlab.com/gitlab-org/gitlab/-/tree/master/lib/gitlab/ci/templates)
- 標示```environment```
```
stages:
  - test
  - build
  - deploy

test:
  stage: test
  script: echo "Running tests"

build:
  stage: build
  script: echo "Building the app"

deploy_review:
  stage: deploy
  script:
    - echo "Deploy a review app"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_ENVIRONMENT_SLUG.example.com
  only:
    - branches
  except:
    - master

deploy_staging:
  stage: deploy
  script:
    - echo "Deploy to staging server"
  environment:
    name: staging
    url: https://staging.example.com
  only:
  - master

deploy_prod:
  stage: deploy
  script:
    - echo "Deploy to production server"
  environment:
    name: production
    url: https://example.com
  when: manual
  only:
  - master
```

---

## Auto DevOps
我們不用 Auto DevOps，不要勾選
![](https://i.imgur.com/XtqqpkI.png)
* The Auto DevOps pipeline will run if no alternative CI configuration file is found.
![](https://i.imgur.com/kmNSksr.png)
- [base domain](http://gitvepc/help/topics/autodevops/index#auto-devops-base-domain)
The Auto DevOps base domain is required if you want to make use of **Auto Review Apps**, **Auto Deploy**, and **Auto Monitoring**
- [Auto Deploy](http://gitvepc/help/topics/autodevops/index#auto-deploy)
- [Quick Start Guide](https://docs.gitlab.com/ee/topics/autodevops/quick_start_guide.html)
- [Customizing Auto DevOps](https://docs.gitlab.com/ee/topics/autodevops/customize.html)
- [GitLab: Auto DevOps 之牛刀小試 3 - Auto Deploy (Production)](https://ithelp.ithome.com.tw/articles/10226257)

---

## SSH Access
產生 ssh key pair
```
ssh-keygen -t rsa
```
貼上 ssh 公鑰到 deploy key
![](https://i.imgur.com/snRjN7d.png)
已有 ssh 私鑰 (~/.ssh/id_rsa) 的機器，```git clone``` 就不用打帳號密碼
```
chmod 600 ~/.ssh/id_rsa
GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@gitvepc:tcnieh/vEPC-deployment.git
```