安装 jenkins 的两种方式、于任务示例。
安装方式一: docker
1 2 3 4 5 6 7 8
| # 拉取 jenkins 镜像 docker pull jenkinsci/blueocean:latest # 创建挂载目录 mkdir /home/jenkins_home # 授权挂载目录 chmod 777 /home/jenkins_home # 运行jenkins 并挂载 在宿主机目录 /home/jenkins_home docker run -d --name jenkins -p 8081:8080 -v /home/jenkins_home:/var/jenkins_home --restart=always jenkinsci/blueocean:latest
|
安装方式二: yum
1 2 3 4 5 6 7 8 9
| sudo wget -O /etc/yum.repos.d/jenkins.repo \ https://pkg.jenkins.io/redhat/jenkins.repo sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key sudo yum upgrade sudo yum install epel-release java-11-openjdk-devel sudo yum install jenkins sudo systemctl daemon-reload sudo systemctl start jenkins sudo systemctl status jenkins
|
访问 jenkins
安装成功之
后进入 Jenkins ui 页面 进行任务创建。
1 2 3 4 5 6
| 1 浏览器访问 http://xxx.xxx.xx.xx:8081 2 根据提示 找到密码后 输入 初始化密码. 3 安装默认插件 4 新建用户 5 登录
|
构建任务
GitHub 代理
现在 git 好像不支持 用户密码授权了,要使用 git Personal AccessToken 代替密码 输入地址的授权凭证时注意以下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| # 下载 github 加速代理 https://github.com/dotnetcore/FastGithub/releases
wget https://github.com/dotnetcore/FastGithub/releases/download/2.1.2/fastgithub_linux-x64.zip unzip fastgithub_linux-x64.zip cd fastgithub_linux-x64 ./fastgithub start
# 设置代理 vim /etc/profile # 粘贴以下两行到文件末尾 export http_proxy=http://127.0.0.1:38457 export https_proxy=http://127.0.0.1:38457 # 然后保存 :wq # 让代理生效 source /etc/profile # 查看 env |grep -i proxy # 如果要取消代理 unset http_proxy unset https_proxy
|
jenkins 任务 git 代理
Github 之类的国外仓库, jenkins 有可能无法访问。
但是系统的 全局代理设置后却不对 jenkins 生效, 这个时候你需要在 jenkins 中再额外配置一次代理。
1 2 3 4 5 6 7 8
| 位置: 系统管理 > 系统配置 > 全局属性
# 新增两个键值对 # 键 http_proxy # 值 http://127.0.0.1:38457 # 键 https_proxy # 值 http://127.0.0.1:38457
|
构建 dotnet 镜像
docker build -t toolbox.api:latest .
打标签
docker tag toolbox.api:latest registry.cn-shenzhen.aliyuncs.com/project-workspace/toolbox:latest
推送镜像到 阿里云
docker push registry.cn-shenzhen.aliyuncs.com/project-workspace/toolbox:latest
运行容器
1 2 3 4 5 6 7
| # 登录 doceker login # 拉取镜像 docker pull registry.cn-hangzhou.aliyuncs.com/iamshen/offeringweb:latest # 运行 docker run --name=toolbox.api --env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -p 5200:80 --workdir=/app --restart=always --log-opt max-size=50m --log-opt max-file=3 --detach=true registry.cn-hangzhou.aliyuncs.com/iamshen/offeringweb:latest
|
构建脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| # 进入项目路径 cd /var/lib/jenkins/workspace/ToolBox/web/ToolBox.Api # 删除 发布包 rm -rf /var/lib/jenkins/workspace/ToolBox/web/ToolBox.Api/publish # 输出 dotnet 环境 /usr/share/dotnet/dotnet --info # dotnet 编译 /usr/share/dotnet/dotnet restore # dotnet 发布 /usr/share/dotnet/dotnet publish -c Release -o publish cd /var/lib/jenkins/workspace/ToolBox/web/ToolBox.Api/publish ls -l # 登录 阿里云 docker 镜像中心, 请提前把docker 镜像中心的 登录密码写到指定路径 /var/lib/pwd.txt 然后 chmod 777 /var/lib/pwd.txt # 如果报错,请添加dockers 用户组 usermod -a -G docker jenkins 然后重启 jenkins systemctl restart jenkins. cat /var/lib/pwd.txt | docker login --username 15958044099 registry.cn-shenzhen.aliyuncs.com --password-stdin # 构建镜像 docker build -t toolbox.api:latest . # 打标签 docker tag toolbox.api:latest registry.cn-shenzhen.aliyuncs.com/project-workspace/toolbox:latest # 推送镜像 docker push registry.cn-shenzhen.aliyuncs.com/project-workspace/toolbox:latest
|