How can I update jenkins plugins from the terminal?

后端 未结 6 1915
庸人自扰
庸人自扰 2021-01-30 11:07

I am trying to create a bash script for setting up Jenkins. Is there any way to update a plugin list from the Jenkins terminal?

At first setup there is no plugin availab

6条回答
  •  旧时难觅i
    2021-01-30 11:59

    With a current Jenkins Version, the CLI can just be used via SSH. This has to be enabled in the "Global Security Settings" page in the administration interface, as described in the docs. Furthermore, the user who should trigger the updates must add its public ssh key.

    With the modified shell script from the accepted answer, this can be automatized as follows, you just have to replace HOSTNAME and USERNAME:

    #!/bin/bash
    
    jenkins_host=HOSTNAME #e.g. jenkins.example.com
    jenkins_user=USERNAME
    
    jenkins_port=$(curl -s --head https://$jenkins_host/login | grep -oP "^X-SSH-Endpoint: $jenkins_host:\K[0-9]{4,5}")
    
    function jenkins_cli {
        ssh -o StrictHostKeyChecking=no -l "$jenkins_user" -p $jenkins_port "$jenkins_host" "$@"
    }
    
    UPDATE_LIST=$( jenkins_cli list-plugins | grep -e ')$' | awk '{ print $1 }' ); 
    
    if [ ! -z "${UPDATE_LIST}" ]; then 
        echo Updating Jenkins Plugins: ${UPDATE_LIST}; 
        jenkins_cli install-plugin ${UPDATE_LIST};
        jenkins_cli safe-restart;
    else
        echo "No updates available"
    fi
    

    This greps the used SSH port of the Jenkins CLI and then connects via SSH without checking the host key, as it changes for every Jenkins restart.

    Then all plugins with an update available are upgraded and afterwards Jenkins is restarted.

提交回复
热议问题