Edit a file from Jenkins pipeline UI during pipeline execution

77 Views Asked by At

We have a requirement to allow edit a file during pipeline execution. The file need to be modified from Jenkins UI.

We know we can take variable inputs in pipeline stage. We need same way to edit a file from Jenkins UI before proceeding next stage.

2

There are 2 best solutions below

1
Prabhat Singh On

Well you can use the "input" step in your Jenkins pipeline script. This step pauses the pipeline and waits for human input before proceeding to the next stage.

pipeline {
    agent any
    
    stages {
        stage('Edit File') {
            steps {
                // Display a message indicating the need to edit the file
                echo 'Please edit the file below:'
                
                // Use the input step to wait for user input
                input message: 'Edit the file', ok: 'Proceed'
            }
        }
        stage('Next Stage') {
            steps {
                // Add steps for the next stage of the pipeline
            }
        }
    }
}

when the pipeline reaches the "Edit File" stage, it will pause and display a message in the Jenkins UI, instructing the user to edit the file. Once the user has made the necessary edits and clicks "Proceed" in the UI, the pipeline will continue to the next stage.

Note: You can further adapt whatever suits your specific requirements, such as specifying the file to be edited or providing additional instructions to the user. Additionally, you may want to add error handling or validation to ensure that the file is edited correctly before proceeding to the next stage of the pipeline.

0
ycr On

You can do something like below, but it ain't going to be pretty.

stage('Edit File') {
    
    def filePath = '/path/to/file.txt'
    def fileOrigContent = readFile(filePath)
    // Add the file content as the default value of the input field
    def userInput = input(id: 'fileContent', message: 'Edit the content below:',
                          parameters: [
                            string(defaultValue: fileOrigContent, description: 'File content', name: 'Filecontent')
                        ])
    
    writeFile(file: filePath, text: userInput, encoding: "UTF-8")
}