【问题标题】:Add predefined Jenkins jobs to Jenkins master in docker将预定义的 Jenkins 作业添加到 docker 中的 Jenkins master
【发布时间】:2018-03-28 15:53:11
【问题描述】:

我现在已经在互联网上搜索了整整 2 天,没有任何进展。可能是这样,只需单击一下即可找到答案,但我找不到。我瞎了。顺便说一句,我在 Windows 10 上工作...

问题是我想创建一个简单的 Dockerfile,它可以创建一个带有 Jenkins master 的 Docker 映像,其中包含我希望它包含的所有内容。例如:

  • 一般 Jenkins 配置
  • 用户
  • 插件
  • 工作
  • 奴隶

等等……

现在我至少有一些部分在工作......我能够摆脱最初的“你好,欢迎来到 jenkins”,并定义我的第一个用户 + 安装我在文本文件中写下的插件。

但是当涉及到该死的詹金斯工作时,它似乎变得有点复杂。

由于我的 Dockerfile 从 jenkins/jenkins:lts-alpine 获取“基本映像”,因此它为 /var/jenkins_home/ 创建了一个卷,这似乎是存储作业的位置。而且将文件复制到这个文件夹似乎有点问题。我尝试在我的 Dockerfile 中添加一条 COPY 指令来复制我在 Jenkins 中手动创建作业时创建的文件夹和文件,但似乎 Jenkins 出于某种原因没有读取它们,即使在从磁盘重新启动/重新加载之后也是如此。问题是,在“安装”后复制作业时它实际上正在工作。就像 docker cp jobs-on-my-machine container:/var/jenkins_home/jobs 一样,但我不想在我的 Dockerfile 之外添加很多额外的东西。我想让它尽可能简单。使用此解决方案,我很可能甚至无法提交此更改,因为 docker diff 显示此输出:

C:\Dev>docker diff 3
C /tmp
A /tmp/hsperfdata_jenkins
A /tmp/hsperfdata_jenkins/6
A /tmp/jetty-0.0.0.0-8080-war-_-any-6459623464825334329.dir
A /tmp/jna--1712433994
A /tmp/winstone4948624124562796293.jar

你看... /var/jenkins_home/jobs/ 目录中没有任何变化...根本没有可跟踪的内容:(

在下面看看我的 Dockerfile 内容:

# Pull the latest Jenkins docker image from Docker Hub.
# Currently Alpine is used because the Internet says its safer :)
from jenkins/jenkins:lts-alpine

# Disable Jenkins setup wizard normally showing up during initial startup
ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"

# Copy the groovy script creating the first user into the 
# run-on-startup-directory into the docker image
COPY **security.groovy** /usr/share/jenkins/ref/init.groovy.d/security.groovy

# Copy the plugins text file into the docker image
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt

# Run the Jenkins default install-plugins script to install 
# plugins defined in the plugins text file
RUN /usr/local/bin/install-plugins.sh < /usr/share/jenkins/ref/plugins.txt

# Add the predefined Jenkins jobs _inside_ folder 'jenkins-jobs' into Jenkins
COPY jenkins-jobs /var/jenkins_home/jobs/

security.groovy 内容:

#!groovy

import jenkins.model.*
import hudson.security.*
import jenkins.security.s2m.AdminWhitelistRule

def instance = Jenkins.getInstance()

def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount("admin", "admin")
instance.setSecurityRealm(hudsonRealm)

def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
instance.setAuthorizationStrategy(strategy)
instance.save()

Jenkins.instance.getInjector().getInstance(AdminWhitelistRule.class).setMasterKillSwitch(false)

plugins.txt:

cloudbees-folder
bouncycastle-api
structs
script-security
workflow-step-api
scm-api
workflow-api
junit
antisamy-markup-formatter
workflow-support
workflow-job
token-macro
build-timeout
credentials
ssh-credentials
plain-credentials
credentials-binding
timestamper
durable-task
workflow-durable-task-step
matrix-project
resource-disposer
ws-cleanup
ant
gradle
pipeline-milestone-step
jquery-detached
jackson2-api
ace-editor
workflow-scm-step
workflow-cps
pipeline-input-step
pipeline-stage-step
pipeline-graph-analysis
pipeline-rest-api
handlebars
momentjs
pipeline-stage-view
pipeline-build-step
pipeline-model-api
pipeline-model-extensions
apache-httpcomponents-client-4-api
jsch
git-client
git-server
workflow-cps-global-lib
display-url-api
mailer
branch-api
workflow-multibranch
authentication-tokens
docker-commons
docker-workflow
pipeline-stage-tags-metadata
pipeline-model-declarative-agent
workflow-basic-steps
pipeline-model-definition
workflow-aggregator
github-api
git
github
github-branch-source
pipeline-github-lib
mapdb-api
subversion
ssh-slaves
matrix-auth
pam-auth
ldap
email-ext
mercurial

【问题讨论】:

  • 请将plugins.txt的内容复制到/usr/share/jenkins/ref/plugins.txt
  • 添加了______:)

标签: image docker jenkins dockerfile master


【解决方案1】:

万一其他人发现这个问题:

来自GitHub - jenkinsci/docker

在这样的派生图像中,您可以使用挂钩脚本或其他插件自定义您的 jenkins 实例。为此,请使用 /usr/share/jenkins/ref 作为一个位置来定义您希望目标安装看起来像的默认 JENKINS_HOME 内容:

当 jenkins 容器启动时,它会检查 JENKINS_HOME 是否有这个引用内容,如果需要,将它们复制到那里。它不会覆盖此类文件,因此如果您从 UI 升级了某些插件,它们将不会在下次启动时恢复。
如果您确实想要覆盖,请将“.override”附加到参考文件的名称中。例如。名为 /usr/share/jenkins/ref/config.xml.override 的文件将覆盖 JENKINS_HOME 中现有的 config.xml 文件。

我自己所做的以及对我有用的是:将 Jenkins 实例上的 jobs 目录复制到您可以运行 docker build 的位置。将 Jenkins 实例中的 jobs 目录放入另一个目录,例如 jobs_jenkins。这意味着您将拥有$PWD/jobs_jenkins/jobs

在您的Dockerfile 中添加以下行:

...
FROM jenkins/jenkins:lts-alpine
COPY jobs_jenkins /usr/share/jenkins/ref/
...

据我了解,我认为 jenkins 映像本身运行一个使用 /usr/share/jenkins/ref 的“启动脚本”,直接复制到 /var/jenkins_home 的内容将不存在... 如需更多信息,请阅读README.md。 (来自GitHub - jenkinsci/docker

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多