I'm trying to get Jenkins to deploy the latest code from github branch to my app server. When I push to my release branch, however, the latest commit code doesn't get deployed. Jenkins seems to deploy some older commit of mine. When I print the Branch_Name
and Git_Commit
I get the correct branch and commit id, so I'm not sure where I'm going wrong. Currently using a Jenkins 2.319.2 server.
The latest GitHub commit code gets pushed to the Jenkins Server WORKSPACE directory which looks like this C:\Program Files (x86)\Jenkins\workspace\PROJ_release. In the Workspace directory I confirmed that the latest commit changes are there. Somewhere between the Jenkins server and deploying to the app server seems to not get the latest commit changes.
Below is my Jenkins file:
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
""")
}
}
}
}
The issue was the line bat 'git checkout .'
was undoing the latest changes right before pushing to the app server. I removed the line of code and now the most recent commit code is being pushed successfully.