安装部署jenkins [八]

 

 

安全与权限

使用插件Role-based Authorization Strategy

Manage Roles

Assign Roles

使用deva账户测试

新建视图

Manage Roles

Assign Roles

  测试

1.安装插件 Role
2.启用该插件Configure Global Security
3.系统管理 Manageand Assign roles 创建dev角色
4.给deva分配Global roles 中的dev角色
5.Project roles 创建基于Job的角色 dev-job 通过正则表达式进行匹配 dev-.*
6.

权限配置错误导致打不开Jenkins---->解决

导出,使用notepad++折叠修改
[root@linux-node1 ~]# sz /root/data/jenkins/config.xml

基于pipeline构建

node {
    stage('Code PUll'){
        echo "Code PUll"
    }
    stage('Code Build'){
        echo "Code Build"
    }
    stage('Unit Test'){
        echo "Unit test"
    }
}

点击Open Blue Ocean 进到另一种页面

部署下一步骤。是否需要继续

node {
    stage('Code PUll'){
        echo "Code PUll"
    }
    stage('Code Build'){
        echo "Code Build"
    }
    stage('Unit Test'){
        echo "Unit test"
    }
    stage('是否部署?'){
         input ' 部署测试环境'
    }
}
input

执行脚本的输出

把密钥粘到如下地方拉取代码

 

node {
    stage('Code PUll'){
        echo "Code PUll"
    }
    stage('Code Build'){
        echo "Code Build"
    }
    stage('Unit Test'){
        echo "Unit test"
    }
    stage('是否部署?'){
        input ' 部署测试环境'
    }
    stage('Deploy Test ENV'){
        echo "Test"
        sh '/opt/test.sh test'
    }
}
执行脚本的构建

拉取代码

构建

node {
    stage('Code PUll'){
        echo "Code PUll"
        git credentialsId: '142276b8-fda2-4589-8892-c0bcbbb9adac', url: 'http://192.168.56.12/devops/account.git'
    }
    stage('Code Build'){
        echo "Code Build"
        sh 'mvn clean compile'
    }
    stage('Unit Test'){
        echo "Unit test"
    }
    stage('是否部署?'){
        input ' 部署测试环境'
    }
    stage('Deploy Test ENV'){
        echo "Test"
        sh '/opt/test.sh test'
    }
}
没有环境变量会报错

node {
    stage('Code PUll'){
        echo "Code PUll"
        git credentialsId: '142276b8-fda2-4589-8892-c0bcbbb9adac', url: 'http://192.168.56.12/devops/account.git'
    }
    stage('Code Build'){
        echo "Code Build"
        sh 'source /etc/profile && mvn clean compile'
    }
    stage('Unit Test'){
        echo "Unit test"
    }
    stage('是否部署?'){
        input ' 部署测试环境'
    }
    stage('Deploy Test ENV'){
        echo "Test"
        sh '/opt/test.sh test'
    }
}
加上环境变量

[root@linux-node1 ~]# which mvn
/root/mvn/apache-maven-3.5.2/bin/mvn

可以使用语法生产器

jenkins和gitlab做集成

1.Jenkins配置gitlab 系统管理 系统设置
2. 在gitlab上生成api token给第一步用
3.jenkins增加构建触发器。勾选上gitlab,同时生成一个token。
4.gitlab上创建webhook。填写job的url和token

配置gitlab-api-token

manage---->下拉到

gitlab和jenkins做集成

Enable SSL verification去掉(没有SSL)

测试 :自动触发构建

[root@linux-node2 ~]# git clone git@192.168.56.12:devops/account.git
[root@linux-node2 ~]# cd account/
[root@linux-node2 account]# git status
# On branch master
[root@linux-node2 account]# git fetch
[root@linux-node2 account]# git checkout dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[root@linux-node2 account]# git status
# On branch dev
[root@linux-node2 account]# git config --global user.email "xusx"      填写用户和邮箱
[root@linux-node2 account]# git config --global user.name "root@devops.com"
[root@linux-node2 account]# echo "hehe-haha" > index.html
[root@linux-node2 account]# git add index.html
[root@linux-node2 account]# git commit -m "test"
[root@linux-node2 account]# git push

到gitlab上去create merge request

1.开发提交一个开发分支(不允许开发直接提交master分支)频繁提交。

2.合并到master(merge)

不要点Remove删除原分支,点击Merge后就能把代码合并到master

当master收到合并请求后,在跑一个Test测试流水线

当merge request触发的时候可以自动跑测试流水线

node {
    stage('Code PUll'){
        echo "Code PUll"
        git credentialsId: '142276b8-fda2-4589-8892-c0bcbbb9adac', url: 'http://192.168.56.12/devops/account.git'
    }
    stage('Code Build'){
        echo "Code Build"
        sh '/root/mvn/apache-maven-3.5.2/bin/mvn clean compile'
    }
    stage('Unit Test'){
        echo "Unit test"
        sh '/root/mvn/apache-maven-3.5.2/bin/mvn clean test'
    }
    stage('Package'){
        echo "Package"
        sh '/root/mvn/apache-maven-3.5.2/bin/mvn clean package'
    }
    stage('是否部署?'){
        input ' 部署测试环境'        
    }
    stage('Deploy Test ENV'){
        echo "Deploy Test ENV"
        sh 'ls -al'
    }
}
测试阶段流水线

 构建触发器

Jenkins和gitlab做集成

[root@linux-node2 account]# pwd
/root/account
[root@linux-node2 account]# echo "wang ni ma" > wang.html
[root@linux-node2 account]# git add wang.html
[root@linux-node2 account]# git commit -m "add wang.html"
[root@linux-node2 account]# git push

git push提交后,自动触发构建,每次写完push一次,可以提高开发的成功率

当一天的开发任务完成后,去创建new merger request

生产部署

参数化构建

Choice ParameterString Parameter

最贴近生产的

总结

1.提交阶段流水线: 1.更新代码 2.构建 3.单元测试 4.质量扫描(开发只要提交代码就会触发这个阶段的流水线
2.集成测试阶段流水线:  1.更新代码 2.构建 3.单元测试 4.质量扫描 5.打包 6.部署到测试环境 7.自动化API测试(只要master上有代码push/合并,就会触发
3.生成部署阶段流水线: 1.用户输入参数,调用部署脚本进行自动化部署(生产部署阶段流水线

确保代码提交构建成功后才能合并到master

node {
    gitlabCommitStatus(builds:["Commit Build"]){
      stage('Code PUll'){
          echo "Code PUll"
          git credentialsId: '142276b8-fda2-4589-8892-c0bcbbb9adac', url: 'http://192.168.56.12/devops/account.git'
      }
      stage('Code Build'){
          echo "Code Build"
          sh '/root/mvn/apache-maven-3.5.2/bin/mvn clean compile'
      }
      stage('Unit Test'){
          echo "Unit test"
      }
      stage('是否部署?'){
        
      }
      stage('Deploy Test ENV'){
          echo "Test"
          sh '/opt/test.sh test'
        }
    }
}
gitlabCommitStatus
[root@linux-node2 account]# pwd
/root/account
[root@linux-node2 account]# vim info.php
[root@linux-node2 account]# git add info.php
[root@linux-node2 account]# git commit -m "add info.php"
[dev 95abb82] add info.php
 1 file changed, 4 insertions(+)
 create mode 100644 info.php
[root@linux-node2 account]# git push
代码修改后提交

自动触发构建

 

posted @ 2017-11-27 21:56  reborn枪  阅读(150)  评论(0)    收藏  举报