我使用Jenkins与Kubernetes插件,我想将JNLP镜像与我的从属镜像组合使用。

huangapple go评论60阅读模式
英文:

I'm using Jenkins with Kubernetes plugin and I want to combine JNLP image with my slave image

问题

我希望我的默认 JNLP 容器能够运行我的镜像。
因此,我需要用我的镜像来覆盖 JNLP,但保留 JNLP 数据以便连接到我的主节点。

  1. 我应该将 JNLP 镜像作为基础使用,是吗?
  2. 如果我已经有 "FROM UBUNTU",我该如何将其与我的镜像组合,能够同时使用多个基础镜像并复制构建产物吗?我应该如何编写 Dockerfile?

我的自定义镜像 -

FROM ubuntu:18.04

ARG JFROG_CI_INT_USERNAME
ARG JFROG_CI_INT_PASSWORD
ARG JFROG_CI_INT_NPM_TOKEN
ARG GITHUB_ORANGE_USER
ARG GITHUB_ORANGE_PASSWORD
ARG PULUMI_USER
ARG PULUMI_TOKEN

ENV GITHUB_ORANGE_USER=$GITHUB_ORANGE_USER
ENV GITHUB_ORANGE_PASSWORD=$GITHUB_ORANGE_PASSWORD
ENV DEBIAN_FRONTEND=noninteractive

#=============
# 设置工作目录
#=============
WORKDIR /home/jenkins

COPY requirements.txt /
COPY authorization.sh /

# 更新软件仓库
RUN apt-get update
RUN apt-get -qqy install software-properties-common
RUN add-apt-repository ppa:git-core/ppa
RUN add-apt-repository ppa:openjdk-r/ppa
RUN add-apt-repository ppa:deadsnakes/ppa

RUN apt-get -qqy install git

ENV PYTHON_VER="3.9"

RUN apt-get -qqy update && \
    apt-get -qqy --no-install-recommends install \
    gpg-agent \
    software-properties-common \
    openjdk-11-jdk \
    ca-certificates \
    build-essential \
    tzdata \
    zip \
    unzip

ENTRYPOINT ["/usr/local/bin/start.sh"]

注意:Dockerfile 中的中文注释已经保留。

英文:

I want my default JNLP container to run with my image.
Therefore, I need to overrite the JNLP with my image, but to keep the JNLP data to be able to connect to my master.

  1. The jnlp image I should have as base is inbound-agent no?
  2. How can I combine it with my image if I already have "FROM UBUNTU" can I combine multiple base images and copy the artifacts? How can I do that and what should be my dockerfile?

My own image -

FROM ubuntu:18.04
ARG JFROG_CI_INT_USERNAME
ARG JFROG_CI_INT_PASSWORD
ARG JFROG_CI_INT_NPM_TOKEN
ARG GITHUB_ORANGE_USER
ARG GITHUB_ORANGE_PASSWORD
ARG PULUMI_USER
ARG PULUMI_TOKEN
ENV GITHUB_ORANGE_USER=$GITHUB_ORANGE_USER
ENV GITHUB_ORANGE_PASSWORD=$GITHUB_ORANGE_PASSWORD
ENV DEBIAN_FRONTEND=noninteractive
#=============
# Set WORKDIR
#=============
WORKDIR /home/jenkins
COPY requirements.txt /
COPY authorization.sh /
# Update software repository
RUN apt-get update
RUN apt-get -qqy install software-properties-common
RUN add-apt-repository ppa:git-core/ppa
RUN add-apt-repository ppa:openjdk-r/ppa
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get -qqy install git
ENV PYTHON_VER="3.9"
RUN apt-get -qqy update && \
apt-get -qqy --no-install-recommends install \
gpg-agent \
software-properties-common \
openjdk-11-jdk \
ca-certificates \
build-essential \
tzdata \
zip \
unzip \
ENTRYPOINT ["/usr/local/bin/start.sh"]

答案1

得分: 1

您可以使用多阶段构建,但最终会得到一个紧密耦合的镜像,每次想要更改 Jenkins 代理版本时都需要重新构建它。

有一个更好(在我看来)的选项,使用两个容器。您可以在 Kubernetes 中运行代理,使用两个镜像:inbound-agent 和您的镜像。这是我有的一个工作流示例:

pipeline {
    agent {
        kubernetes {
            yaml """
apiVersion: v1
kind: Pod
spec:
    containers:
    - name: jnlp
      image: jenkins/inbound-agent:4.10-3-alpine-jdk8
      volumeMounts:
      - name: home-volume
        mountPath: /home/jenkins
      env:
      - name: HOME
        value: /home/jenkins
    - name: maven
      image: my-registry:5000/maven-3.6.3-jdk-11:latest
      command:
      - sleep
      args: 
      - 1d
      volumeMounts:
      - name: home-volume
        mountPath: /home/jenkins
      env:
      - name: JAVA_TOOL_OPTIONS
        value: -Dfile.encoding=UTF8
    volumes:
    - name: home-volume
      emptyDir: {}
"""
        }
    }
    stages {
      stage('Build') {
        steps {
          script {
            container('maven') {
              sh('mvn clean deploy')

这样,您可以使这两个镜像解耦,但它们在同一个 Pod 中运行以使工作流正常工作。

英文:

You can do a multi-stage build but you'll end with a tightly coupled image, and you'll have to rebuild it every time you want to change the jenkins agent version.

There's a better (IMHO) option, using two containers. You can run an agent in kubernetes using two images: inbound-agent and your image. This is from a working pipeline that I have:

pipeline {
    agent {
        kubernetes {
            yaml """
apiVersion: v1
kind: Pod
spec:
    containers:
    - name: jnlp
      image: jenkins/inbound-agent:4.10-3-alpine-jdk8
      volumeMounts:
      - name: home-volume
        mountPath: /home/jenkins
      env:
      - name: HOME
        value: /home/jenkins
    - name: maven
      image: my-registry:5000/maven-3.6.3-jdk-11:latest
      command:
      - sleep
      args: 
      - 1d
      volumeMounts:
      - name: home-volume
        mountPath: /home/jenkins
      env:
      - name: JAVA_TOOL_OPTIONS
        value: -Dfile.encoding=UTF8
    volumes:
    - name: home-volume
      emptyDir: {}
"""
        }
    }
    stages {
      stage('Build') {
        steps {
          script {
            container('maven') {
              sh('mvn clean deploy')

This way you have both images decoupled, but they run together in the same pod to make the pipeline work.

huangapple
  • 本文由 发表于 2023年2月26日 20:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定