Search code examples
jenkinsgitlabjenkins-pipelinecicd

Converting .gitlab-ci.yml file syntax into Jenkins.yaml


I used gitlab mostly but I have requirement to run the GitLab CICD pipeline into Jenkins CICD Tool. Considering I am newbie to Jenkins but experienced in Gitlab.

Pls let me know the conversion of my current .gitlab-ci.yml into jenkins.yaml file.

Current .gitlab-ci.yml file:

before_script:
    - apt-get update -qq
    - apt-get install -qq git zip
test:                              
  environment: test
  script:
    - chmod 400 $SSH_KEY
    - apt-get update -qq
    - apt-get install -qq git zip
    - zip -r xyz.zip $CI_PROJECT_DIR 
    # - ssh -i $SSH_KEY -o stricthostkeychecking=no hello@<hostname> "pwd ; rm -rf xyz xyz.zip"
    - scp -o stricthostkeychecking=no -i $SSH_KEY xyz.zip hello@<hostname>:/home/<linux_user>/
    - ssh -i $SSH_KEY -o stricthostkeychecking=no hello@<hostname> "pwd;rm -rf xyz; unzip xyz.zip -d xyz; chmod 700 -R xyz; rm -rf xyz.zip"
  artifacts:
    paths:
      - xyz.zip
  only:
    changes:
      - abc/**/*
    refs:
        - test

Converted Jenkins Pipeline syntax:

pipeline {
  agent any
  environment {
    TEST = "test"
  }
  stages {
    stage("Test") {
      steps {
        sh "apt-get update -qq"
        sh "apt-get install -qq git zip"
        sh "chmod 400 ${SSH_KEY}"
        sh "apt-get update -qq"
        sh "apt-get install -qq git zip"
        sh "zip -r xyz.zip ${CI_PROJECT_DIR}"
        sh "scp -o stricthostkeychecking=no -i ${SSH_KEY} xyz.zip hello@<hostname>:/home/<linux_user>/"
        sh "ssh -i ${SSH_KEY} -o stricthostkeychecking=no hello@<hostname> 'pwd;rm -rf xyz; unzip xyz.zip -d xyz; chmod 700 -R xyz; rm -rf xyz.zip'"
      }
      post {
        always {
          archiveArtifacts artifacts: 'xyz.zip', onlyIfSuccessful: true
        }
      }
    }
  }
  options {
    skipDefaultCheckout()
  }
  triggers {
    changeset {
      pathFilters("abc/**/*")
    }
    branch 'test'
  }
}

Pls check and validate jenkins syntax (currently no access to jenkins instance) and will Jenkins above pipeline work in same way as it was working in gitlab.


Solution

  • This isn't too difficult. All the sh commands you run can be run with Jenkins by enclosing them with sh. For example:

    sh "chmod 400 $SSH_KEY"
    sh "apt-get update -qq"
    

    or a multi-line sh:

    sh '''
      chmod 400 $SSH_KEY
      apt-get update -qq
      apt-get install -qq git zip
      zip -r xyz.zip $CI_PROJECT_DIR 
      ...
    '''
    

    A major difference between Jenkins and GitLab is that Jenkins has a huge ecosystem of plugins do things in a quicker and more efficient way. So it's best to consider if any of them might be useful to you.

    The hardest part is setting up the triggers. While GitLab has an easy automatic trigger, Jenkins isn't designed with GitLab in mind, meaning that you will have to use the GitLab plugin to make the pipeline trigger. Take a look at the triggers section of the documentation.