1

I am pretty new to jenkins and trying to figure out the best way to accomplish rollbacks. Currently this is my pipeline:

  1. Obtain a webhook from beanstalk (git versioning)
  2. Build the project
  3. Obtain the build artifact and deploy to Azure

This is working great - the last thing I would like to solve is being able to rollback the project to a specific build.

When I do a "replay" however, it doesn't grab the actual commit the build ran on - it grabs the current commit in the repo. So it doesn't build the project from the commit it originally ran on - but the latest commit in the repo.

pipeline {
agent any
stages {

    stage('Install Dependenciess') {
        steps {
            bat'npm install'
        }
    }

    stage('Build the Project') {
        steps {
            bat'npm run build'
        }
    }

    stage('Deploy to Azure Prod') {
        when {
            environment name: 'REPOSITORY_BRANCH', value: 'master'
            beforeAgent true
        }
        steps {

            timeout(time:5, unit: 'DAYS') {
                input message: 'Approve Deployment?'
            }

            azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
            resourceGroup: env.RES_GROUP, 
            appName: env.WEB_APP, 
            filePath: "**/*.*",
            sourceDirectory: "build"
        }
    }

    stage('Deploy to Azure Development') {
        when {
            environment name: 'REPOSITORY_BRANCH', value: 'development'
            beforeAgent true
        }
        steps {

            azureWebAppPublish azureCredentialsId: env.AZURE_CRED_ID,
            resourceGroup: env.RES_GROUP, 
            appName: env.WEB_APP, 
            filePath: "**/*.*",
            sourceDirectory: "build"
        }
    }

}

post {
    always {
        archiveArtifacts artifacts: "build/**/*.*", onlyIfSuccessful: true
        cleanWs()
    }

    success {
        mail bcc: '', body: "<b>Example</b><br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "";
    }

}

}

Justin H
  • 13
  • 1
  • 4
  • 1
    do you store the built artifacts somewhere? – Yuri G. Jul 12 '19 at 22:02
  • @YuriG. I keep the last 10 deployments. So if I open an old build I can see the build files for that particular run.

    I looked into storing builds over in Azure Blob - but if I have to rollback I am unsure how I would set up a process to go grab those old artifacts to re-deploy to Azure. Bear in mind - our company will have dozens of projects running on this same build process.

    – Justin H Jul 12 '19 at 22:13
  • If I understand correctly, right now, the same Jenkins job builds the project and deploys it. Am I right? – Yuri G. Jul 12 '19 at 22:28
  • @YuriG. - Yep that is correct. I will post my build steps above – Justin H Jul 12 '19 at 22:43

1 Answers1

1

As a short term solution you can replay you last good build. Read the docs for more information

Update: There is a bug , so the proposed way will work only in multibranch pipeline. So as a short term solution you can convert your job to a multibranch pipeline and to use a replay when it’s needed

As a long term solution I would separate build and deployment job and store the build artifact in private npm registry. In that way you can always pick whatever version is needed to deploy

Yuri G.
  • 179
  • 4
  • Hey Yuri - this is what I tried originally - but ran into this issue "When I do a "replay" however, it doesn't grab the actual commit the build ran on - it grabs the current commit in the repo. So it doesn't build the project from the commit it originally ran on - but the latest commit in the repo." – Justin H Jul 12 '19 at 23:04
  • are you sure you are talking about replay and not rebuild? it's 2 different features – Yuri G. Jul 12 '19 at 23:06
  • Looks like you’re right https://issues.jenkins-ci.org/plugins/servlet/mobile#issue/JENKINS-36453 – Yuri G. Jul 12 '19 at 23:57
  • Yeah, I was just testing this out again to make sure. The proper way to do this though Yuri - I should have a build pipeline that stores artifacts over in Azure (say Azure blob) - and then somehow store a reference to that build in the event I need to redeploy older code? Any idea where a good tutorial on sometihng like that would be? Thanks for the help. – Justin H Jul 13 '19 at 00:01
  • Have a seen this https://wiki.jenkins.io/plugins/servlet/mobile?contentId=66848409#content/view/66848409 – Yuri G. Jul 13 '19 at 00:16
  • I like this idea and using "tags" to deploy a specific artefact - separating CI and CD. A solution could be to tag git after passing CI. The multi-branch plugin can then detect there is a new tag and trigger a new set of stages. – Robert Jul 13 '19 at 11:56