1. 程式人生 > 實用技巧 >搭建Jenkins +Github倉庫 + Docker映象自動化

搭建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埠,即可訪問網站