一、环境搭建
1. 安装并启动 DevStar
在Ubuntu24.04下使用以下命令完成安装:
wget -c https://devstar.cn/assets/install.sh && chmod +x install.sh && sudo ./install.sh
安装完成:
启动devstar
sudo devstar start
启动成功
二、平台初始化
访问本机的8080端口,在浏览器输入http://localhost:8080并访问,点击立即安装
安装完成后进行注册
三、项目开发
注册完成后进入到DevStar首页:
点击用户头像旁边的"+"图标创建仓库
初始化本地仓库:
mkdir gitprojects && cd gitprojects
git init
echo "print('Hello World')" > main.py
git add .
git commit -m "first commit"
commit时可能需要设置身份标识:
提交:
关联远程仓库:
git remote add origin http://localhost:8080/用户名/仓库名.git
提交至远程仓库:
git push -u origin 默认分支名
我的默认分支名是master,根据实际情况修改
创建分支:
#新建分支
git checkout -b 分支名
#自己根据需要实现功能
#分支备注
git commit -am "分支备注"
#提交代码到分支
git push origin 分支名
结果如图:
合并代码:
点击创建合并请求,会有详细的命令行提示
已经合并完成了。
发布版本:
git tag -a v1.0 -m "Release version 1.0"
git push origin v1.0
四、CI\CD流水线使用
CI/CD 流水线是现代软件开发中保障高效交付的关键工具。CI 即持续集成(Continuous Integration),CD 可指代持续交付(Continuous Delivery)、持续部署(Continuous Deployment)。
- 持续集成(CI):开发人员频繁地将代码变更合并到共享仓库(如 Git 仓库)中,每次合并后,CI 系统自动运行一系列的构建和测试流程,包括代码编译、单元测试、代码质量检查等。通过持续集成,能快速发现代码集成过程中的问题,如代码冲突、单元测试失败等,让团队成员在开发早期就解决问题,避免问题在后期累积,提高代码质量和项目稳定性。例如,一个 Web 开发团队中,多个开发人员同时开发不同功能模块,他们每天多次将代码提交到主分支,CI 系统会自动编译代码并运行单元测试,一旦发现某个提交导致测试失败,就及时通知开发人员修复。
- 持续交付(CD - Continuous Delivery):在持续集成的基础上,持续交付强调将通过测试的代码自动部署到预生产环境(如测试服务器、 staging 环境),使代码随时可发布到生产环境。但发布到生产环境这一步仍需人工触发。持续交付流程包括自动化构建、测试、打包和部署到预生产环境,确保软件在任何时候都处于可发布状态,减少发布风险和时间成本。
- 持续部署(CD - Continuous Deployment):持续部署是持续交付的进一步延伸,当代码通过所有测试后,会自动部署到生产环境,无需人工干预。这要求整个软件交付流程高度自动化和可靠,对测试的全面性和准确性有很高要求。例如,一些互联网公司的小型功能更新或 bug 修复,通过持续部署能快速让新功能或修复后的版本上线,及时响应用户需求。
首先新建工作流文件
name: Simple CI/CD Pipeline
on:
push:
branches:
- main
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build application
run: |
echo "Building application..."
mkdir -p dist
echo "Version: $(date +%Y%m%d)-$(git rev-parse --short HEAD)" > dist/version.txt
echo "Hello from CI/CD!" > dist/output.txt
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
test:
runs-on: ubuntu-latest
needs: build
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: build-output
path: dist/
- name: Run tests
run: |
echo "Running tests..."
if [ -f "dist/output.txt" ]; then
echo "File existence test passed!"
else
echo "File existence test failed!"
exit 1
fi
if grep -q "Hello from CI/CD!" dist/output.txt; then
echo "Content validation test passed!"
else
echo "Content validation test failed!"
exit 1
fi
deploy:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: build-output
path: deploy/
- name: Deploy to production
run: |
echo "Deploying to production environment..."
ls -la deploy/
echo "Deployment completed for $(cat deploy/version.txt)"
文件路径应该为你的仓库名/.gitea/workflows/文件名.yaml,这个路径下的yaml文件会被自动识别。例如:
接下来对仓库的指定操作就可以触发工作流了,比如我向仓库推送代码:
在仓库界面的Actions中就可以查看到变化: