This is the 8th day of my participation in the Novembermore Challenge

Transfer from Jenkins to Azure Pipelines

Jenkins is an open source automation server that has traditionally been installed by enterprises in their own data centers and managed locally. Many providers also offer managed Jenkins hosting, or Azure Pipelines is a cloud native continuous integration pipeline that provides the management of multi-stage Pipelines and builds agent VMS hosted in the cloud.

Jenkins has two styles of writing Jenkinsfils, declarative and scripted. Scripting was previously written in Groovy, but the updated version also supports declarative in order to support pipeline.

According to Microsoft documentation we can easily turn declarative Jenkinsfiles into YAML files that ADO- Pipeline can understand

Such as

pipeline {
    agent none
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
                sh 'npm run build'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
    }
}
Copy the code

You can replace it with a YAML file like this

The original stage->job command executed ->script

jobs:
- job: Build
  steps:
  - script: npm install
  - script: npm run build
- job: Test
  steps:
  - script: npm test
Copy the code

Container-based builds

Using containers in a build pipeline allows you to build and test in a Docker image that has the exact dependencies required by the pipeline and has been configured. It frees you from having to include build steps to install more software or configure the environment. Both Jenkins and Azure Pipelines support container-based build.

For example, to run the build in Ubuntu 14.04 (” Trusty “) container, and then run the tests in Ubuntu 16.04 (” Xenial “) container:

Jenkinsfile

Copy

pipeline {
    agent none
    stages {
        stage('Build') {
            agent {
                docker {
                    image 'ubuntu:trusty'
                    args '-v $HOME:/build -w /build'
                }
            }
            steps {
                sh 'make'
            }
        }
        stage('Test') {
            agent {
                docker {
                    image 'ubuntu:xenial'
                    args '-v $HOME:/build -w /build'
                }
            }
            steps {
                sh 'make test'
            }
        }
    }
}
Copy the code

Converted to azure – pipelines. Yml

resources:
  containers:
  - container: trusty
    image: ubuntu:trusty
  - container: xenial
    image: ubuntu:xenial
​
jobs:
- job: build
  container: trusty
  steps:
  - script: make
- job: test
  dependsOn: build
  container: xenial
  steps:
  - script: make test
Copy the code

The agent to choose

Jenkins provides build agent options using the Agent option to ensure that your build pipeline — or a particular phase of the pipeline — is running on a particular build agent machine. Similarly, Azure Pipelines provides a number of options to configure where build environments run.

Escrow agent selection

Azure Pipelines provides cloud hosted build agents for Linux, Windows and macOS builds. To select a build environment, you can use the vmImage keyword. For example, to select the macOS version:

pool:
  vmimage: macOS-latest
Copy the code

Also specify a Container and a Docker image for more fine-grained control over how the build runs

Local Proxy selection

If you host the build agent locally, you can build the agent “functionality” based on the architecture of the machine or the software definition installed on it. For example, if you have set up a local build agent with these Java features, you can use the DEMANDS keyword to ensure that your jobs are running on it:

YAML copy

pool:
  demands: java
Copy the code

The environment variable

In Jenkins, you typically define environment variables for the entire pipeline. For example, to set two environment variables, CONFIGURATION=debug and PLATFORM=x86:

pipeline {
    environment {
        CONFIGURATION = 'debug'
        PLATFORM      = 'x64'
    }
}
Copy the code

Similarly, in Azure Pipelines, configure variables used in YAML configuration and set as environment variables during job execution:

variables:
  configuration: debug
  platform: x64
Copy the code

azure-pipelines.yml

jobs:
- job: debug build
  variables:
    configuration: debug
  steps:
  - script: ./build.sh $(configuration)
- job: release build
  variables:
    configuration: release
  steps:
  - script: ./build.sh $(configuration)
Copy the code

Handle success and failure

Jenkins allows you to run commands using the POST pipe section after the build is complete. You can specify commands to run when the build succeeds (using the SUCCESS section), when the build fails (using the Failure section), or always (using the Always section). Such as:

post { always { echo "The build has finished" } success { echo "The build succeeded" } failure { echo "The build failed" }}Copy the code

Similarly, Azure Pipelines has a rich conditional execution framework that allows you to run jobs or job steps based on many conditions, including pipeline success or failure.

To simulate Jenkins post-build conditions, you can define jobs that run based on always(), Succeeded (), or failed() conditions:

azure-pipelines.yml

jobs:
- job: always
  steps:
  - script: echo "The build has finished"
    condition: always()
​
- job: success
  steps:
  - script: echo "The build succeeded"
    condition: succeeded()
​
- job: failed
  steps:
  - script: echo "The build failed"
    condition: failed()
Copy the code