Home - hemanth22/pipeline-studio GitHub Wiki

Welcome to the pipeline-studio wiki!

pipeline {
    agent any

    environment {
        SOURCE_BRANCH_NAME = "${params.SOURCE_BRANCH_NAME}"
        RELEASE_BRANCH_NAME = "${params.RELEASE_BRANCH_NAME}"
        GITHUB_SOURCE_URL = "${params.GITHUB_SOURCE_URL}"
        ANSIBLE_PLAYBOOK = 'deploy.playbook'
        SUDO_PASSWORD = 'hemanth'
    }

    stages {
        stage("Initialization of Jenkins") {
            steps {
                sh '''
                echo "Initial step"
                ls
                ls $WORKSPACE/
                '''
                echo "${GITHUB_SOURCE_URL}"
                echo "${RELEASE_BRANCH_NAME}"
            }
        }
        
        stage('Checkout SCM') {
            steps {
                script {
                    // Custom SCM configuration using GitSCM class with dynamic branch name
                    echo "Checking out the source code"
                    def scmConfig = [$class: 'GitSCM',
                        branches: [name: "${RELEASE_BRANCH_NAME}"](/hemanth22/pipeline-studio/wiki/name:-"${RELEASE_BRANCH_NAME}"),
                        doGenerateSubmoduleConfigurations: false,
                        userRemoteConfigs: [[
                            url: ""+GITHUB_SOURCE_URL+"",  // Replace with your Git repository URL
                            credentialsId: '16aac721-d9ed-49c5-94e3-8e573e1c5f8e'  // Replace with your credentials ID if necessary
                        ]]
                    ]
                    def scmData = checkout(scmConfig)

                    // Get the commit date of the last commit
                    GIT_COMMIT_DATE = sh(returnStdout: true, script: 'git log -1 --format=%cd --date=iso').trim()

                    // Extracting and echoing Git branch, commit, and URL
                    echo "scmData.GIT_BRANCH=${scmData.GIT_BRANCH}"
                    echo "scmData.GIT_COMMIT=${scmData.GIT_COMMIT}"
                    echo "scmData.GIT_URL=${scmData.GIT_URL}"
                    commitId=sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
                    version=env.BUILD_NUMBER+'_'+commitId
                    println version
                    checkout([
                      $class: 'GitSCM',
                      branches: [name: 'main'](/hemanth22/pipeline-studio/wiki/name:-'main'),  // Specify the branch to check out
                      extensions: [$class: 'RelativeTargetDirectory', relativeTargetDir: 'pipeline'](/hemanth22/pipeline-studio/wiki/$class:-'RelativeTargetDirectory',-relativeTargetDir:-'pipeline'),  // Checkout into 'pipeline' directory
                      userRemoteConfigs: [[credentialsId: '16aac721-d9ed-49c5-94e3-8e573e1c5f8e',  // Jenkins credentials ID
                      url: "https://github.com/hemanth22/pipeline-studio.git" ]]
                    ])
                }
            }
        }

        stage('Deploy') {
            steps {
                echo "Deploying project. Last commit date: ${GIT_COMMIT_DATE}"
                // Insert your deployment steps here
                sh '''
                ls $WORKSPACE/
                echo "================================================="
                ls $WORKSPACE/pipeline/BIT
                echo "================================================="
                cp -v $WORKSPACE/pipeline/BIT/deploy.playbook $WORKSPACE
                echo "================================================="
                ls $WORKSPACE/
                echo "================================================="
                '''
                runAnsiblePlaybook(ANSIBLE_PLAYBOOK,SUDO_PASSWORD)
            }
        }
    }

    post {
        always {
            echo "Pipeline completed. Last commit date was: ${GIT_COMMIT_DATE}"

            // Clean up the workspace
            deleteDir()
        }
    }
}
// Define the function outside the pipeline
def runAnsiblePlaybook(playbook,password) {
    sh """
        ansible-playbook -vvvvv ${playbook} -e "ansible_become_pass=${password}"

    """
}