1. DevOps 演示案例
1.1. Gitlab演示
1.1.1. 演示项目
- 创建一个Group:devops
- 创建一个Project: devops-demo
1.1.2. 演示用户
- 张三 用户名:zhangsan-dev zhangsan@example.com 开发工程师
- 李四 用户名:lisi-master lisi@example.com 开发经理
1.1.3. 权限分配
- 将张三分配到devops-demo项目,角色Developer
- 将李四分配到devops-demo项目,角色Master
1.1.4. Git演示
- 拉取代码并编写,上传代码。
启用Deploy Key
使用张三登录
- 生成证书 ssh-keygen -t rsa
- 将证书添加到Gitlab上 cat .ssh/id_rsa.pub
启用Deploy Key
1.1.5. Gitlab演示
- 创建Milestones,例如每个Sprint对应一个Milestone。创建v0.1
- 创建Issue,可以使用Issue模版,格式化Issue描述信息。 选择Milestone
- 创建Boards,通过KANBAN管理进度。
1.1.6. 提交阶段(Commit Stage):
目标:每次提交都可以自动触发进行单元测试、编译、质量扫描
触发条件:开发人员Push代码到Gitlab项目的develop分支,自动触发该阶段执行。
步骤:
拉取代码
- 代码编译
- 单元测试
- 质量扫描
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
git branch: 'develop', credentialsId: '0ee94a52-bd31-4e20-bdbf-6385533689c5', url: 'git@192.168.56.11:devops/jpress.git'
}
}
stage('代码编译') {
steps {
sh '/usr/local/maven/bin/mvn compile'
}
}
stage('单元测试') {
steps {
sh '/usr/local/maven/bin/mvn test'
}
}
}
post {
always {
echo 'One way or another, I have finished'
//deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}
1.1.7. Jenkins集成Gitlab
1.使用root用户登录Gitlab,创建Access Token 2.Jenkins-系统管理-系统设置-Gitlab设置
1.1.8. 提交阶段流水线自动触发
1.提交阶段Job设置构建触发器,选择分支过滤 2.Gitlab-项目-Setting-Integration-Webhook
1.1.9. 集成测试阶段
目标:将代码打包构建,并自动化部署到测试环境,运行自动化测试
触发条件:当Master分支有Merge操作,自动触发。
步骤:
拉取代码
- 单元测试
- 构建打包
- 上传到制品库
- 部署审核
- 自动化部署测试环境
- 自动化测试
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
git branch: 'master', credentialsId: '0ee94a52-bd31-4e20-bdbf-6385533689c5', url: 'git@192.168.56.11:devops/jpress.git'
}
}
stage('单元测试') {
steps {
sh '/usr/local/maven/bin/mvn test'
}
}
stage('构建打包') {
steps {
sh '/usr/local/maven/bin/mvn package'
}
}
stage('上传制品库') {
steps {
echo "1"
//sh "curl --fail -u admin:123456.coM --upload-file ./starter/target/starter-3.0.zip 'http://192.168.56.12:8081/repository/jpress/'"
}
}
stage('测试环境部署审核') {
steps {
input '是否部署测试环境?'
}
}
stage('自动化部署') {
steps {
echo "2"
//sh "/opt/deploy.sh SIT 3.0"
}
}
stage('自动化测试') {
steps {
echo "test"
//sh "/opt/test.sh SIT"
}
}
}
post {
always {
echo 'One way or another, I have finished'
//deleteDir() /* clean up our workspace */
}
success {
echo 'I succeeeded!'
}
unstable {
echo 'I am unstable :/'
}
failure {
echo 'I failed :('
}
changed {
echo 'Things were different before...'
}
}
}
1.1.10. 部署阶段
目标:部署代码到对应的环境上
触发条件:用户手工选择需要部署的环境进行触发
步骤:
- 自动化部署选择的环境
- 自动化冒烟测试
pipeline {
agent any
stages {
stage('自动化部署') {
steps {
echo "${DEPLOY_ENV}"
//sh "/opt/deploy.sh ${DEPLOY_ENV} ${DEPLOY_VER}"
}
}
stage('冒烟测试') {
steps {
echo "${DEPLOY_ENV}"
//sh "/opt/deploy.sh ${DEPLOY_ENV} ${DEPLOY_VER}"
}
}
}
}