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

服务器说明:

192.168.192.120:项目服务器

192.168.192.121:GitLab

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

安装GitLab Runner参考:

GitLab实现CICD自动化部署_gitlab cidi_程序员xiaoQ的博客-CSDN博客

手动在 GNU/Linux 上安装极狐GitLab Runner | 极狐GitLab
 

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

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

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

在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 的持续集成2 · 大专栏

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.ymlbefore_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_KEYstages:          # List of stages for jobs, and their order of execution- build- test- deployvariables:PROJECT_NAME: ${CI_PROJECT_NAME}PROJECT_DIR: ${CI_PROJECT_DIR}build-job:       # This job runs in the build stage, which runs first.stage: buildscript:- 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: productionscript:- 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服务器上的内容:

可能出现的报错:

"Enter passphrase for /dev/fd/63" error (#1) · Issues · GitLab-examples / ssh-private-key · GitLab

 [Gitlab CI/CD] Error loading key “/dev/fd/63“: invalid format_wifiiiiiiii的博客-CSDN博客

gitlab-runner构建报错fatal: git fetch-pack: expected shallow listfatal: The remote end hung up unexpect_带着希望活下去的博客-CSDN博客

参考:

在 GitLab CI/CD 中通过 SCP 运行 Composer 和 npm 脚本 | 极狐GitLab

linux - .gitlab-ci.yml 中最简洁明了的SSH命令描述方式 - IT工具网

ssh远程执行多个命令_ssh远程执行多条命令_IT超人的博客-CSDN博客

lib/gitlab/ci/templates · master · GitLab.org / GitLab FOSS · GitLab

服务器配置gitlab-runner流程问题总结

基于 gitlab 的持续集成2 · 大专栏

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/58485.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

查看单元测试用例覆盖率新姿势:IDEA 集成 JaCoCo

1、什么是 IDEA IDEA 全称 IntelliJ IDEA&#xff0c;是 Java 编程语言开发的集成环境。IntelliJ 在业界被公认为最好的 Java 开发工具&#xff0c;尤其在智能代码助手、代码自动提示、重构、JavaEE 支持、各类版本工具(git、SVN 等)、JUnit、CVS 整合、代码分析、 创新的 GUI…

【力扣每日一题】2023.8.8 任意子数组和的绝对值的最大值

目录 题目&#xff1a; 示例&#xff1a; 分析&#xff1a; 代码&#xff1a; 题目&#xff1a; 示例&#xff1a; 分析&#xff1a; 题目给我们一个数组&#xff0c;让我们找出它的绝对值最大的子数组的和。 这边的子数组是要求连续的&#xff0c;让我们找出一个元素之和…

PHPstudy配置伪静态步骤,tp5.1的框架

搜索mod_rewrite.so&#xff0c;然后去掉前面的#&#xff08;即放开注释&#xff09; 2.找到index.php 同级文件.htaccess&#xff08;没有就新建&#xff09; 这些是tp5.1自带的内容&#xff0c;把它注释掉&#xff0c;是错误的内容&#xff0c;添加下面的这段配置 #<If…

paddlenlp:社交网络中多模态虚假媒体内容核查

初赛之环境配置篇 一、背景二、任务三、数据集1、初赛阶段2、评分标准 四、环境操作五、写在最后 一、背景 随着新媒体时代信息媒介的多元化发展&#xff0c;各种内容大量活跃在媒体内中&#xff0c;与此同时各类虚假信息也充斥着社交媒体&#xff0c;影响着公众的判断和决策。…

Spring Boot整合ES的两种方式

使用Spring Data Elasticsearch Starter 在Spring Boot中整合Elasticsearch的方式之一是使用Elasticsearch的官方Spring Data Elasticsearch Starter。该Starter提供了对Elasticsearch的高级集成&#xff0c;简化了配置和管理Elasticsearch客户端。 下面是使用Spring Data E…

Mysql的instr()函数用法详解

最近接手了一个大型老项目&#xff0c;用到的jfinal技术&#xff0c;后端大部分都是拼写的sql&#xff0c;对一些sql函数不太理解的我算是一个挑战&#xff0c;也是一个进步的很大空间。 今天来说下instr这个函数 首先看下我们的表数据 我们先执行&#xff1a; SELECT * fro…

【学习笔记】生成式AI(ChatGPT原理,大型语言模型)

ChatGPT原理剖析 语言模型 文字接龙 ChatGPT在测试阶段是不联网的。 ChatGPT背后的关键技术&#xff1a;预训练&#xff08;Pre-train&#xff09; 又叫自监督式学习&#xff08;Self-supervised Learning&#xff09;&#xff0c;得到的模型叫做基石模型&#xff08;Founda…

重发布选路问题

一、思路 &#xff1b; 1.增加不优选路开销解决选路不佳问题 2.用增加开销的方式使R1 不将ASBR传的R7传给另一台ASBR解决R1、R2、R3、R4pingR7环回环路 二、操作 ------IP地址配置如图 1.ospf及rip的宣告 rip&#xff1a; [r1]rip 1 [r1-rip-1]version 2 [r1-rip-1]netw…

详细介绍golang中.()类型断言的使用方法

文章目录 一、什么是.()用法&#xff1f;二、.()的基本用法三、.()用法的高级应用3.1 nil类型的转换3.2 将函数作为参数传递 四、.()使用中的注意事项五、总结 Golang是一门非常流行的编程语言&#xff0c;在很多领域都有着广泛的应用。在开发过程中&#xff0c;很多时候我们需…

2021年12月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试

一、单选题(共25题,每题2分,共50分) 第1题 昨天的温度是5摄氏度,今天降温7摄氏度,今天的温度是多少摄氏度? A:12 B:7 C:2 D:-2 正确的答案是 D:-2。 解析: 根据题目描述,昨天的温度是 5 摄氏度,今天降温了 7 摄氏度。降温意味着温度变低,所以今天的温度…

DLA 神经网络的极限训练方法:gradient checkpointing

gradient checkpointing 一般来说&#xff0c;训练的过程需要保存中间结果&#xff08;不管是GPU还是CPU&#xff09;。前向传播根据输入(bottom_data)计算输出(top_data)&#xff0c;后向传播由top_diff计算bottom_diff&#xff08;如果某个变量打开梯度进行训练的话&#xff…

Android 获取网络连接状态新方法

一. 问题背景 Android12上&#xff0c;有的app模块判断当前网络的类型和连接状态时&#xff0c;还是使用的旧的API&#xff0c;导致返回的结果不准确&#xff0c;影响代码逻辑判断&#xff0c;本篇文章就这一问题&#xff0c;整理一下判断网络类型和连接状态的新方法。 二. 原因…