Skip to content

Commit 47edf25

Browse files
author
libin
committed
加入 BEFORE_FUNC和AFTER_FUNC 方法
1 parent 6262897 commit 47edf25

File tree

5 files changed

+95
-40
lines changed

5 files changed

+95
-40
lines changed

.github/deploy.example.yml renamed to .github/workflows/deploy.example.yml

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ on:
44
push:
55
branches:
66
- main
7-
- master
7+
- feature/before_after_func
8+
# pull_request:
9+
# branches:
10+
# - main
811

912
env:
10-
# Docker 账户
11-
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
13+
DOCKER_USERNAME: ${{ env.DOCKER_USERNAME }}
1214
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
1315
DOCKER_REGISTRY_URL: "registry.cn-hangzhou.aliyuncs.com" # 示例:阿里云私有镜像仓库
14-
# Docker 镜像地址
15-
DOCKER_IMAGE: "registry.cn-hangzhou.aliyuncs.com/example_namespace/example_image"
16+
DOCKER_IMAGE: "registry.cn-hangzhou.aliyuncs.com/example-namespace/example-deployments"
1617
DOCKER_IMAGE_TAG: ""
17-
# Docker 容器启动配置
18-
CONTAINER_NAME: "example_container"
19-
DOCKER_RUN_PARAMS: "-p 91:80 -e APP_ENV=example"
20-
# 服务器
21-
SERVER_HOST: ${{ secrets.SERVER_HOST }}
22-
SERVER_USER: ${{ secrets.SERVER_USER }}
18+
CONTAINER_NAME: "example-deployments"
19+
DOCKER_RUN_PARAMS: "-p 88:80 -e APP_ENV=example"
20+
SERVER_HOST: ${{ env.SERVER_HOST }}
21+
SERVER_USER: ${{ env.SERVER_USER }}
2322
SERVER_SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_PRIVATE_KEY }}
2423

2524
jobs:
2625
CI-Build:
2726
runs-on: ubuntu-latest
28-
2927
steps:
3028
- name: Checkout code
3129
uses: actions/checkout@v4
@@ -41,33 +39,38 @@ jobs:
4139
- name: Set up Docker Buildx
4240
uses: docker/setup-buildx-action@v3
4341

44-
- name: Run PHP syntax check
45-
run: find . -type f -name "*.php" -exec php -l {} \; | tee php-syntax-check.log
46-
47-
- name: Build Docker image
48-
run: |
49-
docker build -t ${{ env.DOCKER_IMAGE }} .
50-
5142
- name: Login to Docker Hub
5243
uses: docker/login-action@v3
5344
with:
5445
username: ${{ env.DOCKER_USERNAME }}
5546
password: ${{ env.DOCKER_PASSWORD }}
5647
registry: ${{ env.DOCKER_REGISTRY_URL }} # 私有镜像仓库,需要配置此地址
5748

58-
- name: Push Docker image
59-
run: |
60-
docker tag ${{ env.DOCKER_IMAGE }} ${{ env.DOCKER_IMAGE }}:latest
61-
docker push ${{ env.DOCKER_IMAGE }}:latest
49+
- name: Build and push Docker image
50+
uses: docker/build-push-action@v6
51+
with:
52+
context: .
53+
file: ./tests/Dockerfile
54+
push: true
55+
tags: ${{ env.DOCKER_IMAGE }}:latest
6256

6357

6458
CD-Deploy:
6559
needs: CI-Build
6660
runs-on: ubuntu-latest
67-
6861
steps:
6962
- name: Deploy to server
7063
run: |
64+
export BEFORE_FUNC=$(cat <<'EOF'
65+
echo "I am before function"
66+
EOF
67+
)
68+
69+
export AFTER_FUNC=$(cat <<'EOF'
70+
echo "I am after function"
71+
EOF
72+
)
73+
7174
curl -o deploy.sh https://raw.githubusercontent.com/jefferyjob/deployments/refs/heads/main/scripts/deploy.docker.sh
7275
chmod +x deploy.sh
7376
./deploy.sh key deploy

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
test: ## 语法错误检测
44
shellcheck -e SC2046 -e SC2317 scripts/*.sh
55

6+
.PHONY:docker
7+
docker: ## 测试Docker容器服务
8+
docker build -f tests/Dockerfile -t example-deployments .
9+
docker stop example-deployments || true
10+
docker rm example-deployments || true
11+
docker run -d -p 8080:80 --name example-deployments example-deployments
612

713
.PHONY:help
814
.DEFAULT_GOAL:=help

README.cn.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,30 @@
1717
## 使用方法
1818

1919
### 环境变量配置
20-
| 变量名 | 是否必须 | 描述 |
21-
|------------------------|-----|-----------------------------------------------------------------------------------|
22-
| DOCKER_IMAGE || Docker 镜像地址,包括镜像名称和标签(如:`example_namespace/myapp`),用于拉取并启动指定的应用容器。 |
23-
| CONTAINER_NAME || Docker 容器名称(如:`my_container`),在 Docker 中唯一标识该容器,确保不与其他容器冲突。 |
24-
| SERVER_HOST || 远程服务器的主机名或 IP 地址(如:`192.168.1.100`),用于通过 SSH 连接到目标服务器。 |
25-
| SERVER_USER || 服务器登录用户名(如:`root`),确保该用户具备操作 Docker 的权限。 |
26-
| DOCKER_USERNAME || Docker 仓库的登录账号(如:`mydockeruser`),用于从私有镜像仓库中拉取镜像。 |
27-
| DOCKER_PASSWORD || Docker 仓库的登录密码(如:`secret_password`),配合 `DOCKER_USERNAME` 使用,用于私有镜像的认证。 |
28-
| DOCKER_REGISTRY_URL || Docker 仓库的 URL 地址(如:`https://index.docker.io/v1`), 若为空,则默认使用 Docker Hub。 |
29-
| DOCKER_IMAGE_TAG || 要部署的镜像版本标签(如:`latest`),默认为 `latest`,用于指定 Docker 镜像的版本。 |
30-
| DOCKER_RUN_PARAMS || 启动容器时传递的额外运行参数(如:`-e ENV=prod`),可以包括环境变量、端口映射等。 |
31-
| SERVER_PASSWORD || 服务器登录密码(如:`mypassword`),仅在 `AUTH_METHOD``pwd` 时使用。 |
20+
| 变量名 | 是否必须 | 描述 |
21+
|------------------------|-----|------------------------------------------------|
22+
| DOCKER_IMAGE || Docker 镜像地址,包括镜像名称和标签(如:`example_namespace/myapp`),用于拉取并启动指定的应用容器。 |
23+
| CONTAINER_NAME || Docker 容器名称(如:`my_container`),在 Docker 中唯一标识该容器,确保不与其他容器冲突。 |
24+
| SERVER_HOST || 远程服务器的主机名或 IP 地址(如:`192.168.1.100`),用于通过 SSH 连接到目标服务器。 |
25+
| SERVER_USER || 服务器登录用户名(如:`root`),确保该用户具备操作 Docker 的权限。 |
26+
| DOCKER_USERNAME || Docker 仓库的登录账号(如:`mydockeruser`),用于从私有镜像仓库中拉取镜像。 |
27+
| DOCKER_PASSWORD || Docker 仓库的登录密码(如:`secret_password`),配合 `DOCKER_USERNAME` 使用,用于私有镜像的认证。 |
28+
| DOCKER_REGISTRY_URL || Docker 仓库的 URL 地址(如:`https://index.docker.io/v1`), 若为空,则默认使用 Docker Hub。 |
29+
| DOCKER_IMAGE_TAG || 要部署的镜像版本标签(如:`latest`),默认为 `latest`,用于指定 Docker 镜像的版本。 |
30+
| DOCKER_RUN_PARAMS || 启动容器时传递的额外运行参数(如:`-e ENV=prod`),可以包括环境变量、端口映射等。 |
31+
| SERVER_PASSWORD || 服务器登录密码(如:`mypassword`),仅在 `AUTH_METHOD``pwd` 时使用。 |
3232
| SERVER_SSH_PRIVATE_KEY || SSH 私钥内容(如:`-----BEGIN PRIVATE KEY-----`),用于密钥登录服务器,仅在 `AUTH_METHOD``key` 时使用。 |
33+
| BEFORE_FUNC || 部署前运行脚本 |
34+
| AFTER_FUNC || 部署后运行脚本 |
3335

36+
`BEFORE_FUNC``AFTER_FUNC` 示例代码
37+
38+
```bash
39+
BEFORE_FUNC=$(cat <<'EOF'
40+
echo "I am before function"
41+
EOF
42+
)
43+
```
3444

3545
### 脚本运行
3646
**方法1: 不下载直接运行部署脚本**

scripts/deploy.docker.sh

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ print_env() {
5050
echo " CONTAINER_NAME: $CONTAINER_NAME"
5151
echo " DOCKER_RUN_PARAMS: $DOCKER_RUN_PARAMS"
5252
echo "--------------------------------------------------------------------------"
53+
[[ -n "$BEFORE_FUNC" ]] && echo " BEFORE_FUNC: ******"
54+
[[ -n "$AFTER_FUNC" ]] && echo " AFTER_FUNC: ******"
55+
echo "--------------------------------------------------------------------------"
5356
echo " AUTH_METHOD: $AUTH_METHOD"
5457
echo " ACTION: $ACTION"
5558
echo "--------------------------------------------------------------------------"
@@ -160,11 +163,11 @@ deploy_pwd_server() {
160163

161164
# 远程服务器上执行的部署逻辑
162165
deploy_server() {
163-
# 确保脚本遇到错误时退出
164-
set -e
166+
set -e # 确保脚本遇到错误时退出
167+
sudo -i # 切换到root用户
165168

166-
# 切换到root用户
167-
sudo -i
169+
# 部署前运行脚本
170+
deploy_before_func
168171

169172
# 备份现有的容器和镜像
170173
deploy_backup_container
@@ -179,6 +182,9 @@ deploy_server() {
179182

180183
# 如果部署成功,删除备份镜像并清理系统
181184
deploy_cleanup
185+
186+
# 部署后运行脚本
187+
deploy_after_func
182188
}
183189

184190
# 读取并导出所需的环境变量
@@ -191,12 +197,15 @@ export_env_vars() {
191197
export DOCKER_IMAGE_TAG='$DOCKER_IMAGE_TAG'; \
192198
export CONTAINER_NAME='$CONTAINER_NAME'; \
193199
export DOCKER_RUN_PARAMS='$DOCKER_RUN_PARAMS'; \
200+
export BEFORE_FUNC='$BEFORE_FUNC'; \
201+
export AFTER_FUNC='$AFTER_FUNC'; \
194202
"
195203
}
196204

197205
# 登陆Docker镜像仓库
198206
deploy_login_docker() {
199207
if [[ -z "$DOCKER_REGISTRY_URL" ]]; then
208+
echo "未配置 DOCKER_REGISTRY_URL 跳过登陆Docker镜像仓库"
200209
return
201210
fi
202211

@@ -210,6 +219,7 @@ deploy_login_docker() {
210219
# 退出登陆Docker镜像仓库
211220
deploy_logout_docker() {
212221
if [[ -z "$DOCKER_REGISTRY_URL" ]]; then
222+
echo "未配置 DOCKER_REGISTRY_URL 跳过退出登陆Docker镜像仓库"
213223
return
214224
fi
215225

@@ -293,6 +303,25 @@ deploy_cleanup() {
293303
sudo docker system prune -f || true
294304
}
295305

306+
deploy_before_func() {
307+
if [[ -z "$BEFORE_FUNC" ]]; then
308+
echo "未配置 BEFORE_FUNC 方法,跳过执行"
309+
return
310+
fi
311+
312+
echo "准备运行 BEFORE_FUN 方法..."
313+
$BEFORE_FUNC
314+
}
315+
316+
deploy_after_func() {
317+
if [[ -z "$AFTER_FUNC" ]]; then
318+
echo "未配置 AFTER_FUNC 方法,跳过执行"
319+
return
320+
fi
321+
322+
echo "准备运行 AFTER_FUNC 方法..."
323+
$AFTER_FUNC
324+
}
296325

297326
######################################################################
298327
# Docker 服务部署

tests/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM registry.cn-hangzhou.aliyuncs.com/jefferyjob/nginx:latest
2+
3+
WORKDIR /usr/share/nginx/html
4+
RUN echo '<!DOCTYPE html><html><head><title>Hello Jefferyjob</title></head><body><h1>Hello Jefferyjob/deployments</h1></body></html>' > index.html
5+
EXPOSE 80
6+
7+
CMD ["nginx", "-g", "daemon off;"]

0 commit comments

Comments
 (0)