假如把開發(fā)工作流程分為以下幾個階段:
編碼 -> 構建 -> 集成 -> 測試 -> 交付 -> 部署
持續(xù)集成是指軟件個人研發(fā)的部分向軟件整體部分交付应结,頻繁進行集成以便更快地發(fā)現其中的錯誤缀踪【猓“持續(xù)集成”源自于極限編程(XP)忿薇,是 XP 最初的 12 種實踐之一裙椭。
持續(xù)交付在持續(xù)集成的基礎上,將集成后的代碼部署到更貼近真實運行環(huán)境的「類生產環(huán)境」(production-like environments)中煌恢。持續(xù)交付優(yōu)先于整個產品生命周期的軟件部署骇陈,建立在高水平自動化持續(xù)集成之上。
持續(xù)部署是指當交付的代碼通過評審之后瑰抵,自動部署到生產環(huán)境中你雌。持續(xù)部署是持續(xù)交付的最高階段。這意味著二汛,所有通過了一系列的自動化測試的改動都將自動部署到生產環(huán)境婿崭。它也可以被稱為“Continuous Release”。
「持續(xù)集成(Continuous Integration)」肴颊、「持續(xù)交付(Continuous Delivery)」和「持續(xù)部署(Continuous Deployment)」提供了一個優(yōu)秀的 DevOps 環(huán)境氓栈,對于整個團隊來說,好處與挑戰(zhàn)并行婿着。無論如何授瘦,頻繁部署醋界、快速交付以及開發(fā)測試流程自動化都將成為未來軟件工程的重要組成部分。
Jenkins2.107.3版本修改啟動端口號(Windows)
可能有一些原因提完,8080端口被占用了形纺,無法使用時需要修改jenkins的啟動端口號。
如果首次安裝徒欣,建議先停止原有系統(tǒng)的8080端口占用逐样,等jenkins安裝完成后,再進行修改打肝,然后該回8080的原系統(tǒng)端口脂新。
先停止jenkins服務
net stop jenkins
打開"C:\Program Files (x86)\Jenkins\jenkins.xml"
修改
<!--
The MIT License
Copyright (c) 2004-2017, Sun Microsystems, Inc., Kohsuke Kawaguchi, Oleg Nenashev, and other Jenkins contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!--
Windows service definition for Jenkins.
To uninstall, run "jenkins.exe stop" to stop the service, then "jenkins.exe uninstall" to uninstall the service.
Both commands don't produce any output if the execution is successful.
-->
<service>
<id>Jenkins</id>
<name>Jenkins</name>
<description>This service runs Jenkins automation server.</description>
<env name="JENKINS_HOME" value="%BASE%"/>
<!--
if you'd like to run Jenkins with a specific version of Java, specify a full path to java.exe.
The following value assumes that you have java in your PATH.
-->
<executable>%BASE%\jre\bin\java</executable>
<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=9003 --webroot="%BASE%\war"</arguments>
<!--
interactive flag causes the empty black Java window to be displayed.
I'm still debugging this.
<interactive />
-->
<logmode>rotate</logmode>
<onfailure action="restart" />
<!--
In the case WinSW gets terminated and leaks the process, we want to abort
these runaway JAR processes on startup to prevent corruption of JENKINS_HOME.
So this extension is enabled by default.
-->
<extensions>
<!-- This is a sample configuration for the RunawayProcessKiller extension. -->
<extension enabled="true"
className="winsw.Plugins.RunawayProcessKiller.RunawayProcessKillerExtension"
id="killOnStartup">
<pidfile>%BASE%\jenkins.pid</pidfile>
<stopTimeout>10000</stopTimeout>
<stopParentFirst>false</stopParentFirst>
</extension>
</extensions>
<!-- See the referenced examples for more options -->
</service>
重新啟動jenkins服務
net start jenkins
安裝插件
之所以要安裝插件是我們以后的操作都要用到插件。沒有的話粗梭,基本干不了活争便。
因為我們是要使用GitHub,所以需要用到Git plugin楼吃、和GitHub plugin.
我們進入:系統(tǒng)管理 -->管理插件 --->可選插件
Git client plugin
Utility plugin for Git support in JenkinsGit plugin
This plugin integrates Git with Jenkins.MSBuild Plugin
This plugin makes it possible to build a Visual Studio project (.proj) and solution files (.sln).
配置Git
系統(tǒng)管理 --->Global Tool Configuration 點擊git安裝始花,注意:是git文件夾中的cmd文件夾中的git.exe。
配置GitHub
GitHub webhooks 設置
webhook是通知Jenkins時的請求地址孩锡,用來填寫到GitHub上酷宵,這樣GitHub就能通過該地址通知到Jenkins;
假設Jenkins所在服務器的地址是:192.168.0.1躬窜,端口為8080浇垦,那么webhook地址就是http://192.168.0.1:8080/github-webhook
再次提醒,上述地址必須是外網也能訪問的荣挨,否則GitHub無法訪問到Jenkins男韧;
生成Personal access tokens
Jenkins訪問GitHub工程的時候,有的操作是需要授權的默垄,所以我們要在GitHub上生成授權的token給Jenkins使用此虑,這就是Personal access tokens,生成步驟如下:
Jenkins配置GitHub訪問權限
GitHub Plugin插件口锭,在”系統(tǒng)管理->管理插件”位置檢查此插件是否已經安裝朦前,沒有的話請先安裝;
配置GitHub鹃操,點擊“系統(tǒng)管理->系統(tǒng)設置”韭寸,如下圖:
在系統(tǒng)設置頁面找到”GitHub”,配置一個”GitHub Server”荆隘,如下圖恩伺,”API URL”填寫”https://api.github.com“,”Credentials”位置如下圖紅框所示椰拒,選擇”Add->Jenkins”:
彈出的頁面中晶渠,”Kind”選擇”Secret text”凰荚,”Secret”填入前面在GitHub上生成的Personal access tokens,Description隨便寫一些描述信息褒脯,如下圖:
填寫完畢后浇揩,點擊右側的”Test connection”按鈕,如果信息沒有填錯憨颠,顯示的內容如下圖所示:
點擊頁面最底部的”保存”按鈕;
Jenkins新建構建項目
- 選擇”Git”积锅;
- “Repository URL”輸入倉庫地址:https://github.com/ChenQianPing/Tdf.S01.git爽彤;
- “Credentials”創(chuàng)建一個Credentials,Kind選擇”Username with password”缚陷,Username輸入GitHub賬號适篙,Password輸入GitHub密碼;
- “源碼庫瀏覽器”選擇”githubweb”箫爷;
- “URL”輸入項目主頁:https://github.com/ChenQianPing/Tdf.S01嚷节;
- “構建觸發(fā)器”中勾選”GitHub hook trigger for GiTScm polling”;
- 構建環(huán)境設置虎锚,如下圖所示硫痰,勾選”Use secret text(s) or file(s)”,下面的”Credentials”選擇我們之前配置過的”Personal access tokens”
PowerShell篇
環(huán)境的準備
我選擇的PowerShell版本是V5.0窜护。網上搜索一下安裝包效斑,下載后在服務器安裝即可。作為安裝PowerShell的先決條件柱徙,.Net Framework 4.5也是必須安裝的缓屠。
Windows7默認自帶的Powershell版本只有2.0,但是很多基于Powershell開發(fā)的Module是需要更高版本的护侮,那么我們如何升級Powershell呢敌完?https://www.microsoft.com/en-us/download/details.aspx?id=54616核心功能一樣。支持windows 7 sp1
遠程訪問服務器羊初,首先是利用PowerShell的New-PSSession命令滨溉,填寫遠程服務器的管理員賬號密碼登陸,獲取Session
Get-Credential是個交互函數凳忙,執(zhí)行時會彈出一個用戶名密碼對話框业踏,因此我們要人為構造一個PSCredential。改寫一下方法
$pass=ConvertTo-SecureString -String 'your password' -AsPlainText -Force
$cre=New-Object pscredential('your username', $pass)
$session=New-PSSession -ComputerName $server -Credential $cre
獲取Session以后涧卵,遠程服務器已經完全落入我們的掌心∏诩遥現在可以通過Invoke-Command遠程執(zhí)行命令,可以用Enter-PSSession直接進入遠程會話柳恐,可以通過Copy-Item實現文件傳輸伐脖。以下是我在部署過程中遇到的常用的命令热幔。
遠程停止Windows Service:
Invoke-Command -Session $session -ScriptBlock{
Stop-Service -Name 'your service name'
}
遠程停止Web
Invoke-Command -Session $session -ScriptBlock{
import-module webadministration
set-location IIS:\
$site=Get-Item 'IIS:\Sites\Ems.Webv2'
$site.Stop()
}
本地文件拷貝
Copy-Item -Path "D:\\測試用文件.txt" -Destination "D:\\tt" -Force
拷貝本地文件到遠程服務器
ls "local folder" | cp -Destination "remote folder" -ToSession $session -Recurse -Force
ls "C:\tmp" | cp -Destination "D:\tt" -ToSession $session -Recurse -Force
將文件打包成zip
Compress-Archive "Folder" -DestinationPath "Zip File Name" -Force
Compress-Archive "D:\tt" -DestinationPath "D:\tt01.zip" -Force
將zip文件解壓
Expand-Archive "Zip File Name" -DestinationPath "Folder" -Force
Expand-Archive "D:\tt01.zip" -DestinationPath "D:\tt01" -Force
拷貝遠程服務器文件到本地
cp -FromSession $session -Path "Remote File" -Destination "Local Folder" -Recurse -Force
cp -FromSession $session -Path "D:\tt\測試用文件3.txt" -Destination "D:\tt01" -Recurse -Force
查看PowerShell版本信息
$PSVersionTable.PSVersion
釋放PSSession
Remove-PSSession -Id $session.Id #使用完畢后一定記得釋放PSSesion
Help
get-help Copy-Item -examples