0


将整个Jenkins发布到git,完成Jenkins备份,附上部署教程

我们的Jenkins一般都是两种部署方式,一种是直接部署到机器上,另一种是部署在docker内,但是不管哪一种部署方式,都依赖于机器的存货性,下面咱这个也相当于一个备份方案吧,可以随便想部署到哪里就部署到哪里。

首先,大家要先了解Jenkins_home 这个目录内具体都代表什么含义,大家可以看此人的文章, 介绍了Jenkins的目录 JENKINS_HOME 目录解析 - 简书

然后大家了解了Jenkins目录之后,就可以根据自己的项目需求,具体决定自己的Jenkins需要保存什么目录,大家可以看下我的,我的这个jobs中,只留下了各个job的config.xml文件,所以当Jenkins启动起来时,只有干净的job,没有任何的日志

接下来呢说一下部署,还有插件的安装

部署呢还是用到了docker-compose,安装教程Docker Compose | 菜鸟教程

docker-compose.yml 文件

version: "2"
services:
  jenkins:
    platform: linux/amd64
    container_name: jenkins-node-perf
    build:
      context: .
      dockerfile: ./jenkins-server/Dockerfile
      args:
        - proxy=${proxy}
    ports:
      - ${SERVER_PORT}:8080
      - 50000:50000
    volumes:
      - jenkins-data:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      # Must mount host's /tmp dir, since we will mount some temp dir into workload docker image,
      # and we actually use host's docker daemon
      - /tmp:/tmp
    environment:
      - JENKINS_IP=${SERVER_IP}
      - JENKINS_PORT=${SERVER_PORT}
      # - DOCKER_HOST=tcp://docker:2376
      # - DOCKER_CERT_PATH=/certs/client
      # - DOCKER_TLS_VERIFY=1
    restart: on-failure

networks:
  default:
    name: jenkins

volumes:
  jenkins-data: {}
  # jenkins-docker-certs: {}

Jenkins-serer目录内容,install_plugins.py 是安装插件写的一个python脚本,plugin_list.txt 插件列表文件,自己根据自己的Jenkins文件写,requirements python需要的包,也是根据自己的Jenkins而定

外层的 .env文件是docker-compose 需要的变量,根据自己的需求定

Dockerfile 文件内容

大家首先需要自己docker pull jenkins/jenkins:2.361.4-jdk11 从docker拉取此镜像


FROM jenkins/jenkins:2.361.4-jdk11
ARG proxy=""
USER jenkins
# ADD --chown=jenkins:jenkins ./jobs/ /var/jenkins_home/jobs
ADD --chown=jenkins:jenkins ./jenkins-home /var/jenkins_home
ADD ./jenkins-server/requirements.txt .
ADD ./jenkins-server/install_plugins.py .
ADD ./jenkins-server/plugin_list.txt .

ADD ./jenkins-server/config.json /var/jenkins_home/.docker/config.json

# setup IP

RUN echo "<?xml version='1.1' encoding='UTF-8'?>                                    \
<jenkins.model.JenkinsLocationConfiguration>                                        \
  <adminAddress>address not configured yet &lt;nobody@nowhere&gt;</adminAddress>    \
  <jenkinsUrl>http://$JENKINS_IP:$JENKINS_PORT/</jenkinsUrl>                        \
</jenkins.model.JenkinsLocationConfiguration>" > /var/jenkins_home/jenkins.model.JenkinsLocationConfiguration.xml

USER root

env http_proxy $proxy
env https_proxy $proxy
env ftp_proxy $proxy

RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
  https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list

RUN apt-get update && apt-get install -y docker-ce-cli python3-pip runc bc

RUN chown -R jenkins /var/jenkins_home/jobs
RUN chgrp -R jenkins /var/jenkins_home/jobs

# setup docker without sudo

RUN groupadd docker
RUN usermod -aG docker jenkins
RUN newgrp docker

# USER jenkins

RUN python3 install_plugins.py
RUN pip3 install -r requirements.txt

install_plugins 脚本

import os

f = open('plugin_list.txt', 'r')
for plugin in f.readlines():
    plugin = plugin.strip('\n')
    print(f'Installing plugin [{plugin}] ...', flush=True)
    cmd = f'JAVA_TOOL_OPTIONS="" jenkins-plugin-cli --plugins {plugin} > /dev/null 2>&1'
    ret_code = os.WEXITSTATUS(os.system(cmd))

    if (ret_code != 0):
        exit(1)

然后运行docker-compose up 就可以运行了

标签: jenkins docker 容器

本文转载自: https://blog.csdn.net/wNews/article/details/128037372
版权归原作者 主后端沾点前端 所有, 如有侵权,请联系我们删除。

“将整个Jenkins发布到git,完成Jenkins备份,附上部署教程”的评论:

还没有评论