我试图让Jenkins将github分支中的最新代码部署到我的应用服务器上。然而,当我推到发布分支时,最新的提交代码并没有得到部署。Jenkins似乎部署了我的一些旧提交。当我打印Branch_Name
和Git_Commit
时,我得到了正确的分支和提交ID。所以我不知道我错在哪里了。目前使用的是Jenkins 2.319.2服务器。
最新的GitHub提交代码被推送到Jenkins服务器的WORKSPACE目录,看起来像C:\Program Files(x86)\Jenkins\workspace\PROJ_release。在Workspace目录中,我确认了最新的提交更改都在那里。Jenkins服务器和部署到应用服务器之间的某个地方似乎没有得到最新的提交更改。
下面是我的Jenkins文件:
def url = ""
def branch = env.BRANCH_NAME
def msdeploy = 'C:\\Program Files (x86)\\IIS\\Microsoft Web Deploy V3'
if (branch == "master"|| branch == "release") {
node {
// get dontnet.exe reference
environment {
dotnet = 'C:\\Program Files\\dotnet\\sdk\\2.1.803\\dotnet.exe'
version = "${env.BUILD_NUMBER}"
}
// use nodejs 10.0.0 configured in global tools
env.NODEJS_HOME = "${tool 'NodeJS10.0.0'}"
env.PATH="${env.NODEJS_HOME};${env.PATH};"
stage("Clean Workspace"){
cleanWs()
}
stage('Pre Check'){
env.GIT_COMMIT = checkout(scm).GIT_COMMIT
env.type = ""
// Only allow push to dev(master) and test(release)
if (branch == "master") {
url = 'mymasterurl'
echo("Preceding with CI options for dev")
env.type = "Dev"
} else if (branch == "release"){
url = 'myreleaseurl'
echo("Preceding with CI options for highside prod")
env.type = "Release"
}
}
stage('Checkout/Clone Repository') {
checkout scm
}
stage('Restore'){
bat '%dotnet% dotnet restore PROJ.csproj'
}
stage('Build/Deploy'){
withCredentials([usernamePassword(credentialsId: 'WEBDEPLOY_SRV_ACCOUNT', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD'),
string(credentialsId:'PROJ_AD_PASSWORD',variable: 'AD_PASSWORD')]) {
def randomKey = UUID.randomUUID()
.toString()
.toUpperCase()
def input = readJSON file: 'appsettings.json'
//add Secrets
input['TOKEN']['SECURITYKEY'] = randomKey
input['AD_SERVICE_ACCOUNT']['USERNAME'] = env.USERNAME
input['AD_SERVICE_ACCOUNT']['PASSWORD'] = env.PASSWORD
echo "app settings: ${input}"
// add build info to environment ts file for front end
powershell ("""
(Get-Content .\\ClientApp\\environments\\environment.prod.ts).replace('%BUILD%', "${env.BUILD_NUMBER}") | Set-Content .\\ClientApp\\environments\\environment.prod.ts
(Get-Content .\\ClientApp\\environments\\environment.prod.ts).replace('%COMMIT%', "${env.GIT_COMMIT}") | Set-Content .\\ClientApp\\environments\\environment.prod.ts
(Get-Content .\\ClientApp\\environments\\environment.prod.ts).replace('%DATE%', "${BUILD_TIMESTAMP}") | Set-Content .\\ClientApp\\environments\\environment.prod.ts
""")
echo "CURRENT WORKSPACE : ${WORKSPACE}"
// write json pretty print it, indented with a configurable number of spaces
writeJSON file: 'appsettings.json', json: input, pretty: 4
def input2 = readJSON file: 'appsettings.json'
echo "app settings after: ${input2}"
// build the package to deploy to server
bat '%dotnet% dotnet msbuild PROJ.csproj /verbosity:diag /P:CreatePackageOnPublish=True /P:Configuration=Release /P:VersionAssembly=%BUILD_NUMBER% /P:DeployOnBuild=true /P:PublishProfile="WebPackage" /P:AllowUntrustedCertificate=true /p:Username=%USERNAME% /p:Password=%PASSWORD%'
// remove all the changes to avoujd git conflicts
bat 'git status'
bat 'git checkout .'
echo "Removed the pending changes"
bat 'git status'
// now deploy to server
bat ("""
cd \"${msdeploy}\"
msdeploy.exe -verb:sync -source:package="%WORKSPACE%\\PROJ.zip" -dest:auto,ComputerName=${url}:8172/msdeploy.axd,authtype='Basic',username=%USERNAME%,password=%PASSWORD% -allowUntrusted -enableRule:DoNotDeleteRule -enableRule:AppOffline
""")
}
}
}
}
2条答案
按热度按时间ctehm74n1#
问题是
bat 'git checkout .'
行在推送到应用服务器之前正在撤消最新的更改。我删除了这行代码,现在最近的提交代码被成功推送。sr4lhrrt2#
此问题可能需要更多信息才能解决。
我看到您还回显了部署命令中使用的WORKSPACE。这是否与构建过程实际构建zip文件的位置相匹配?PROJ.zip该目录中的www.example.com是否可写?
WORKSPACE在仓库的工作目录中吗?我看到在部署命令前有一个
git checkout .
,所以如果WORKSPACE在git工作目录中,那么在部署前你就从分支中提取了内容。它可能需要项目文件中的一些值,使用中的变量的值(当然不是凭据的值),以及比我更熟悉Microsoft工具链的人来苏斯这一点。