# Jenkins

## 项目地址

{% embed url="<https://github.com/jenkinsci/jenkins>" %}

## 快速开始

1、创建 Dockerfile，在 Jenkins 镜像中预装 Blue Ocean 插件（可选）

```
mkdir /opt/jenkins && cd /opt/jenkins
```

{% code title="Dockerfile" %}

```
FROM jenkins/jenkins:2.263.4-lts-jdk11
USER root
RUN apt-get update && apt-get install -y apt-transport-https \
       ca-certificates curl gnupg2 \
       software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN apt-key fingerprint 0EBFCD88
RUN add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/debian \
       $(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins blueocean:1.24.4
```

{% endcode %}

2、构建 Jenkins 镜像（可选）

```
docker build -t jenkins:2.263.4-lts-jdk11-blueocean .
```

3、创建 docker-compose.yml 文件

{% code title="docker-compose.yml" %}

```
services:
  jenkins:
    # 如果无需预装 Blue Ocean 插件，可以直接使用官方镜像
    image: 'jenkins:2.263.4-lts-jdk11-blueocean'
    restart: always
    environment:
      DOCKER_HOST: 'tcp://docker:2376'
    ports:
      - '80:8080'
      - '50000:50000'
    volumes:
      - '/data/jenkins/certs:/certs/client:ro'
      - '/data/jenkins/home:/var/jenkins_home'
    links:
      - "dind:docker"
  dind:
    image: 'docker:dind'
    restart: always
    environment:
      DOCKER_TLS_CERTDIR: '/certs'
    ports:
      - '2376:2376'
    volumes:
      - '/data/jenkins/certs:/certs/client'
      - '/data/jenkins/home:/var/jenkins_home'
    privileged: true
```

{% endcode %}

4、创建 Jenkins 数据目录并授权

```
mkdir -p /opt/jenkins/home
chown 1000:1000 /opt/jenkins/home
```

5、启动 Jenkins

```
docker-compose up -d
```

6、获取 Jenkins 管理员初始密码

```
cat /data/jenkins/home/secrets/initialAdminPassword
```

7、浏览器访问 Jenkins 地址 http\://\<ip>:80，使用管理员初始密码登录

### Q\&A

#### 如何配置gitlab webhook触发流水线执行

在 Jenkins 的插件管理中安装 [Gitlab 插件](https://github.com/jenkinsci/gitlab-plugin)，然后在流水线中勾选启用 GitLab webhook，记录下方的 URL，为了安全起见，展开高级选项并生成一串 token 密钥。

![](/files/gWAFLfW7K0ItujnjrwTi)

![](/files/oryqeHZYJWO9jVG7EX0h)

在 Gitlab 项目中进入 Setting -> Webhook，填入上方生成的 URL 与 Token 密钥，保存后即可发送测试

![](/files/X3PgnfBIqumVcGZHU613)

![](/files/Uq4usQs7zVRZJR7EB3qp)

![](/files/PHnpQFSySnCuthKVDntc)

在 Webhook 条件触发时，可在 Jenkins 流水线上看到任务被调用执行

![](/files/5FWC6HeJtJxcXsk9XFMW)

通过Webhook触发的流水线中可以使用下参数：

```
gitlabBranch
gitlabSourceBranch
gitlabActionType
gitlabUserName
gitlabUserUsername
gitlabUserEmail
gitlabSourceRepoHomepage
gitlabSourceRepoName
gitlabSourceNamespace
gitlabSourceRepoURL
gitlabSourceRepoSshUrl
gitlabSourceRepoHttpUrl
gitlabMergeRequestTitle
gitlabMergeRequestDescription
gitlabMergeRequestId
gitlabMergeRequestIid
gitlabMergeRequestState
gitlabMergedByUser
gitlabMergeRequestAssignee
gitlabMergeRequestLastCommit
gitlabMergeRequestTargetProjectId
gitlabTargetBranch
gitlabTargetRepoName
gitlabTargetNamespace
gitlabTargetRepoSshUrl
gitlabTargetRepoHttpUrl
gitlabBefore
gitlabAfter
gitlabTriggerPhrase
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viva.gitbook.io/project/devops/cicd/jenkins.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
