gitlab CICD

1. 安装gitlab-runner 这里不再介绍  这里选择docker 模式

2. 使用gitlab ci 工具 cicd 代码流程

  代码检查--> docker build   -->   更新harbor 服务器   -->   同步到kubernetes --> 发送邮件通知

 

3.在项目的主目录创建.gitlab-ci.yml 文件

stages:
  - scanner
  - sendmail
  - build
  - update_img
  - sync_img
  - deploy
  - check_pod
  - send_mail
variables:     
  namespace: "test"
  deployment_name: "platform-api"
  project: "harbor.test.com/cicd/platform-api"
  release: "latest"
  harbor: "http://harbor.test.com"
  DOCKER_HARBOR_USER: "admin"
  DOCKER_HARBOR_PASSWD: "Harboreeee12345"
  BASE_IMAGE: "harbor.test.com/lks/cicd/initbase:v1"
  DOCKER_PUSH_IMAGE: "harbor.test.com/lks/cicd/dockerpush:git"
  KUBECTL_TOOL: "harbor.test.com/devel-tool/kubectl_tool:latest"
  mail_list: "test@com"
include:
  - local: '.gitlab/check/SonarqubeCheck.yml'
  - local: '.gitlab/docker/DefaultDockerBuild.yml'
  - local: '.gitlab/docker/PushImage.yml'
  - local: '.gitlab/Kubernetes/SyncImage.yml'
  - local: '.gitlab/Kubernetes/CheckPod.yml'
  - local: '.gitlab/check/SendMail.yml'

 

4. 功能部署使用include 方式加载本地yml文件 。

1)代码检查 .gitlab/check/SonarqubeCheck.yml

image: harbor.test.com/ci/ubuntu-sonarqube-nodejs-scanner:v4       
example-master-sonar:
  stage: scanner
  only:
    - master
    - merge_requests
  tags:
    - sonar-docker
  script:
    - sonar-scanner-4.4.0 -Dsonar.projectName=$CI_PROJECT_NAME -Dsonar.projectKey=$CI_PROJECT_NAME -Dsonar.branch.name=${CI_COMMIT_REF_NAME} -Dsonar.host.url=http://isonar.test.com -Dsonar.sourceEncoding=UTF-8

example-develop-sonar:
  stage: scanner
  only:
    - develop
  tags:
    - sonar-docker
  script:
    - sonar-scanner-4.4.0 -Dsonar.projectName=$CI_PROJECT_NAME -Dsonar.projectKey=$CI_PROJECT_NAME -Dsonar.branch.name=${CI_COMMIT_REF_NAME} -Dsonar.language=nodejs -Dsonar.host.url=http://isonar.test.com

example-feature-sonar:
  stage: scanner
  only:
    - /^feature-.*$/
  tags:
    - sonar-docker
  script:
    - sonar-scanner-4.4.0 -Dsonar.projectName=$CI_PROJECT_NAME -Dsonar.projectKey=$CI_PROJECT_NAME -Dsonar.branch.name=${CI_COMMIT_REF_NAME} -Dsonar.host.url=http://isonar.test.com -Dsonar.sourceEncoding=UTF-8

2)打包docker build  images          .gitlab/docker/DefaultDockerBuild.yml

docker-build:
  image: $BASE_IMAGE
  stage: build
  tags:
    - kube-runner
  script:
    - echo "$project:$CI_COMMIT_SHORT_SHA $CI_JOB_ID"
    - docker build -t $project:$CI_COMMIT_SHORT_SHA --no-cache .
    - docker tag $project:$CI_COMMIT_SHORT_SHA $project:$release
  retry: 2
  rules:
    - exists:
        - Dockerfile

 

3)  上传到harbor  中  .gitlab/docker/PushImage.yml

docker-build:
  image: $BASE_IMAGE
  stage: build
  tags:
    - 10.121.116.100
  script:
    - echo "$project:$CI_COMMIT_SHORT_SHA $CI_JOB_ID"
    - docker build -t $project:$CI_COMMIT_SHORT_SHA --no-cache .
    - docker tag $project:$CI_COMMIT_SHORT_SHA $project:$release
  retry: 2
  rules:
    - exists:
        - Dockerfile

4) 同步kubernetes deployment 中

deploy_dev:
  image: $KUBECTL_TOOL
  stage: sync_img
  script:
    - kubectl set image deployment $deployment_name *=$project:$CI_COMMIT_SHORT_SHA -n $namespace --kubeconfig=.gitlab/kube_env/10.121.12.120/admin.conf
  tags:
    - k8s-runner1
  only:
    - master
    - merge_requests

gitlab 内置变量

$CI_COMMIT_SHORT_SHA      commit_id_hash

$CI_JOB_ID          job id
 $CI_PROJECT_URL               项目的url 
  
    $GITLAB_USER_EMAIL         提交者的mail 地址
posted @ 2021-02-02 14:48  扛把子修BUG  阅读(437)  评论(0编辑  收藏  举报