jenkins + gogs + docker + nginx 完成自动化部署
app安装部署
1.docker-compose配置文件
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
depends_on:
- jenkins
- gogs
restart: unless-stopped
dind:
image: docker:dind
container_name: dind
environment:
- DOCKER_TLS_CERTDIR= # 禁用 TLS,以简化配置
- DOCKER_OPTS="--insecure-registry registry:5000"
privileged: true # 允许 DinD 运行特权容器
volumes:
- /etc/docker/daemon.json:/etc/docker/daemon.json
gogs:
image: gogs/gogs
container_name: gogs
ports:
- "3000:3000" # HTTP service for Gogs
environment:
- ROOT_URL=http://localhost/gogs/
- APP_NAME=Gogs
- TZ=Asia/Shanghai # 设置时区, 根据需要调整
volumes:
- gogs_data:/data # 数据卷映射, 请根据实际情况修改路径
restart: unless-stopped
jenkins:
image: 'jenkins/jenkins:lts'
ports:
- '8080:8080'
environment:
- TZ=Asia/Shanghai # 设置时区, 根据需要调整
- JENKINS_OPTS=--prefix=/jenkins
- DOCKER_HOST=tcp://dind:2375 # 指定 Jenkins 使用的 Docker 主机
volumes:
- jenkins_home:/var/jenkins_home
- /usr/bin/docker:/usr/bin/docker
restart: unless-stopped # 容器开机自启动
depends_on:
- dind
registry:
image: registry:2
ports:
- "5000:5000"
volumes:
- registry_data:/var/lib/registry
networks:
deploy_net:
driver: bridge
volumes:
gogs_data:
driver: local
jenkins_home:
driver: local
registry_data:
driver: local
2.nginx配置文件
events { }
http {
upstream gogs {
server gogs:3000;
}
upstream jenkins {
server jenkins:8080;
}
server {
listen 80;
location /gogs/ {
proxy_pass http://gogs/gogs/;
rewrite ^/gogs(.*)$ $1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# 修改HTML响应中的相对路径以适应新的URL路径
sub_filter 'href="/' 'href="/gogs/';
sub_filter 'src="/' 'src="/gogs/';
sub_filter_once off;
}
location /jenkins/ {
proxy_pass http://jenkins/jenkins/; # 添加前缀
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
自动化流程
1.创建一个git仓库
1.创建一个git仓库,新建一个分支
2.编写Jenkinsfile 在根目录,如修改,怎幺在jenkins中指定路径
pipeline {
agent any
environment {
// 定义源Docker镜像的名称和标签(例如来自Docker Hub)
SOURCE_DOCKER_REGISTRY = 'docker.1ms.run' // 私有仓库地址
SOURCE_DOCKER_IMAGE = 'nginx'
SOURCE_DOCKER_TAG = 'latest'
// 定义目标Docker镜像的名称和标签(在你的私有仓库中)
TARGET_DOCKER_IMAGE = 'my-nginx'
TARGET_DOCKER_TAG = 'latest'
// 定义私有Docker仓库的URL
PRIVATE_DOCKER_REGISTRY = 'registry:5000'
}
stages {
stage('Print Hello World') {
steps {
echo 'Hello World 你好世界'
}
}
stage('Initialize') {
steps {
script {
// 检查 Docker 版本
sh 'docker --version'
}
}}
stage('Pull and Tag Docker Image1') {
steps {
script {
try {
// 使用Docker Pipeline插件拉取公共Docker仓库中的镜像
docker.image("${env.SOURCE_DOCKER_REGISTRY}/${SOURCE_DOCKER_IMAGE}:${SOURCE_DOCKER_TAG}").pull()
// 给镜像打上私有仓库的标签
// docker.image("${SOURCE_DOCKER_IMAGE}:${SOURCE_DOCKER_TAG}").tag("${PRIVATE_DOCKER_REGISTRY}/${TARGET_DOCKER_IMAGE}:${TARGET_DOCKER_TAG}")
sh "docker tag ${SOURCE_DOCKER_REGISTRY}/${SOURCE_DOCKER_IMAGE}:${SOURCE_DOCKER_TAG} ${PRIVATE_DOCKER_REGISTRY}/${TARGET_DOCKER_IMAGE}:${TARGET_DOCKER_TAG}"
echo "Successfully tagged ${SOURCE_DOCKER_IMAGE}:${SOURCE_DOCKER_TAG} as ${PRIVATE_DOCKER_REGISTRY}/${TARGET_DOCKER_IMAGE}:${TARGET_DOCKER_TAG}"
echo "Image pulled and tagged successfully."
} catch (err) {
echo "An error occurred during pull and tag operations: ${err.message}"
throw err
}
}
}
}
stage('Push Docker Image1') {
steps {
script {
try {
// 推送Docker镜像到私有仓库
docker.withRegistry("http://${PRIVATE_DOCKER_REGISTRY}", '') {
docker.image("${PRIVATE_DOCKER_REGISTRY}/${TARGET_DOCKER_IMAGE}:${TARGET_DOCKER_TAG}").push()
}
echo "Image pushed successfully."
} catch (err) {
echo "An error occurred during push operation: ${err.message}"
throw err
}
}
}
}
}
}
2.配置jenkins自动化
1.登录jenkins - 在初始化阶段,可以安装一些默认的插件
http://localhost/jenkins/
2.下载基本所需的插件
Dashboard -> Manage Jenkins ->Plugins -> available plugins
Docker Pipeline / Docker plugin / Git plugin / Pipeline: Groovy
3.构建一个自动化任务
Dashboar -> New Item -> name + pipeline -> configure + Pipeline + Pipeline script from SCM
指定git + 添加git仓库的地址 + 添加凭证(用户密码 / ssh密钥对)指定分支
4.在git仓库的指定分支中编写jenkinsfile
5.执行
Dashboar ->name -> Build Now
标签:nginx,gogs,SOURCE,jenkins,DOCKER,Docker,docker
From: https://www.cnblogs.com/wykang/p/18679536