这是jenkins专题的最后一篇文章了,搞起小伙伴们,不想废话,大家都比较忙,点击 create new jobs,创建一个job

输入job的名称,My first job ,选择pipeline,点击ok

现在不要问什么是pipeline,只管往下做,做完你就知道了,做完还不知道什么是pipeline,把jenkins卸载了就行了,你无缘于持续集成和交付
点击 pipeline script from scm(从版本控制仓储那里获得脚本)

选择git

输入我们仓储的地址(这个代码就是我们应用程序的代码),这时候我们发现拉取不了我们github上的代码,问题在于没有权限,先保存

为什么jenkins拉取不了我们自己github上的项目呢,其实也简单,因为jenkins和我们自己的github没有关联,他没有权限当然拉取不了代码,那么ssh出场了
打开我们linux终端(里面运行着jenkins服务的机器)
bash-4.2$ mkdir githubrepos
-bash-4.2$ cd githubrepos/
-bash-4.2$ mkdir .ssh
-bash-4.2$ ls -al
total 4
drwxrwxr-x. 3 jenkins jenkins 18 Feb 22 23:33 .
drwxr-xr-x. 30 jenkins jenkins 4096 Feb 22 23:33 ..
drwxrwxr-x. 2 jenkins jenkins 6 Feb 22 23:33 .ssh
ssh-keygen -t rsa
输入以下命令

test是我们要生成文件的名称,密码就随便输入吧,保证两次输入的正确就行
下图这个两个文件就是我们刚才生成的,一个公钥一个私钥,
命令:cat test.pub,查看我们的公钥

现在打开我们github上项目,点击setting

点击add deploykey

把我们的刚生成出来的公钥,添加到GitHub上

回到jenkins页面
点击Credential

点击global

点击add credentials

选择 ssh username with private key,勾选enter dicrectly,把私钥塞进去,这个证书的名字啦什么的,就自己随便输入吧,我都是写的test


点击配置我们的Job

又进入这个熟悉的页面,唯一不同的是,我们是带着credentials来的

选择我们刚刚创建的test credentials,发现可以了

我们是用jenkins构建应用,然后需要用到docker的命令,需要也要安装下docker(这个步骤省略,请看docker专题)
安装完成后,加入用户组输入以下命令
usermod -aG docker jenkins
usermod -aG root jenkins
chmod 664 /var/run/docker.sock
chmod 777 /var/run/docker.sock
sudo usermod -a -G docker $USER
reboot
把jenkins加入docker的用户组,(没有这一步jenkins不能运行docker的命令会说没有权限)
之后就是开始build和push啦,这个过程就是pipeline
所有的秘密都在jenkinsfile 里面
pipeline {
agent any
stages {
/*stage('Pull') {
* steps {
* git branch: 'master', credentialsId: '8446d8f7-e247-4ab4-babc-4ac962cc1d8f', url: 'https://github.com/gty1223984269/ImageService.git'
*
* }
*
* }*/
stage('Build') {
steps {
sh'chmod +x CI/build.sh'
sh'CI/build.sh'
}
}
stage('Deploy') {
steps {
sh'chmod +x CI/deploy.sh'
sh'CI/deploy.sh'
}
}
}
}
这是我们的jenkins file 每个stage就是一块操作,
build和deploy都是我们的sh脚本
这是build脚本,就是删除所有docker,进行build
docker rm$(docker images -a -q)
docker build.-t imageservice
这是deploy脚本.登录阿里云,进行image 的push
docker login --username=122398xxxxx@qq.com --password=xxxxx registry.cn-qingdao.aliyuncs.com
docker tag imageservice registry.cn-qingdao.aliyuncs.com/aliyun-jack/imageservice:2.1
docker push registry.cn-qingdao.aliyuncs.com/aliyun-jack/imageservice:2.1
什么是pipeline?
1,pull代码
2,单元测试
3,build代码
4,push应用
每个过程都是一个stage,就像一个管道一样,pipeline,jenkins专题到这里也就结束了
下个专题应该是kubernetes
网友评论