将.gitlab-ci.yml文件语法转换为Jenkins.yaml

6jygbczu  于 2023-03-07  发布在  Jenkins
关注(0)|答案(1)|浏览(164)

我大部分时间使用Gitlab,但我需要将Gitlab的CICD管道运行到Jenkins的CICD工具中。考虑到我是Jenkins的新手,但在Gitlab中经验丰富。
请让我知道我目前的.gitlab-ci.yml到jenkins.yaml文件的转换。
当前.gitlab-ci.yml文件:

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

转换后的Jenkins Pipeline语法:

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'
  }
}

请检查并验证jenkins语法(目前无法访问jenkins示例),并将Jenkins以上管道的工作方式与它在gitlab中的工作方式相同。

ss2ws0br

ss2ws0br1#

这并不太难,你运行的所有sh命令都可以用sh括起来,然后用Jenkins来运行。

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

或者多线SH:

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

Jenkins和GitLab最大的区别在于Jenkins拥有庞大的plugins生态系统,可以更快更高效地完成任务,所以最好考虑一下其中是否有对你有用的。
最困难的部分是设置触发器。虽然GitLab有一个简单的自动触发器,但Jenkins在设计时并没有考虑到GitLab,这意味着你必须使用GitLab plugin来制作管道触发器。

相关问题