Skip to content

Commit

Permalink
doc: add jenkins document.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 22, 2024
1 parent d79b379 commit e8d279e
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 17 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
- [Docker私有仓库搭建](#docker私有仓库搭建)
- [`registry`](#registry)
- [`Harbor`](#harbor)
- [使用 Docker 实战](#使用docker实战)
- [使用 Docker 实战](#使用-docker-实战)
- [Docker REST API](#docker-rest-api)
- [`Nginx`](#nginx)
- [`MySQL`](#mysql)
Expand Down Expand Up @@ -239,10 +239,15 @@ docker run -t -i nginx:latest /bin/bash
由于国区已经无法访问,可以将镜像打包,直接安装镜像压缩文件

```sh
$ docker pull gitlab/gitlab-ce:17.0.1-ce.0 # 下载镜像
$ docker pull gitlab/gitlab-ce:17.2.0-ce.0 # 下载镜像
$ docker pull --platform linux/amd64 gitlab/gitlab-ce:17.2.0-ce.0
$ docker pull --platform linux/amd64 portainer/portainer-ce:2.20.3-alpine
$ docker pull --platform linux/amd64 portainer/agent:2.20.3-alpine
# 保存 Docker 镜像到本地文件
$ docker save -o [output-file.tar] [image-name]
$ docker save -o gitlab-ce-17.0.1.tar gitlab/gitlab-ce
$ docker save -o gitlab-ce.0-17.2.0.tar gitlab/gitlab-ce:17.2.0-ce.0
$ docker save -o portainer-agent-2.20.3-alpine.tar portainer/agent:2.20.3-alpine
$ docker save -o portainer-ce-2.20.3-alpine.tar portainer/portainer-ce:2.20.3-alpine
```

将镜像文件发送到服务器
Expand Down Expand Up @@ -622,7 +627,7 @@ curl -s --unix-socket /var/run/docker.sock http://dummy/containers/json
docker info
```

## 使用Docker实战
## 使用 Docker 实战

> ⚠文件挂载注意:docker 禁止用主机上不存在的文件挂载到 container 中已经存在的文件
Expand Down Expand Up @@ -728,6 +733,10 @@ docker info

[在 docker 中部署 Navidrome](docs/navidrome.md)

### `Jenkins`

[在 docker 中部署 Jenkins](docs/jenkins/README.md)

### `Humpback`

首先创建放持久化数据文件夹,`mkdir -p /opt/app/humpback-web`,里面存放持久化数据文件,会存储站点管理和分组信息,启动后请妥善保存。
Expand Down Expand Up @@ -759,9 +768,9 @@ docker run -d --name seafile \

```shell
docker run -d --name seafile \
-e SEAFILE_SERVER_HOSTNAME=pan.showgold.com \
-e SEAFILE_ADMIN_EMAIL=wcj@nihaosi.com \
-e SEAFILE_ADMIN_PASSWORD=wcj@nihaosi.com \
-e SEAFILE_SERVER_HOSTNAME=pan.example.com \
-e SEAFILE_ADMIN_EMAIL=wcj@example.com \
-e SEAFILE_ADMIN_PASSWORD=wcj@example.com \
-v $HOME/_docker/seafile-data:/shared \
-p 80:80 \
seafileltd/seafile:latest
Expand Down
11 changes: 11 additions & 0 deletions docs/gitlab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ gitlab_rails['backup_keep_time'] = 604800
docker exec 容器名或容器ID gitlab-ctl reconfigure
```

## 备份恢复

```sh
# 从xxxxx编号备份中恢复
# 然后恢复数据,1406691018为备份文件的时间戳
gitlab-rake gitlab:backup:restore BACKUP=1406691018

# 新版本 1721392543_2024_07_19_17.0.1_gitlab_backup.tar
gitlab-rake gitlab:backup:restore BACKUP=1721392543_2024_07_19_17.0.1
```

## 容器管理

```bash
Expand Down
20 changes: 20 additions & 0 deletions docs/jenkins/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM --platform=linux/amd64 jenkins/jenkins:2.468-jdk21

# 切换到 root 用户以安装 Docker CLI
USER root

# 安装必要的软件包
RUN apt-get update && apt-get install -y \
curl \
gnupg2 \
lsb-release \
software-properties-common

# 安装特定版本的 Docker CLI
RUN curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-26.1.4.tgz | tar xzvf - --strip-components=1 -C /usr/local/bin

# 创建 docker 组并将 jenkins 用户添加到该组
RUN groupadd docker && usermod -aG docker jenkins

# 切换回 jenkins 用户
USER jenkins
253 changes: 253 additions & 0 deletions docs/jenkins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
Jenkins
===

下面图文示例,是通过 Docker 部署 Jenkins 并将 `Pipeline` 脚本运行在 `Docker` 中的教程,这样可以方便的使用任何版本的 `nodejs` 或者 `java` 的沙盒环境

## 部署 Jenkins

获取 Jenkins 和 Jenkins agent 镜像,上传到服务器

```sh
# 获取 Jenkins 镜像
docker pull --platform linux/amd64 jenkins/jenkins:2.468-jdk21
# 保存 Docker 镜像到本地文件
docker save -o jenkins-2.468-jdk21.tar jenkins/jenkins:2.468-jdk21
# 上传到服务器
scp -P 2222 jenkins-2.468-jdk21.tar [email protected]:/home/docker-images

# 获取 Jenkins agent 镜像
docker pull --platform linux/amd64 jenkins/ssh-agent:jdk21
docker save -o jenkins-ssh-agent-jdk21.tar jenkins/ssh-agent:jdk21
scp -P 2222 jenkins-ssh-agent-jdk21.tar [email protected]:/home/docker-images
```

创建一个 `Dockerfile`,基于 `jenkins/jenkins:2.468-jdk21` 镜像添加 Docker 支持:

```Dockerfile
FROM --platform=linux/amd64 jenkins/jenkins:2.468-jdk21

# 切换到 root 用户以安装 Docker CLI
USER root

# 安装必要的软件包
RUN apt-get update && apt-get install -y \
curl \
gnupg2 \
lsb-release \
software-properties-common

# 安装 Docker CLI
# RUN curl -fsSL https://get.docker.com | sh
# 安装特定版本的 Docker CLI
RUN curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-26.1.4.tgz | tar xzvf - --strip-components=1 -C /usr/local/bin

# 创建 docker 组并将 jenkins 用户添加到该组
RUN groupadd docker && usermod -aG docker jenkins

# 切换回 jenkins 用户
USER jenkins
```

构建 jenkins 新的镜像

```sh
docker build --platform=linux/amd64 -t my-jenkins-docker-2468-jdk21 .
# 保存 Docker 镜像为一个 tar 文件
docker save -o my-jenkins-docker-2468-jdk21.tar my-jenkins-docker-2468-jdk21
# 通过 SCP 传输 tar 文件到远程服务器
scp -P 2222 my-jenkins-docker-2468-jdk21.tar [email protected]:/home/docker-images
```

```yml
# https://github.com/jenkinsci/docker/blob/master/README.md#docker-compose-with-jenkins
services:
jenkins:
image: my-jenkins-docker-2468-jdk21 # 自定义镜像
#image: jenkins/jenkins:2.468-jdk21
ports:
- "8086:8080"
volumes:
- jenkins_home:/var/jenkins_home
# 支持 docker
- /var/run/docker.sock:/var/run/docker.sock
ssh-agent:
image: jenkins/ssh-agent:jdk21
volumes:
jenkins_home:
```
确保您与 `docker-compose.yml` 在同一个目录中。并启动 `Jenkins`:

```sh
docker compose up -d # 启动 Jenkins
docker compose down # 停止并删除与 Docker Compose 配置文件相关的所有容器、网络、卷和镜像
```

访问 jenkins: http://152.22.3.186:8086/

## 1. 安装插件

- [Git Parameter Plug-In](https://plugins.jenkins.io/git-parameter) 支持在 CI 上自动加载仓库分支

## 2. 添加凭证

<img src="./imgs/1.png" width="620" />

<img src="./imgs/2.png" width="620" />

<img src="./imgs/3.png" width="620" />

<img src="./imgs/4.png" width="620" />

## 3. 新建工作流

<img src="./imgs/5.png" width="620" />

修改配置

<img src="./imgs/6.png" width="620" />


设置 [`Git Parameter`](https://plugins.jenkins.io/git-parameter) 可以在构建的时候读取分支,选择分支

<img src="./imgs/7.png" width="620" />

<img src="./imgs/8.png" width="620" />

## 3. 添加构建选项,可以在构建前选择 `生产` 还是 `开发` 模式,在脚本中判断

<img src="./imgs/9.png" width="620" />

## 4. 选择 `Pipeline script from SCM` 指定脚本位置

<img src="./imgs/10.png" width="620" />

这方法是将 仓库 `config` 分支添加一个 `Jenkinsfile` 配置

## Nodejs

安装 [Docker Pipeline](https://plugins.jenkins.io/git-parameter) 插件,支持从管道中构建和使用 `Docker` 容器。

```sh
docker pull --platform linux/amd64 node:14.16.0
docker pull --platform linux/amd64 node:18
docker pull --platform linux/amd64 node:20
docker pull --platform linux/amd64 node:22
# 保存 Docker 镜像到本地文件
docker save -o node14.16.tar node:14.16.0
# 上传到服务器
scp -P 2222 node14.16.tar [email protected]:/home/docker-images
docker save -o node18.tar node:18
scp -P 2222 node18.tar [email protected]:/home/docker-images
docker save -o node20.tar node:20
scp -P 2222 node20.tar [email protected]:/home/docker-images
docker save -o node22.tar node:22
scp -P 2222 node22.tar [email protected]:/home/docker-images
```

Pipeline 脚本中使用 Docker nodejs 20 运行

```groovy
pipeline {
//agent any
// pipeline 放到 docker 中执行
agent {
docker {
// 使用 Node.js 18 的 Docker 镜像
image 'node:20'
// 可选:挂载 NPM 缓存目录,加速构建
args '-v /root/.npm:/root/.npm'
}
}
environment {
def git_url="http://152.22.3.186:8081/mall/h5.git"
def git_auth = "12312312-f199-4b15-b087-123123"
def git_branch = "${branch}"
def project_env = "${project_env}"
def data_dir = "/mnt/mall/h5/h5_vip"
// 本地需要上传的目录 以及远程服务器的目录
def localDir = "${WORKSPACE}/h5_vip/test_dir/"
def vip_host = '106.53.119.240'
def vip_remote_dir = "/mnt/mall/h5"
}
stages {
stage('Git Checkout'){
steps {
echo "🏆 WORKSPACE: 【${WORKSPACE}】"
echo "🎯 branch: 【${git_branch}】"
echo "🏅 project_env: 【${project_env}】"
echo 'check git'
checkout([
$class: 'GitSCM',
branches: [[name: "${git_branch}" ]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: "${git_auth}",
url: "${git_url}"
]]
])
sh 'pwd'
sh 'ls -la'
}
}
}
stage('Send Files') {
when {
expression {
currentBuild.result == null || currentBuild.result == 'SUCCESS'
}
}
steps {
sh 'pwd'
script{
switch (project_env) {
case "vip":
sh '''
ls -la
cd h5_vip
npm install --registry=https://registry.npmmirror.com/
ls -la
npm run build
ls -la
rm -rf test_dir
mv dist test_dir
'''
withCredentials([sshUserPrivateKey(credentialsId: 'bd6f00e6-9dfd-4fd5-b94b-7559ca212e9a', keyFileVariable: 'SSH_KEY')]) {
// 连接到远程服务器并删除 test_dir 目录 重新上传
sh '''
ssh -i ${SSH_KEY} -o StrictHostKeyChecking=no [email protected] "rm -rf /mnt/mall/h5/test_dir"
scp -i ${SSH_KEY} -o StrictHostKeyChecking=no -P 22 -r "${localDir}" [email protected]:${vip_remote_dir}
'''
}
break
case "dev":
// for (i in ['152.22.3.186']) {
// data_dir = "/mnt/mall/h5/"
// //sh "ssh $i 'cp -rf ${data_dir}/* ${data_dir}/h5_vip_{uuid_no}'"
// sh "scp -r -o StrictHostKeyChecking=no -i /var/lib/jenkins/.ssh/id_rsa -P 22 '${WORKSPACE}/h5_vip/' '$i:${data_dir}'"
// }
break
}
}
}
}
}
post {
success {
sh "echo 'Success success'"
}
failure {
sh "echo 'Faild faild'"
}
}
}
```
Binary file added docs/jenkins/imgs/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/jenkins/imgs/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions docs/portainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,19 @@ curl -L https://downloads.portainer.io/ee2-18/portainer-agent-stack.yml -o porta
docker stack deploy -c portainer-agent-stack.yml portainer
```

## v1.22x
## v2.20

新建 `portainer-agent-stack.yml` 文件, 将下面内容复制到配置文件中,你可以从官方仓库拷贝 [`portainer/portainer-compose`](https://github.com/portainer/portainer-compose) 配置。

```yml
version: '3.2'
services:
agent:
image: portainer/agent
environment:
# REQUIRED: Should be equal to the service name prefixed by "tasks." when
# deployed inside an overlay network
AGENT_CLUSTER_ADDR: tasks.agent
# AGENT_PORT: 9001
# LOG_LEVEL: debug
image: portainer/agent:2.20.3-alpine
volumes:
- /var/run/docker.sock:/var/run/docker.sock
#- ${HOME}/.orbstack/run/docker.sock:/var/run/docker.sock
- /var/lib/docker/volumes:/var/lib/docker/volumes
- /etc/localtime:/etc/localtime:ro
networks:
- agent_network
deploy:
Expand All @@ -57,10 +51,12 @@ services:
constraints: [node.platform.os == linux]

portainer:
image: portainer/portainer
image: portainer/portainer-ce:2.20.3-alpine
command: -H tcp://tasks.agent:9001 --tlsskipverify
ports:
- "9443:9443"
- "9000:9000"
- "8000:8000"
volumes:
- portainer_data:/data
- /etc/localtime:/etc/localtime:ro
Expand Down

0 comments on commit e8d279e

Please sign in to comment.