编辑
2025-07-01
Python
00
请注意,本文编写于 49 天前,最后修改于 49 天前,其中某些信息可能已经过时。

目录

gitlab cicd
方案选择
前端发版时依赖参考

gitlab cicd

GitLab 提供的一套自动化流水线系统,用于自动构建、测试、部署代码

核心:命中某些分支后,自动将代码发布到生产或测试环境,无需人为确认发版,常用于apk打包、前端发版情况

方案选择

1、基于.gitlab-ci.yml配置的方案:(无法避免防火墙影响)

思路:

1)拉起gitlab上的Runner,Runner通过ssh跳到生产环境,然后执行脚本;

2)yml脚本参考:

bash
# CI 方式:自动登录进服务器,pull 然后 npm build # 参考文档:https://docs.gitlab.com/ee/ci/ssh_keys/#verifying-the-ssh-host-keys image: node stages: - deploy default: before_script: ## ## Create the SSH directory and give it the right permissions ## - mkdir -p ~/.ssh - chmod 700 ~/.ssh ## ## Assuming you created the SSH_KNOWN_HOSTS file type CI/CD variable, uncomment the ## following two lines. ## - cp "$SSH_KNOWN_HOSTS" ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts ## ## Install ssh-agent if not already installed, it is required by Docker. ## (change apt-get to yum if you use an RPM-based image) ## - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' ## ## Run ssh-agent (inside the build environment) ## - eval $(ssh-agent -s) ## ## Give the right permissions, otherwise ssh-add will refuse to add files ## Add the SSH key stored in SSH_PRIVATE_KEY file type CI/CD variable to the agent store ## - chmod 400 "$SSH_PRIVATE_KEY" - ssh-add "$SSH_PRIVATE_KEY" deploy-prod: stage: deploy only: - production tags: - iottest script: - ssh root@10.11.11.11 /var/docker/django/mjtest/front-dist/build-front.sh mjtest-front-aa deploy-sit: stage: deploy only: - sit tags: - iottest-sit script: - ssh root@10.11.11.11 /var/docker/django/mjtest/front-dist/build-front.sh mjtest-front-aa

3)sh脚本参考:见下脚本run in host的情况

2、基于settings中webhooks回调执行方案:(可以避免防火墙影响)

思路:

1)Trigger触发器配置规则后,当命中分支时,自动回调指定的URL并将请求头等信息发送过去;

2)核心的拉取代码及构建发版,依赖回调URL执行后,异步调用执行的脚本(需要提前配置好sh脚本,用以git clone 及npm build等操作,如果是django操作,可以基于celery任务拉起实现异步执行),其中,要注意执行环境需要支持git及node及yarn或npm版本;

3)sh脚本参考:

bash
#!/usr/bin/env bash set -e set -x proj_name=$1 src_dir=/var/docker/django/mjtest/$proj_name dist_dir=/var/docker/django/mjtest/front-dist/$proj_name if [ "$proj_name" = "" ]; then echo ">>> must specify project_name with command argument!" echo ">>> exit..." exit fi echo ">>> Distributing $proj_name ..." cd $src_dir git clean -df git pull git submodule update --remote --recursive # if run in docker yarn && yarn build # if run not in docker but in Host # docker run --rm -v $src_dir:/var/src -e YARN_REGISTRY=https://registry.npmmirror.com --workdir=/var/src node sh -c 'yarn && yarn build' rm -rf $dist_dir mv dist $dist_dir set +x

前端发版时依赖参考

1、如果是容器构建:

bash
docker run --rm -v $src_dir:/var/src -e YARN_REGISTRY=https://registry.npmmirror.com --workdir=/var/src node sh -c 'yarn && yarn build'

2、如果本身服务已经在容器里面:当前容器依赖安装

bash
前端发版容器参考: 前提:设定国内的 npm 镜像服务,加快下载 设置环境变量:export YARN_REGISTRY=https://registry.npmmirror.com 查看环境变量:echo $YARN_REGISTRY 一、确保node版本 # 移除当前版本 apt-get remove -y nodejs # 安装 NodeSource 仓库脚本 curl -fsSL https://deb.nodesource.com/setup_21.x | bash - # 安装 Node.js apt-get install -y nodejs # 查看版本 node -v 二、安装yarn 1、Yarn 提供了自己的仓库,你需要添加它: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list 2、更新包列表并安装 Yarn: apt-get update apt-get install -y yarn 3、验证 Yarn 版本: yarn -v

本文作者:lixf6

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!