jenkins:可视化,可管理化的部署
problem:
- jenkins用户配置git repo连接失败
solution:
Jenkins runs as another user, not as your ordinary login.
- Log on as jenkins
su jenkins
(you may first have to dosudo passwd jenkins
to be able to set the password for jenkins. I couldn't find the default...) - Generate ssh key pair:
ssh-keygen
- Copy the public key (
id_rsa.pub
) to your github account (or wherever) - Clone the repo as jenkins in order to have the host added to jenkins
known_hosts
which is neccessary to do. Now you can remove the cloned repo again if you wish.
problem:
- 使用jenkins构建报错:ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
solution:
是jenkins找不到分支来拉指定的git代码,是因为github上master节点名称变更为main
-----------------------------------------
① 下载git plugin;
配置git repository clone + global credentials
workspace path: /root/.jenkins/workspace
② Build
③ send build artifacts over SSH,配置源目录
配置 ssh server,包含接受的目录
1. Jenkins-freestyle构建项目后把文件ssh传输给serverB
1.0 plugins下载 ssh, ssh-credentials
比如使用git repo test.git
1.1 配置ssh-server(下载plugin: publish over ssh)
Dashboard > manage jenkins > system
(不确定是否over-operation)
我在jenkins server上用jenkins用户生成了id_rsa, 并把id_rsa.pub拷贝给了serverB的 ~/.ssh/下的authorized_keys
同时我在serverB创建了用户jenkins
su -s /bin/bash - jenkins # jenkins user ls -l /var/lib/jenkins/.ssh/id_rsa # check the auth sudo chown jenkins:jenkins /var/lib/jenkins/.ssh/id_rsa # change the auth # 想修改发现没权限 exit # using root sudo visudo jenkins ALL=(ALL) NOPASSWD: ALL # 继续 -bash-4.2$ ls -l /var/lib/jenkins/.ssh total 8 -rw-------. 1 jenkins jenkins 1679 Dec 12 17:06 id_rsa -rw-r--r--. 1 jenkins jenkins 389 Dec 12 17:06 id_rsa.pub #发送 ssh-copy-id -i id_rsa.pub -p22 jenkins@192.168.2.112
path-to-key: the path of id_rsa on jenkins server
通过ssh登录到serverB的root用户
git文件传输到/root/data2下面,必须exists
1.2 post-build action
source files: 从workspace/[free-style-name]目录下面开始算。
1. 先从git repo上pull代码到workspace
2. 模拟在本地build出可执行文件,手动mkdir target/
3. 从本地target下发送到remote
- target/*.go --- test/target下面的1layer,go结尾
- target/* --- test/target下面的1layer, all
- target/** --- test/target下面的2layers, all
remote directory: 发送到/root/data2(上面配ssh server配置的)以下的gin_test/
2. pipeline
相对freestyle的界面化,pipeline的门槛较高。
复杂的构建过程,维护不容易,选择pipeline!
install pipeline plugin
turn each command into shell script, add into Groovy:
pipeline grammar => choose Git => generate scripts equals to 'git pull'
=> choose sh:shell Script => generate 'go build' grammar
=> choose send over SSH => generate ...
pipeline { agent any stages { stage('pull code') { steps { git branch: 'main', credentialsId: 'github-hewenyan', url: 'git@github.com:sabertobihwy/test.git' } } stage('build code') { steps { sh '''echo "start building..." echo "end building..."''' } } stage('deploy code') { steps { sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.2.112', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'echo "transfer files success..."', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'pipeline-test/', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'target/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
但是把pipeline代码放到jenkins不安全:万一jenkins挂了怎么办?
=> pipeline script from SCM
3. Jenkinsfile - pipeline
what if we want to add version control to the pipeline? --- jenkinsfile
1. add pipeline script to Jenkinsfile
2. choose SCM
3. this is the relational path
4. trigger
1. 可以多个pipeline之间的依次构建
2. remote build ,access from an URL,允许go代码中直接触发pipeline
then visit-> 192.168.2.108:8080/job/pipeline-test/build?token=randomToken
3. build periodically using "cron job" / poll SCM periodically, if updates, execute
1.定时构建语法
定时字符串从左到右依次: 分 时 日 月 周
*号等同于H,表示任意一个合理的数
* * * * *
第一个*表示分钟,取值0~59,若其他值不做设定,则表示每个设定的分钟都会构建
5 * * * * ,表示每个小时的第5分钟都会构建一次
第二个*表示小时,取值0~23, 若其他值不做设定,则表示每个设定小时的每分钟都会构建
* 5 * * * ,表示在每天5点的时候,一小时内每一分钟都会构建一次
第三个*表示一个月的第几天,取值1~31,若其他值不做设定,则表示每个月的那一天每分钟都会构建一次
* * 5 * *,表示在每个月5号的时候,0点开始每分钟构建一次
第四个*表示第几月,取值1~12,若其他值不做设定,则表示每年的那个月每分钟都会构建一次
* * * 5 *,表示在每年的5月份,1号0点开始每分钟构建一次
第五个*表示一周中的第几天,取值0~7,其中0和7代表的都是周日,若其他值不做设定,则表示每周的那一天几每分钟都会构建一次
* * * * 5,表示每周五0点开始每分钟构建一次
2.构建实例
H used to distribute jobs across time, ***** IS EQUIVALENT TO H/1****
每天凌晨2:00跑一次
H 2 * * *
每隔5分钟构建一次
H/5 * * * *
每两小时构建一次
H H/2 * * *
每天中午12点定时构建一次
H 12 * * * 或0 12 * * *(0这种写法也被H替代了)
每天下午18点前定时构建一次
H 18 * * *
每15分钟构建一次
H/15 * * * * 或*/5 * * * *(这种方式已经被第一种替代了,jenkins也不推荐这种写法了)
周六到周日,18点-23点,三小时构建一次
H 18-23/3 * * 6-7
4. poll SCM: poll changes in SCM; expensive operation
5. Build with parameters
in Jenkinsfile, modify to -> remoteDirectory: '${remote_dir}/'
Jenkins 部署example
jenkins server: build executable file and send to production server
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2021-12-12 CSS记录