首页 > 解决方案 > 将 jenkinsfile 转换为 gitlabci.yaml

问题描述

我有一个复杂的 jenkinsfile groovy 脚本,我想将其转换为 gitlabCI yaml 脚本。


pipeline {
    agent {
        docker {
            image "my_repo:master"
            registryUrl "my_ECR_Img"
            registryCredentialsId "my_registry_CredentialsId"
            label "jenkins-infrastructure"
            alwaysPull true
        }
    }

    parameters {
        gitParameter name: 'COMMIT_HASH',
                     type: 'PT_REVISION',
                     defaultValue: 'master',
                     branch: 'origin/master',
                     useRepository: ".*infra.git"
        choice(name: 'TARGET_STAGE', choices: ['dev', 'staging', 'prod', 'test'], description: 'Target stage for commit deployment')
        booleanParam(name: 'SKIP_PLAN', defaultValue: "false", description: 'Skip validate/plan step (e.g. when retrying a failed pipeline)')
        choice(name: 'MODULE', choices: ['all','module1','module2','module3'], description: 'which module to apply - select all for all modules')
    }

    environment {
        AWS_REGION = 'eu-central-1'
        COMMIT_HASH = sh(
            script: "printf \$(git describe --always)",
            returnStdout: true
        )
        PROJECT = "eve"
        REPOSITORY_NAME = "infra"
        AUTOMATED_BUILD = "${params.COMMIT_HASH == "master" ? "TRUE" : "FALSE"}"
        AWS_ENVIRONMENT = "${params.COMMIT_HASH == "master" ? "dev" : "${params.TARGET_STAGE}"}"
        MODULE = "${params.COMMIT_HASH == "master" ? "all" : "${params.MODULE}"}"
    }

    stages {
        stage('automated pipeline') {
            when {
                allOf {
                    anyOf {
                        changeRequest()
                        branch 'master'
                    }
                    environment name: 'AUTOMATED_BUILD', value: 'TRUE'
                }
            }

            stages {
                stage('pull request builds') {
                    when {
                        changeRequest()
                    }

                    steps {
                        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'JENKINS_AWS'],
                        sshUserPrivateKey(credentialsId: 'JENKINS-SSH', keyFileVariable: 'SSH_KEY')]) {
                            script  {
                                terraformPlan(environment: 'dev', lock: false, tfsec: true)
                            }
                        }
                    }
                    post {
                      always {
                          junit allowEmptyResults: true, testResults: "**/*/junit.xml"
                      }
                      success {
                        echo "Terraform TFSec Passed"
                      }
                      failure {
                        echo "Terraform TFSec Failed"
                      }
                    }
                }

下面还有一些其他代码,但我只想翻译这个块!我浏览了 gitlabCI 文档,但问题是,这不仅仅是关于翻译,而是在将管道从 jenkins 迁移到 gitlab 时使用不同的方法。任何帮助将不胜感激。

标签: jenkinsgitlabjenkins-pipelinedevops

解决方案


推荐阅读