如何从终端更新 jenkins 插件?

发布于 2024-12-09 03:34:47 字数 209 浏览 0 评论 0原文

我正在尝试创建一个 bash 脚本来设置 Jenkins。有没有办法从 Jenkins 终端更新插件列表?

第一次设置时,列表中没有可用的插件,

即:

java -jar jenkins-cli.jar -s `http://localhost:8080` install-plugin dry

无法工作

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 available on the list

i.e.:

java -jar jenkins-cli.jar -s `http://localhost:8080` install-plugin dry

won't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

终陌 2024-12-16 03:34:47

一个简单但有效的方法是首先列出所有已安装的插件,查找更新并安装它们。

java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins

每个有可用更新的插件都会在末尾的括号中显示新版本。因此,您可以 grep 查找这些:

java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins | grep -e ')$'| awk '{ print $1 }'

如果您使用插件名称调用 install-plugin,它会自动升级到最新版本。

最后你必须重新启动詹金斯。

将它们放在一起(可以放在 shell 脚本中):

UPDATE_LIST=$( java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins | grep -e ')
 | awk '{ print $1 }' ); 
if [ ! -z "${UPDATE_LIST}" ]; then 
    echo Updating Jenkins Plugins: ${UPDATE_LIST}; 
    java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ install-plugin ${UPDATE_LIST};
    java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ safe-restart;
fi

A simple but working way is first to list all installed plugins, look for updates and install them.

java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins

Each plugin which has an update available, has the new version in brackets at the end. So you can grep for those:

java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins | grep -e ')$' | awk '{ print $1 }'

If you call install-plugin with the plugin name, it is automatically upgraded to the latest version.

Finally you have to restart jenkins.

Putting it all together (can be placed in a shell script):

UPDATE_LIST=$( java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ list-plugins | grep -e ')
 | awk '{ print $1 }' ); 
if [ ! -z "${UPDATE_LIST}" ]; then 
    echo Updating Jenkins Plugins: ${UPDATE_LIST}; 
    java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ install-plugin ${UPDATE_LIST};
    java -jar /root/jenkins-cli.jar -s http://127.0.0.1:8080/ safe-restart;
fi
邮友 2024-12-16 03:34:47

您实际上可以从计算机终端(而不是 Jenkins 终端)安装插件。

  1. 从插件网站 (http://updates.jenkins-ci.org/download/plugins) 下载插件,
  2. 将该插件复制到 $JENKINS_HOME/plugins 目录
  3. 中点启动 Jenkins 或调用重新加载设置服务 (http://yourservername:8080/jenkins/reload)

这将在 Jenkins 中启用插件并假设 Jenkins 已启动。

cd $JENKINS_HOME/plugins
curl -O http://updates.jenkins-ci.org/download/plugins/cobertura.hpi
curl http://yourservername:8080/reload

You can actually install plugins from the computer terminal (rather than the Jenkins terminal).

  1. Download the plugin from the plugin site (http://updates.jenkins-ci.org/download/plugins)
  2. Copy that plugin into the $JENKINS_HOME/plugins directory
  3. At that point either start Jenkins or call the reload settings service (http://yourservername:8080/jenkins/reload)

This will enable the plugin in Jenkins and assuming that Jenkins is started.

cd $JENKINS_HOME/plugins
curl -O http://updates.jenkins-ci.org/download/plugins/cobertura.hpi
curl http://yourservername:8080/reload
甜`诱少女 2024-12-16 03:34:47

以下是如何使用 Ansible 部署 Jenkins CI 插件,当然这是从终端使用的。此代码是 roles/jenkins_ci/tasks/main.yaml 的一部分:

- name: Plugins
  with_items:                             # PLUGIN NAME
  - name: checkstyle                      # Checkstyle
  - name: dashboard-view                  # Dashboard View
  - name: dependency-check-jenkins-plugin # OWASP Dependency Check
  - name: depgraph-view                   # Dependency Graph View
  - name: deploy                          # Deploy
  - name: emotional-jenkins-plugin        # Emotional Jenkins
  - name: monitoring                      # Monitoring
  - name: publish-over-ssh                # Publish Over SSH
  - name: shelve-project-plugin           # Shelve Project
  - name: token-macro                     # Token Macro
  - name: zapper                          # OWASP Zed Attack Proxy (ZAP)
  sudo: yes
  get_url: dest="{{ jenkins_home }}/plugins/{{ item.name | mandatory }}.jpi"
           url="https://updates.jenkins-ci.org/latest/{{ item.name }}.hpi"
           owner=jenkins group=jenkins mode=0644
  notify: Restart Jenkins

这是更完整示例的一部分,您可以在以下位置找到该示例:
https://github.com/sakaal/service_platform_ansible/ blob/master/roles/jenkins_ci/tasks/main.yaml

请随意调整它以满足您的需求。

Here is how you can deploy Jenkins CI plugins using Ansible, which of course is used from the terminal. This code is a part of roles/jenkins_ci/tasks/main.yaml:

- name: Plugins
  with_items:                             # PLUGIN NAME
  - name: checkstyle                      # Checkstyle
  - name: dashboard-view                  # Dashboard View
  - name: dependency-check-jenkins-plugin # OWASP Dependency Check
  - name: depgraph-view                   # Dependency Graph View
  - name: deploy                          # Deploy
  - name: emotional-jenkins-plugin        # Emotional Jenkins
  - name: monitoring                      # Monitoring
  - name: publish-over-ssh                # Publish Over SSH
  - name: shelve-project-plugin           # Shelve Project
  - name: token-macro                     # Token Macro
  - name: zapper                          # OWASP Zed Attack Proxy (ZAP)
  sudo: yes
  get_url: dest="{{ jenkins_home }}/plugins/{{ item.name | mandatory }}.jpi"
           url="https://updates.jenkins-ci.org/latest/{{ item.name }}.hpi"
           owner=jenkins group=jenkins mode=0644
  notify: Restart Jenkins

This is a part of a more complete example that you can find at:
https://github.com/sakaal/service_platform_ansible/blob/master/roles/jenkins_ci/tasks/main.yaml

Feel free to adapt it to your needs.

高跟鞋的旋律 2024-12-16 03:34:47

您可以使用此命令行更新插件列表

curl -s -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -s -X POST -H 'Accept: application/json' -d @- http://localhost:8080/updateCenter/byId/default/postBack

You can update plugins list with this command line

curl -s -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -s -X POST -H 'Accept: application/json' -d @- http://localhost:8080/updateCenter/byId/default/postBack
仲春光 2024-12-16 03:34:47

仅供参考 - 某些插件(特别是 Mercurial)无法从命令行正确安装,除非您使用它们的短名称。我认为这与詹金斯包信息数据中的触发器有关。您可以通过在支持 javascript 的浏览器中访问 127.0.0.1:8080/pluginManager/checkUpdates 来模拟 jenkins 自己的包更新。

或者,如果你感觉受虐狂,你可以运行这个 python 代码:

import urllib2,requests

UPDATES_URL = 'https://updates.jenkins-ci.org/update-center.json?id=default&version=1.509.4'
PREFIX = 'http://127.0.0.1:8080'

def update_plugins():
    "look at the source for /pluginManager/checkUpdates and downloadManager in /static/<whatever>/scripts/hudson-behavior.js"
    raw = urllib2.urlopen(self.UPDATES_URL).read()
    jsontext = raw.split('\n')[1] # ugh, JSONP
    json.loads(jsontext) # i.e. error if not parseable
    print 'received updates json'

    # post
    postback = PREFIX+'/updateCenter/byId/default/postBack'
    reply = requests.post(postback,data=jsontext)
    if not reply.ok:
        raise RuntimeError(("updates upload not ok",reply.text))
    print 'applied updates json'

一旦你运行了这个,你应该能够运行 jenkins-cli -s http://127.0.0.1:8080 install-plugin Mercurial -部署。

FYI -- some plugins (mercurial in particular) don't install correctly from the command line unless you use their short name. I think this has to do with triggers in the jenkins package info data. You can simulate jenkins' own package update by visiting 127.0.0.1:8080/pluginManager/checkUpdates in a javascript-capable browser.

Or if you're feeling masochistic you can run this python code:

import urllib2,requests

UPDATES_URL = 'https://updates.jenkins-ci.org/update-center.json?id=default&version=1.509.4'
PREFIX = 'http://127.0.0.1:8080'

def update_plugins():
    "look at the source for /pluginManager/checkUpdates and downloadManager in /static/<whatever>/scripts/hudson-behavior.js"
    raw = urllib2.urlopen(self.UPDATES_URL).read()
    jsontext = raw.split('\n')[1] # ugh, JSONP
    json.loads(jsontext) # i.e. error if not parseable
    print 'received updates json'

    # post
    postback = PREFIX+'/updateCenter/byId/default/postBack'
    reply = requests.post(postback,data=jsontext)
    if not reply.ok:
        raise RuntimeError(("updates upload not ok",reply.text))
    print 'applied updates json'

And once you've run this, you should be able to run jenkins-cli -s http://127.0.0.1:8080 install-plugin mercurial -deploy.

水晶透心 2024-12-16 03:34:47

在当前的 Jenkins 版本中,CLI 只能通过 SSH 使用。必须在管理界面的“全局安全设置”页面中启用此功能,如 文档。此外,应触发更新的用户必须添加其公共 ssh 密钥。

使用已接受答案中修改后的 shell 脚本,可以如下自动化,您只需替换 HOSTNAME 和 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 ')

这将 greps Jenkins CLI 的已使用 SSH 端口,然后通过 SSH 连接,而无需检查主机密钥,因为它发生了变化每次 Jenkins 重新启动时。

然后所有有可用更新的插件都会升级,然后 Jenkins 会重新启动。

| 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

这将 greps Jenkins CLI 的已使用 SSH 端口,然后通过 SSH 连接,而无需检查主机密钥,因为它发生了变化每次 Jenkins 重新启动时。

然后所有有可用更新的插件都会升级,然后 Jenkins 会重新启动。

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

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.

| 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.

染柒℉ 2024-12-16 03:34:47

在 groovy 中

groovy 路径有一个很大的优点:它可以添加到作业中的“系统 groovy 脚本”构建步骤中,而无需进行任何更改。

使用以下内容创建文件“update_plugins.groovy”:

jenkins.model.Jenkins.getInstance().getUpdateCenter().getSites().each { site ->
  site.updateDirectlyNow(hudson.model.DownloadService.signatureCheck)
}
hudson.model.DownloadService.Downloadable.all().each { downloadable ->
  downloadable.updateNow();
}
def plugins = jenkins.model.Jenkins.instance.pluginManager.activePlugins.findAll {
  it -> it.hasUpdate()
}.collect {
  it -> it.getShortName()
}
println "Plugins to upgrade: ${plugins}"
long count = 0
jenkins.model.Jenkins.instance.pluginManager.install(plugins, false).each { f ->
  f.get()
  println "${++count}/${plugins.size()}.."
}
if(plugins.size() != 0 && count == plugins.size()) {
  println "restarting Jenkins..."
  jenkins.model.Jenkins.instance.safeRestart()
}

然后执行以下curl命令:

curl --user 'username:token' --data-urlencode "script=$(< ./update_plugins.groovy)" https://jenkins_server/scriptText

In groovy

The groovy path has one big advantage: it can be added to a 'system groovy script' build step in a job without any change.

Create the file 'update_plugins.groovy' with this content:

jenkins.model.Jenkins.getInstance().getUpdateCenter().getSites().each { site ->
  site.updateDirectlyNow(hudson.model.DownloadService.signatureCheck)
}
hudson.model.DownloadService.Downloadable.all().each { downloadable ->
  downloadable.updateNow();
}
def plugins = jenkins.model.Jenkins.instance.pluginManager.activePlugins.findAll {
  it -> it.hasUpdate()
}.collect {
  it -> it.getShortName()
}
println "Plugins to upgrade: ${plugins}"
long count = 0
jenkins.model.Jenkins.instance.pluginManager.install(plugins, false).each { f ->
  f.get()
  println "${++count}/${plugins.size()}.."
}
if(plugins.size() != 0 && count == plugins.size()) {
  println "restarting Jenkins..."
  jenkins.model.Jenkins.instance.safeRestart()
}

Then execute this curl command:

curl --user 'username:token' --data-urlencode "script=$(< ./update_plugins.groovy)" https://jenkins_server/scriptText
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文