搭建Jenkins +Github仓库 + Docker镜像自动化

环境说明:

  • 阿里云服务器
  • Ubuntu 16.04
  • docker

 

1、安装Jenkins环境,采用Docker安装(直接创建,如果本地没有镜像,则自动拉取)

docker run --name jenkins -u root -v /mydata/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker -p 80:8080 -d jenkins/jenkins

注意:如果后续构建部分不需使用docker创建镜像,可移除docker路径映射内容,其中docker 配置部分是,docker构建时必须加上,否则会出现 docker not found

-v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker

 

按照指定端口(上述设定是80)访问Jenkins,默认设置后如下图,

 

其中有一个注意点:默认生成的访问密码需要 docker logs查看生成日志,密码在日志内,如果不是首次创建此容器,可能看不到密码内容,

所以如果重新创建Jenkins容器时,要先把上述目录  /mydata/jenkins_home 删除,这样才能看到。

 

2、创建一个任务 Tom, 配置GitHub源码管理,此处的仓库为私有仓库,故而需要提供账户密码,以供 git clone 拉取

 

 

 

 

 

 3、配置构建环境,此处可根据实际情况勾选

 

 

 

4、编写构建 shell,脚本如下, 保存即可(Windows环境下应选择批处理文件)

#!/bin/sh

echo "准备构建-------------------------------准备中----------------------------------"
current=`date "+%Y-%m-%d %H:%M:%S"`  
timeStamp=`date -d "$current" +%s`   
#将current转换为时间戳,精确到毫秒  
timetag=$((timeStamp*1000+`date "+%N"`/1000000)) 
echo $timetag

echo "构建Docker镜像-------------------------------开始----------------------------------"
echo pwd
docker build -t tomcore:$timetag .
echo "构建Docker镜像-------------------------------完成----------------------------------"

echo "运行Docker容器-------------------------------------开始----------------------------"
docker run  --name=tomcore-$timetag -p 5001:80 -d tomcore:$timetag
echo "运行Docker容器------------------------------------完成-----------------------------"

注意:此处每次都会按照时间戳生成镜像,再按照此版本镜像生成容器,映射端口及内容

执行构建,控制台输入如下:

  1 Started by user tomkluas
  2 Running as SYSTEM
  3 Building in workspace /var/jenkins_home/workspace/Tom
  4 [WS-CLEANUP] Deleting project workspace...
  5 [WS-CLEANUP] Deferred wipeout is used...
  6 [WS-CLEANUP] Done
  7 The recommended git tool is: NONE
  8 using credential ae0787d0-540b-4469-93b8-e176742f1c46
  9 Cloning the remote Git repository
 10 Cloning repository https://github.com/tomkluas/Tom.git
 11  > git init /var/jenkins_home/workspace/Tom # timeout=10
 12 Fetching upstream changes from https://github.com/tomkluas/Tom.git
 13  > git --version # timeout=10
 14  > git --version # 'git version 2.11.0'
 15 using GIT_ASKPASS to set credentials github password
 16  > git fetch --tags --progress -- https://github.com/tomkluas/Tom.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 17  > git config remote.origin.url https://github.com/tomkluas/Tom.git # timeout=10
 18  > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 19 Avoid second fetch
 20  > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 21 Checking out Revision 814bb6977c6494397095e50af5875b5b1a617fbe (refs/remotes/origin/master)
 22  > git config core.sparsecheckout # timeout=10
 23  > git checkout -f 814bb6977c6494397095e50af5875b5b1a617fbe # timeout=10
 24 Commit message: "init"
 25  > git rev-list --no-walk 814bb6977c6494397095e50af5875b5b1a617fbe # timeout=10
 26 [Tom] $ /bin/sh /tmp/jenkins2099712060690249835.sh
 27 准备构建-------------------------------准备中----------------------------------
 28 1606720133343
 29 构建Docker镜像-------------------------------开始----------------------------------
 30 pwd
 31 Sending build context to Docker daemon  4.353MB
 32 
 33 Step 1/17 : FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
 34  ---> 0e394d284ae5
 35 Step 2/17 : WORKDIR /app
 36  ---> Running in 5a6352225a00
 37 Removing intermediate container 5a6352225a00
 38  ---> 3390c076daa4
 39 Step 3/17 : EXPOSE 80
 40  ---> Running in 6c0db2221b7d
 41 Removing intermediate container 6c0db2221b7d
 42  ---> 90c0fa08ce32
 43 Step 4/17 : EXPOSE 443
 44  ---> Running in 8c0a7b24be37
 45 Removing intermediate container 8c0a7b24be37
 46  ---> 2334d1f464df
 47 Step 5/17 : FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
 48  ---> 84c49e6701a5
 49 Step 6/17 : WORKDIR /src
 50  ---> Running in f6cad0846793
 51 Removing intermediate container f6cad0846793
 52  ---> 5c7b5e92b4d3
 53 Step 7/17 : COPY ["Tom.csproj", ""]
 54  ---> 17b54c93dc00
 55 Step 8/17 : RUN dotnet restore "./Tom.csproj"
 56  ---> Running in 58ce8d1666f0
 57   Determining projects to restore...
 58   Restored /src/Tom.csproj (in 2.41 sec).
 59 Removing intermediate container 58ce8d1666f0
 60  ---> df7edd43eac5
 61 Step 9/17 : COPY . .
 62  ---> 7e0f5785634a
 63 Step 10/17 : WORKDIR "/src/."
 64  ---> Running in 244f81392fae
 65 Removing intermediate container 244f81392fae
 66  ---> 517ff2e56382
 67 Step 11/17 : RUN dotnet build "Tom.csproj" -c Release -o /app/build
 68  ---> Running in 6314f7fd00ab
 69 Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
 70 Copyright (C) Microsoft Corporation. All rights reserved.
 71 
 72   Determining projects to restore...
 73   All projects are up-to-date for restore.
 74   Tom -> /app/build/Tom.dll
 75   Tom -> /app/build/Tom.Views.dll
 76 
 77 Build succeeded.
 78     0 Warning(s)
 79     0 Error(s)
 80 
 81 Time Elapsed 00:00:04.11
 82 Removing intermediate container 6314f7fd00ab
 83  ---> 7656ff11e2e4
 84 Step 12/17 : FROM build AS publish
 85  ---> 7656ff11e2e4
 86 Step 13/17 : RUN dotnet publish "Tom.csproj" -c Release -o /app/publish
 87  ---> Running in 034b0088414c
 88 Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
 89 Copyright (C) Microsoft Corporation. All rights reserved.
 90 
 91   Determining projects to restore...
 92   All projects are up-to-date for restore.
 93   Tom -> /src/bin/Release/net5.0/Tom.dll
 94   Tom -> /src/bin/Release/net5.0/Tom.Views.dll
 95   Tom -> /app/publish/
 96 Removing intermediate container 034b0088414c
 97  ---> c847d77d8dc6
 98 Step 14/17 : FROM base AS final
 99  ---> 2334d1f464df
100 Step 15/17 : WORKDIR /app
101  ---> Running in 13561bc7a562
102 Removing intermediate container 13561bc7a562
103  ---> 509fede2da18
104 Step 16/17 : COPY --from=publish /app/publish .
105  ---> 3fb05c92a7ed
106 Step 17/17 : ENTRYPOINT ["dotnet", "Tom.dll"]
107  ---> Running in 5e01c11f3ee0
108 Removing intermediate container 5e01c11f3ee0
109  ---> 0086746e5afe
110 Successfully built 0086746e5afe
111 Successfully tagged tomcore:1606720133343
112 构建Docker镜像-------------------------------完成----------------------------------
113 运行Docker容器-------------------------------------开始----------------------------
114 a78dda7ba430186b944c3e80aa996cae3a8d6cb193448bd34c9c0fcbe4413c1e
115 运行Docker容器------------------------------------完成-----------------------------
116 Finished: SUCCESS
View Code

 

查看生成的镜像及容器

 

 

 

容器 log 日志如下:

 

  访问网站5001端口,即可访问网站

 

posted @ 2020-11-30 15:27  tomkluas  阅读(490)  评论(0编辑  收藏  举报