6

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
                    """)
               }
      }
   }
}
TarHalda
  • 1,050
  • 1
  • 9
  • 27
eia92
  • 81
  • 2
  • 12
  • You verified the checkout and build uses the correct information. I assume you also verified the `PROJ.zip` contents that you attempt to deploy. So the deploy seems to fail or act in an unexpected way. What is the jobs output on calling msdeploy? If it is non telling, try increasing the log level verbosity. Does `DoNotDeleteRule` prevent deletion, or also replacing of existing files? – Kissaki Oct 08 '22 at 09:09

2 Answers2

3

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.

eia92
  • 81
  • 2
  • 12
2

This problem may require more information to solve.

I see you also echo WORKSPACE which is used in your deployment command. Does that match where the zip file is actually being built by the build process? Is PROJ.zip in that directory writeable?

Is WORKSPACE within your repository's working directory? I see there's a git checkout . before your deployment command, so if WORKSPACE is within the git working directory you're pulling over content from the branch before deployment.

It may take some of the values from the project file, the values of the variables in use (NOT credentials ones of course), and someone a little more familiar with the Microsoft toolchain than I to suss this out.

  • Yes the Workspace matches where the zip file is being built. And yes the PROJ.zip is in a writeable directory. The Workspace is in the jenkins server directory. It looks something 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 – eia92 Oct 06 '22 at 19:09
  • @MickyD if you can answer that question fully with the information provided, please do so. Absolutely nobody else has stepped forward to help the OP. – Christopher E. Stith Oct 07 '22 at 15:21
  • "Low quality answer" but please see the accepted answer is what I said in the third paragraph. – Christopher E. Stith Oct 11 '22 at 17:38