how to play jenkins?

社区源码、文档1等托管在 GitHub 上。

其中基础设施部分在 jenkins-infra

核心库以及插件在 jenkinsci2

Jenkins 中文本地化相关的项目在 jenkins-zh

集中玩耍地地方3

搜集玩法

  • 1、官方建议容器化部署(docker/kubernetes)

    建议使用的Docker映像是jenkinsci/blueocean image(来自 the Docker Hub repository)。 该镜像包含当前的长期支持 (LTS) 的Jenkins版本 (可以投入使用) ,捆绑了所有Blue Ocean插件和功能。这意味着你不需要单独安装Blue Ocean插件。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    docker run \
      -u root \
      --rm \
      -d \
      -p 8080:8080 \
      -p 50000:50000 \
      -v jenkins-data:/var/jenkins_home \
      -v /var/run/docker.sock:/var/run/docker.sock \
      jenkinsci/blueocean
    

    能玩一段时间了。。。

  • 2、前面都是铺垫,玩了一段时间,发现原生镜像中缺这少那的,尤其是插件,看看官方怎么说:

    Keep in mind that the process described above will automatically download the official Jenkins Docker image if this hasn’t been done before.

    • Create Dockerfile with the following content:

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      
      FROM jenkinsci/blueocean
      USER root
      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
      USER jenkins
      RUN jenkins-plugin-cli --plugins "blueocean:1.25.3 docker-workflow:1.28"
      
    • Build a new docker image from this Dockerfile and assign the image a meaningful name, e.g. “myjenkins-blueocean:2.319.3-1”:

      1
      
      docker build -t myjenkins-blueocean:2.319.3-1 .
      
  • 3、同时,也可以站在巨人的肩膀上,有一些实践者把常用的插件已经内置到镜像发布了,比如 kubespheredev/ks-jenkins

  • 4、程序员用界面?命令行起飞

    利用 jcli 管理 Jenkins

    vs

    原生Jenkins CLI

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # 例如,本地敏捷调试
    # 安装
    wget -q https://ghproxy.com/https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz|tar -zxvf && sudo mv jcli /usr/local/bin/
    # 配置
    jcli config gen
    # 都走默认就🆗
    
    # 启动
    jcli center start -m docker --image kubespheredev/ks-jenkins --version 2.249.1 --c-user root --port 8080 --setup-wizard=false
    
  • 5、国内插件源

    https://updates.jenkins-zh.cn/update-center.json

  • 6、划重点,就是 pipeline(前面的插件已经铺垫过)。what???

    pipeline ==~ “持续交付即代码”

    Jenkins Pipeline 的定义通常被写入到一个文本文件(称为 Jenkinsfile )中,该文件可以被放入项目的源代码控制库中。

    official recommended how to play: all in Jenkinsfile!!!

    (片段生成器+声明式+。。。反正就是生成)https://jenkins地址/job/test/pipeline-syntax/

【官方词典】

image-20220302150354810

  • 7、官方把架子打好了,让我们发挥是吧,那有木有前辈累计的模板,或者我有新颖的使用方式方法想分享给大家呢:

    enjoy your play:

    【官方例子】

    【DevOps Workspace】

  • 8、又玩了一段时间,发现jenkins最好是作为自动化的引擎,通过接口集成到 PASS 上。

    Use Pipeline through API

    one example

都在玩儿plugin

  • 插件下载问题,更改了国内镜像源,还总是下载失败?

    1、设置代理

    2、手动修改 jenkins/updates/default.json 中url —> https://mirrors.tuna.tsinghua.edu.cn

    3、Jenkins Customize 更新

  • Jenkins Configuration as Code

    虽然新部署的 Jenkins 实例自动为我们安装了所有所需的插件,并配置好了初始化 Job 等工作,但在开始使用它之前,我们仍需要完成一系列手动工作,如配置 Jenkins 的 “Configure System” 页面

    如果你是一名 Jenkins 管理员,那么你一定不会对这个页面感到陌生,每次部署完一个新的 Jenkins 实例,在可以使用之前,我们往往都需要在该页面作出一些相应的配置。该页面除了包含 Jenkins 自身的一些基本配置信息外,同时还包括了当前系统中所安装的插件的配置信息。也就是说,当你的 Jenkins 安装的插件越多,该页面的配置项就有可能会越多。

    1
    
    云时代 这个插件用处就少些了,所有的配置都放在打包在镜像中了
    
  • generic-webhook-trigger-plugin

    it can trigger on ant webhook

最佳实践

1、multi-branch + library

2、webhook+library

参考文献