## Jenkins 使用 Docker Compose运行
#### Dockerfile
``` Dockerfile
FROM jenkins/jenkins:latest
USER root
RUN sed -i "s/deb.debian.org/ftp.cn.debian.org/g" /etc/apt/sources.list.d/debian.sources && apt-get update
RUN apt-get install python3 python3-pip python3-venv -y
RUN apt-get install nodejs npm -y
RUN npm config set registry http://mirrors.cloud.tencent.com/npm/ && \
npm install -g n && \
export N_NODE_MIRROR=https://mirrors.cloud.tencent.com/nodejs-release/ && \
n 16.20.0
```
#### docker-compose.yaml
``` yaml
version: '3'
services:
jenkins:
# image: jenkins/jenkins:latest
restart: always
image: mgg/jenkins:1.0
ports:
- "8080:8080"
volumes:
- ./jenkins_home:/var/jenkins_home
environment:
TZ: Asia/Shanghai
LANG: C.UTF-8
ssh-agent:
restart: always
image: jenkins/ssh-agent
```
#### 获取Git日志信息,准备自动部署触发自动消息内容Shell
``` Bash
git log --pretty=" - %an, %h, %s" ${GIT_PREVIOUS_SUCCESSFUL_COMMIT}..${GIT_COMMIT}
export MGG_GIT_LOG=$(git log --pretty=" - %an, %h, %s" ${GIT_PREVIOUS_SUCCESSFUL_COMMIT}..${GIT_COMMIT})
export BUILD_USER=$(git show -s --format=%an)
cd /var/jenkins_home/sh/
export BUILD_TIME=$(date "+%Y-%m-%d %H:%M:%S")
env
./send_message ; exit 0
```
#### 企业微信机器人自动发自定义部署任务通知Shell
``` Bash
#!/bin/sh
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx-xxxxx-xxxxx-xxxxx-xxxxx' \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "markdown",
"markdown": {
"content": "<font color=\"warning\">**'"${JOB_NAME}"'部署任务通知**</font> \n
>git URL:<font color=\"comment\">'"${GIT_URL}"'</font>
>构建分⽀:<font color=\"comment\">'"${GIT_BRANCH}"'</font>
>最后提交:<font color=\"comment\">'"${BUILD_USER}"'</font>
>构建时间:<font color=\"comment\">'"${BUILD_TIME}"'</font>
>构建次数:<font color=\"comment\">'"${BUILD_NUMBER}"'</font>
>任务已构建完成
>'"${MGG_GIT_LOG}"'"
}
}'
```
效果:
- [](https://static.adong.wiki/static/images/md/20241018165008.png)
#### cos 上传文件部署示例Shell
``` Bash
npm config set registry http://mirrors.cloud.tencent.com/npm/
npm install
npm run build
python3 -m venv venv && . venv/bin/activate
pip install coscmd
coscmd config -a ${COS_SECRET_ID} -s ${COS_SECRET_KEY} -b bucket-name-10000000 -r ap-guangzhou
coscmd upload -r ./dist/ /
```
#### 流水线(待续)
- ...