如何使用groovy for Jenkins自动化Maven和Java JDK8安装? [英] How to automate Maven and Java JDK8 installation with groovy for Jenkins?

查看:184
本文介绍了如何使用groovy for Jenkins自动化Maven和Java JDK8安装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Jenkins Docker映像,并且我想自动化最后一个JDK的Maven 3和Java 8的安装.但是不幸的是,我使用这两个groovy文件定位到groovy文件夹中:

I'm building a Jenkins Docker image and I will like to automate the installation of Maven 3 and Java 8 last JDK. But unfortunately I use these two groovy files locate into the groovy folder:

groovy/java.groovy :

import jenkins.model.*
import hudson.model.*
import hudson.tools.*

def inst = Jenkins.getInstance()

def desc = inst.getDescriptor("hudson.model.JDK")

def versions = [ "jdk8": "jdk-8u202"]
def installations = [];

for (v in versions) {
  def installer = new JDKInstaller(v.value, true)
  def installerProps = new InstallSourceProperty([installer])
  def installation = new JDK(v.key, "", [installerProps])
  installations.push(installation)
}

desc.setInstallations(installations.toArray(new JDK[0]))

desc.save()

groovy/maven.groovy :

import jenkins.*;
import jenkins.model.*;
import hudson.*;
import hudson.model.*;

mavenName = "maven3"
mavenVersion = "3.6.0"
println("Checking Maven installations...")

// Grab the Maven "task" (which is the plugin handle).
mavenPlugin = Jenkins.instance.getExtensionList(hudson.tasks.Maven.DescriptorImpl.class)[0]

// Check for a matching installation.
maven3Install = mavenPlugin.installations.find {
   install -> install.name.equals(mavenName)
}

// If no match was found, add an installation.
if(maven3Install == null) {
   println("No Maven install found. Adding...")

   newMavenInstall = new hudson.tasks.Maven.MavenInstallation('maven3', null,
    [new hudson.tools.InstallSourceProperty([new hudson.tasks.Maven.MavenInstaller(mavenVersion)])]
)

   mavenPlugin.installations += newMavenInstall
   mavenPlugin.save()

   println("Maven install added.")
} else {
   println("Maven install found. Done.")
}

然后运行命令:

docker run -p 8080:8080 -v `pwd`/groovy:/var/jenkins_home/jobs --rm --name jenkinsdocker jenkinsdocker:latest

不幸的是,这将返回错误:

Unfortunately this returns an error:

java.io.IOException: jenkins.model.InvalidBuildsDir: ${ITEM_ROOTDIR}/builds does not exist and probably cannot be created

我只是想知道groovy文件是否错误或者是否还有其他遗漏?

I'm just wondering if the groovy files are wrong or if there is something else I missed?

在运行 docker run 时如何自动为Jenkins安装Maven/java?还是有另一种方法?

How can I automate the maven/java installation for Jenkins while running a docker run? Or is there another way to do it?

推荐答案

我没有找到jenkinsdocker镜像,所以我猜是从jenkins生成的一些docker镜像.在此jenkinsdocker文档之后的任何情况下,您都应将groovy脚本放在要在init上执行的文件夹.因此,您需要运行docker通过init.groovy.d修改jobs并使用jenkins:latest图像:

I did not find jenkinsdocker image, so I guess is some docker image you generated from jenkins. In any case following this jenkinsdocker documentation you should put your groovy scripts in the init.groovy.d folder to be executed on init. So you need to run docker modifying jobs by init.groovy.d and use the jenkins:latest image:

     docker run -p 8080:8080 -v `pwd`/groovy:/var/jenkins_home/init.groovy.d/ --rm --name jenkins jenkins:latest

您还可以按照上面的示例创建自己的Dockerfile(例如,可以使用2.60.3版):

You can also create your own Dockerfile (you can use version 2.60.3 for instance) as in the example above:

# Extended from https://github.com/jenkinsci/docker/blob/master/README.md
FROM jenkins/jenkins:2.60.3

# Skip setup wizard
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"

# Add groovy script to Jenkins hook
COPY --chown=jenkins:jenkins groovy/ /var/jenkins_home/init.groovy.d/

构建并运行容器:

docker build jenkinsdocker -t .
docker run -p 8080:8080 --name jenkinsdocker jenkinsdocker:latest

这篇关于如何使用groovy for Jenkins自动化Maven和Java JDK8安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆