当前位置:首页 > 技术文章 > 正文内容

编写简单的.gitlab-ci.yml打包部署项目

zonemu8小时前技术文章1

服务器说明:

192.168.192.120:项目服务器

192.168.192.121:GitLab

为了可以使用gitlab的cicd功能,我们需要先安装GitLab Runner

安装GitLab Runner参考:

https://blog.csdn.net/weixin_42304232/article/details/115727182

https://docs.gitlab.cn/runner/install/linux-manually.html

安装好GitLab Runner,我们主要编写.gitlab-ci.yml

在GitLab管理员界面,找到Settings -> CI/CD -> Variables,添加STAGING_PRIVATE_KEY变量

需要在192.168.192.120服务器执行ssh-keygen生成密钥对,其中STAGING_PRIVATE_KEY的值为私钥

# 参考:https://www.dazhuanlan.com/alexleeyy/topics/979217

在192.168.192.120服务器执行下面操作:
1、ssh-keygen,不要输入密码,直接回车
2、cat ~/.ssh/id_rsa,复制私钥到STAGING_PRIVATE_KEY
3、cd /root/.ssh/ && cat id_rsa.pub >> authorized_keys, 把 ssh 公钥添加到认证授权文件,这样可以不需要输入密码

gitlab管理后台,增加变量

.gitlab-ci.yml内容:

# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
 
before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$STAGING_PRIVATE_KEY")
  - echo $STAGING_PRIVATE_KEY
 
stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy
 
 
variables:
  PROJECT_NAME: ${CI_PROJECT_NAME}
  PROJECT_DIR: ${CI_PROJECT_DIR}
 
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo ${PROJECT_NAME}
    - echo ${PROJECT_DIR}
    - cd ${PROJECT_DIR} && cd ..
    - tar -cf "${PROJECT_NAME}.tar" ${PROJECT_NAME}
    - scp -o StrictHostKeyChecking=no project.tar root@192.168.192.120:/root/gitlab/
    - ssh -o StrictHostKeyChecking=no root@192.168.192.120 "
      cd /root/gitlab && 
      tar -xf project.tar && 
      pwd && 
      ls && 
      echo 'exit' && 
      exit
      "
    - 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.
  environment: production
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

部分说明:

# 不存在ssh-agent就下载安装
'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

# ssh-agent是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程
eval $(ssh-agent -s)

# ssh-add命令是把专用密钥添加到ssh-agent的高速缓存中,从而提高ssh的认证速度
ssh-add <(echo "$STAGING_PRIVATE_KEY")

# scp -o StrictHostKeyChecking=no,ssh -o StrictHostKeyChecking=no中的
StrictHostKeyChecking=no去掉对主机的验证检查
scp -o StrictHostKeyChecking=no project.tar root@192.168.192.120:/root/gitlab/
ssh -o StrictHostKeyChecking=no root@192.168.192.120

# 在192.168.192.120服务器执行以下命令
cd /root/gitlab && 
tar -xf project.tar && 
pwd && 
ls && 
echo 'exit' && 
exit

分支内容:

执行情况:

192.168.192.120服务器上的内容:

可能出现的报错:

https://gitlab.com/gitlab-examples/ssh-private-key/-/issues/1

参考:

https://docs.gitlab.cn/jh/ci/examples/deployment/composer-npm-deploy.html

https://gitlab.com/gitlab-org/gitlab-foss/tree/master/lib/gitlab/ci/templates

相关文章

费用管理系统(BMS)(费用管理系统的好处)

随着物流仓储行业的快速发展,费用管理系统的应用愈发重要。BMS系统不仅简化了报价与结算流程,还降低了管理成本,提升了效率。本文深入探讨了海外仓BMS系统的功能与应用,展示了其在费用管理中的重要作用,为...

垃圾收费管理系统(垃圾收运管理系统)

今天来了解《#垃圾收费管理系统》,它包括如下功能:收费单位表格界面,类别表格界面,类别设置信息窗口,单位信息表格界面,单位信息信息窗口,缴费登记信息窗口,缴费登记表格界面,缴费登记的缴费单.1,到期提...

中信泰富&amp;明源云——搭建营销费用管理系统,实现3个在线化

3月23日,中信泰富地产与明源云合作的营销费用管理系统业务解决方案汇报在中信泰富大厦顺利结束。 (会议现场)这是自双方合作以来,继今年1月底项目启动会后的又一重要推进动作,本次汇报得到了双方领导的高度...

Ubuntu 25.04发行版登场:Linux 6.14内核,带来多项技术革新

IT之家 4 月 18 日消息,科技媒体 linuxiac 昨日(4 月 17 日)发布博文,报道称代号为 Plucky Puffin 的 Ubuntu 25.04 发行版正式上线,搭载最新 Linu...

2024年10 大 Linux 桌面发行版推荐

年已过半,现在是探究 2024 年最流行的 Linux 发行版的最佳时机。Linux 是一个开源操作系统,构建在 Linux 内核上,并集成了 GNU shell 实用程序、桌面环境、应用程序、包管理...

2023 年 10 个最佳 Linux 桌面发行版

Linux 操作系统在桌面领域的发展已经不再被忽视,越来越多的用户正在考虑切换到 Linux 上。在 2023 年,我们可以期待更多的 Linux 桌面发行版的推出和发展。这里列举了 10 个最佳的...