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

更多阅读:

相关文章
|
9月前
|
网络安全 开发工具 git
在GitLab CI中同步HTTPS仓库地址的yaml配置
最后,提交并推送 `.gitlab-ci.yml`文件到您的GitLab仓库。GitLab CI/CD将自动识别这个文件,并在每次推送到 `master`分支时执行定义的同步任务。
464 16
|
敏捷开发 数据可视化 Devops
阿里云云效产品使用合集之可视化编排数据如何转换成YAML化编排数据
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
域名解析 Cloud Native jenkins
【Drone-初识篇】Drone借助GitLab构建CICD环境、以及编写 .drone.yaml 流水线
【Drone-初识篇】Drone借助GitLab构建CICD环境、以及编写 .drone.yaml 流水线
2081 0
|
运维 弹性计算 API
阿里云自动化部署和运维的最佳实践-基于YAML的编排
为了提升运维开发人员的开发效率,降低开发的难度,阿里云推出了资源编排(Resource Orchestration Service,简称ROS),和运维编排Operation Orchestration Service,简称OOS)这两个编排平台。作为平台级的产品,阿里云会负责保证其稳定可靠,用户只需要按照平台的规则制定自己的运维逻辑。
2133 0
|
Linux 应用服务中间件 网络安全
linux安装Gitlab
linux安装Gitlab
435 0
|
存储 安全 Linux
Linux服务器上安装配置GitLab的步骤。
按照以上步骤,一个基础的GitLab服务应该运行并可以使用。记得定期检查GitLab官方文档,因为GitLab的安装和配置步骤可能随着新版本而变化。
1047 0
|
Shell Docker 容器
GitlabCI学习笔记之一:安装Gitlab和GitLabRunner
GitlabCI学习笔记之一:安装Gitlab和GitLabRunner
|
Devops 持续交付 开发工具
入职必会-开发环境搭建54-GitLab下载和安装
GitLab 是一个基于 web 的 Git 仓库管理工具,提供了代码托管、版本控制、协作开发、持续集成等功能,是一个综合的 DevOps 平台。用户可以使用 GitLab 托管他们的代码仓库,并利用其丰富的功能来管理和协作开发项目。 以下是 GitLab 的一些主要特点和功能。
604 0
入职必会-开发环境搭建54-GitLab下载和安装
|
持续交付 开发工具 git
阿里云云效产品使用问题之在云效代码域中gitlab使用docker安装的,迁移时遇到“获取企业信息失败”,是什么原因
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。