gitlab 编排.gitlab-ci.yaml高级技巧

简介: gitlab 编排.gitlab-ci.yaml高级技巧

Gitlab 编排 .gitlab-ci.yaml

文章目录

1. 快速构建.gitlab-ci.yaml

创建一个项目

1832b220aa754cd18c504acc7686a560.png创建一个模板.gitlab-ci.yaml

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

选择Getting-started模板

1832b220aa754cd18c504acc7686a560.png

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
unit-test-job:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - echo "Running unit tests... This will take about 60 seconds."
    - sleep 60
    - echo "Code coverage is 90%"
lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 10
    - echo "No lint issues found."
deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

全部正常。

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

我们充分利用模板文件,可以为我们节省大量的时间。

2. python demo ci

2.1 推送项目至gitlab

记录模块清单

pip freeze > requestments.txt
git init
git add .
git commit -m "add a new python demo"
git remote add origin root@gitlab.example.com:8081/root/flask_web1.git
git push

2.2 安装gitlab-runner

docker run -d --name gitlab-runner --restart always --net=host \
     -v /srv/gitlab-runner/config:/etc/gitlab-runner \
     -v /var/run/docker.sock:/var/run/docker.sock \
     gitlab/gitlab-runner:latest

2.3 选择一个本地部署的需求

我们就要为该项目注册shell运行的执行器的runner

$ docker run --rm --net=host -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:alpine-v14.4.2 register
Runtime platform                                    arch=amd64 os=linux pid=7 revision=50fc80a6 version=14.4.2
Running in system-mode.                            
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://gitlab.example.com:8081
Enter the registration token:
8BxfhdBE2zf8NioR1UFE
Enter a description for the runner:
[yourdomain.com]: flask_demo
Enter tags for the runner (comma-separated):
python
Registering runner... succeeded                     runner=8BxfhdBE
Enter an executor: custom, parallels, docker+machine, kubernetes, docker, docker-ssh, shell, ssh, virtualbox, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

查看配置

$ cat /srv/gitlab-runner/config/config.toml 
concurrent = 1
check_interval = 0
[session_server]
  session_timeout = 1800
[[runners]]
  name = "demo"
  url = "http://gitlab.example.com:8081/"
  token = "PF41kT9ZV_1DoT6VzcCu"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "ubuntu:20.04"
    dns = ["8.8.8.8", "1.1.1.1"]
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    extra_hosts = ["gitlab.example.com:192.168.211.70"]
    shm_size = 0
..........
[[runners]]
  name = "flask_demo"
  url = "http://gitlab.example.com:8081"
  token = "tvCxfurs7EmRApH7un2a"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

为此项目注册runner,并不会影响其他项目的项目,因为配置是追加的。

选择一个最接近你的需求的、并修改量小的.gitlab-ci.yaml模板

1832b220aa754cd18c504acc7686a560.png

修改.gitlab-ci.yaml

我的本地环境是ubuntu:8.04,本地可能没有flask,所以要在.gitlab-ci.yaml,装完就可以部署,两步走,即两个stage

.gitlab-ci.yaml

stages:
  - dep          # List of stages for jobs, and their order of execution
  - deploy
dep-job:
  stage: dep
  script:
    - apt -y install python3-pip
    - pip3 install flask
deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  script:
    - echo "Deploying python flask demo application..."
    - python3 app.py"

创建一个Dockerfile模板

1832b220aa754cd18c504acc7686a560.png

1832b220aa754cd18c504acc7686a560.png

修改Dockerfile

1832b220aa754cd18c504acc7686a560.png

更多阅读:

相关文章
|
6天前
|
Java Linux Go
流水线 YAML 高级用法来了,大幅降低重复代码、灵活编排多任务
云效 Flow 流水线 YAML 引入了 template 语法,支持使用模板语言来动态渲染流水线 YAML,满足多个相同或类似逻辑的 Job 批量配置场景,满足多 Job 按需动态生成场景,帮助降低流水线 YAML 重复代码,灵活编排多任务。
|
运维 弹性计算 API
阿里云自动化部署和运维的最佳实践-基于YAML的编排
为了提升运维开发人员的开发效率,降低开发的难度,阿里云推出了资源编排(Resource Orchestration Service,简称ROS),和运维编排Operation Orchestration Service,简称OOS)这两个编排平台。作为平台级的产品,阿里云会负责保证其稳定可靠,用户只需要按照平台的规则制定自己的运维逻辑。
1866 0
|
6天前
|
存储 IDE 测试技术
玩转数据处理利器:学会使用 YAML 文件轻松处理数据
玩转数据处理利器:学会使用 YAML 文件轻松处理数据
62 0
|
7月前
|
Kubernetes Cloud Native 应用服务中间件
【云原生】使用k8s创建nginx服务—通过yaml文件svc类型暴露
【云原生】使用k8s创建nginx服务—通过yaml文件svc类型暴露
148 0
|
7月前
|
Web App开发 资源调度 JavaScript
SAP Fiori Elements 应用里的 ui5.yaml 文件详解试读版
SAP Fiori Elements 应用里的 ui5.yaml 文件详解试读版
80 0
|
7月前
|
Kubernetes 应用服务中间件 nginx
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
|
6天前
|
机器学习/深度学习 数据可视化 计算机视觉
YOLOv5改进 | 2023Neck篇 | 轻量级跨尺度特征融合模块CCFM(附yaml文件+添加教程)
YOLOv5改进 | 2023Neck篇 | 轻量级跨尺度特征融合模块CCFM(附yaml文件+添加教程)
198 1
|
6天前
|
XML 存储 Java
【OpenCV】—输入输出XML和YAML文件
【OpenCV】—输入输出XML和YAML文件
|
6天前
|
Shell Go
go 编辑yaml 文件
在Go语言中编辑YAML文件通常涉及以下步骤: 1. 读取YAML文件内容到字符串。 2. 使用YAML解析库(如`gopkg.in/yaml.v2`)将字符串解析为Go数据结构(如`map[string]interface{}`或自定义的结构体)。 3. 修改数据结构中的值以更新YAML内容。 4. 将修改后的数据结构编码回YAML格式的字符串。 5. 将字符串写回到YAML文件。 以下是一个简单的例子,展示了如何使用`gopkg.in/yaml.v2`库来编辑YAML文件: 首先,确保你已经安装了`gopkg.in/yaml.v2`包: ```bash go get gopkg.i
106 0
|
7月前
|
Kubernetes NoSQL 关系型数据库
通过编写k8s的资源清单yaml文件部署gitlab服务
通过编写k8s的资源清单yaml文件部署gitlab服务

热门文章

最新文章