C#客户端项目需要实现自动化部署,代码提交到git之后进行自动化编译并部署到FTP服务器上,同时对在数据库表中进行版本更新
流程:
1.使用Jenkins将git上的项目下载到指定地址
2.使用nuget还原项目中下载的插件
3.使用MSBuild对项目文件进行编译
4.使用PowerShell将Release包按照FTP服务器的格式进行压缩打包
5.在PowerShell下使用Posh-SSH连接FTP服务器并将压缩好的文件上传
6.使用PowerShell连接数据库并修改项目表中的版本号
1.安装jdk,配置环境变量;jdk华为云各版本高速下载通道:JDK
2.安装jenkins,下载地址:Jenkins
msi直接运行默认安装即可,默认端口号是8080,如已占用可自行替换
war启动方式 java -jar jenkins.war --httpPort=8080,(linux环境、Windows环境都一样);
3.打开浏览器进入链接 http://127.0.0.1:8080
4.填写初始密码,激活系统 (cmd管理员权限:type …/jenkins/secrets/initialAdminPassword)
1.生成ssh(),登陆Github, 添加SSH Keys:将 id_rsa.pub文件内容添加上去
2.Jenkins配置ssh,将id_rsa文件内容配置到到Private Key
3.拉取git仓库代码,要选择ssh凭证
1.配置msbuild
2.还原并生成
3.生成的相关命令可自行添加可以根据命令行查找 msbuild -h 或者去官网查看MSBuild 命令行参考
可以使用jenkins自带的ftp进行配置,但是涉及到给现有文件进行删除分类创建新的文件夹压缩等无法实现,因此使用powershell脚本实现
相关命令: 获取文件版本号:(get-item -path (../文件.exe")).versioninfo.ProductVersion 新建文件夹:New-Item -Path $Local_Version -ItemType Directory 压缩文件:add-type -a 'system.io.compression.filesystem';[io.compression.zipfile]::createfromdirectory(初始目录,目标目录.zip) Copy文件:Copy-Item ($Local_RootDirectory + 'ReleaseUpdate.exe') -Destination $Local_Update -Force 删除文件:Remove-Item -Path ($Local_RootDirectory + 'ReleaseUpdate.exe')在PowerShell下使用Posh-SSH连接FTP服务器并将压缩好的文件上传
#上传FTP
# $server=""
# $port=""
# $username = ""
# $pass = ""
# $password = $pass | convertto-securestring -asplaintext -force
# $credentials = new-object system.management.automation.pscredential($username, $password)
# $session = new-sftpsession -computername $server -port $port -credential $credentials -Force
# Set-SFTPItem -Destination $Remote_ -Path $Local_ -SFTPSession $session -Force
# Set-SFTPItem -Destination $Remote_ -Path $Local_ -SFTPSession $session -Force
# Set-SFTPItem -Destination $Remote_Version -Path $Local_Version -SFTPSession $session -Force
# # if(!$(Test-SFTPPath -SFTPSession $session -Path $Remote_Version))
# # {
# # New-SFTPItem -SFTPSession $session -Path $Remote_Version -ItemType Directory
# # }
# Remove-SFTPSession -SFTPSession $session
使用PowerShell连接数据库并修改项目表中的内容
#更新数据库 # $assemblyfile = "d:1softwareoracleoracle.manageddataaccess.dll" # [reflection.assembly]::loadfile($assemblyfile) Or # Add-Type -Path "D:1softwareoracleOracle.ManagedDataAccess.dll" # $username = "" # $password = "" # $datasource = "" # $sql = "" # $connectionnectionString = 'User Id=' + $username + ';Password=' + $password + ';Data Source=' + $datasource # $connectionnection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionnectionString) # $connectionnection.open() # $command=$connectionnection.CreateCommand() # $command.CommandText=$sql # $reader=$command.ExecuteNonQuery() # $connectionnection.close()
对于使用Jenkins拉取git msbuild生成项目也都可以使用powershell完成:
#先改变PowerShell策略,允许运行签名脚本 #Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force #Install-Module PowerShellGet -Force -SkipPublisherCheck #Install-Module posh-git -Scope CurrentUser -Force #生成ssh公钥私钥 #拉取git $DefaultFile="D:gits" cd $DefaultFile git init git remote add origin git@gitlab.cicconline.com:odts1/client-odts.git git fetch origin ODTS_Dev git pull origin ODTS_Dev #还原nuget $NuGetExe="D:1softwarenugetnuget.exe" $SolutionFileSln=$DefaultFile+"aa.sln" &$NuGetExe restore $SolutionFileSln #编译 $MSBuildExe = "C:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuild15.0BinMSBuild.exe" $SolutionFile = $DefaultFile+ "aa.csproj" &$MSBuildExe $SolutionFile "/t:Build" "/p:Configuration=Release;SolutionDir=$DefaultFile;DeployOnBuild=true;VisualStudioVersion=17.0"



