自动化部署 - CI/CD 流水线配置在CI/CD菜单的编辑器中,先选择对应的项目分支,再配置流水线按钮,自动生成名为 .gitlab-ci.yml 的文件于此项目的根目录;在这里,流水线配置文件 .gitlab-ci.yml 决定了自动化部署的步骤过程 。起初GitLab会给出一个配置模板,这里将配置好的内容如下:
# 总流程 - 按序运行# 这里自定义了七个步骤,可按实际情况自定义名称和顺序,通过命令完成部署stages: # List of stages for jobs, and their order of execution - stop # Job1:停止原有站点运行 - clear # Job2:清除原有部署文件 - clone # Job3:远程克隆源代码 - test # Job4:单元测试 - build # Job5:编译源代码 - publish # Job6:发布项目 - deploy # Job7:重新启动站点运行#### 以下每个作业(步骤节点)对应上述总流程的步骤名称,如下示例每个节点区块格式:# {自定义作业名称}:# stage: {对应上述总流程定义的作业节点名称}# script:# - {按序单行要执行的命令}# - {按序单行要执行的命令}#### Job1:停止原有站点运行stop-job: stage: stop script: - ps -ef | grep Web.dll | grep -v grep | awk '{print $2}' | xargs -r kill -9 && true=0 || false=1## Job2:清除原有部署文件clear-job: stage: clear script: - cd /opt/gitlab-devops-app/ - rm -rvf my-project-test## Job3:远程克隆源代码clone-job: stage: clone script: - cd /opt/gitlab-devops-app/ - git clone -b {分支名称} http://{用户名}:{密码}@{ServerIP}/{project-url}/my-project-test.git## Job4:单元测试;对克隆下来的源代码进行操作unit-test-job: stage: test script: - cd /opt/gitlab-devops-app/my-project-test/Web/ - dotnet test Web.csproj## Job5:编译源代码build-job: stage: build script: - cd /opt/gitlab-devops-app/my-project-test/Web/ - dotnet build --configuration Release## Job6:发布项目publish-job: stage: publish script: - mkdir /opt/gitlab-devops-app/my-project-test/publish/ - cd /opt/gitlab-devops-app/my-project-test/Web/ - dotnet publish --configuration Release --no-build --output ../publish/## Job7:重新启动站点运行deploy-job: stage: deploy environment: production script: - cd /opt/gitlab-devops-app/my-project-test/publish/ - nohup dotnet Web.dll --urls http://*:5000 > /dev/null 2>&1 &以上配置提交保存后,在 CI/CD 的流水线菜单中会显示一条变更后要执行的任务,并且自动按配置的作业节点执行,自动运行的原因是先前配置了runner的[运行未标记的作业] 。效果如下图所示:
经验总结扩展阅读
- 2. 单主机 Elasticsearch 双节点或多节点集群环境部署
- Docker 部署Kibana
- GitLab + Jenkins + Harbor 工具链快速落地指南
- Docker | 部署nginx服务
- mac通过docker一键部署Nexus3
- mac通过docker一键部署MySQL8
- CentOS部署MySQL
- gin项目部署到服务器并后台启动
- centos7中配置java + mysql +jdk+使用jar部署项目
- 云原生下基于K8S声明式GitOps持续部署工具ArgoCD实战-上