@wjwjq/release-helper 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,15 @@
4
4
 
5
5
  服务器端安装和更新,请看发布后文档
6
6
 
7
- ## 初始化
7
+ ## 安装
8
+
9
+ ```bash
10
+ pnpm add -D @wjwjq/release-helper
11
+ ```
12
+
13
+ ## 使用
14
+
15
+ ### 初始化
8
16
 
9
17
  生成.release目录及相关配置文件环境
10
18
 
@@ -12,7 +20,7 @@
12
20
  release-helper init
13
21
  ```
14
22
 
15
- ## 打包
23
+ ### 打包
16
24
 
17
25
  仅打包成xx.tar.gz 需指定release.conf.yaml中assetsDir字段
18
26
 
@@ -20,7 +28,7 @@ release-helper init
20
28
  release-helper pack
21
29
  ```
22
30
 
23
- ## 发版
31
+ ### 发版
24
32
 
25
33
  发布git release相关版本; 需指定release.conf.yaml中host等相关字段
26
34
 
@@ -1,6 +1,52 @@
1
- # .release 目录说明
1
+ # release-helper
2
2
 
3
- ## doc 目录
3
+ 打包 和发布辅助工具, 集成nginx二进制文件,支持supervisor和systemctl启动安装模式
4
+
5
+ 服务器端安装和更新,请看发布后文档
6
+
7
+ ## 使用
8
+
9
+ ### 推荐
10
+
11
+ 在package.json中添加如下命令, 后续通过pnpm pak/pub/publish使用
12
+
13
+ ```bash
14
+ "pak": "release-helper pack",
15
+ "pub": "release-helper publish",
16
+ "release": "release-helper release",
17
+ ```
18
+
19
+ ### 单独使用命令
20
+
21
+ 请在项目根目录,使用命令
22
+
23
+ #### 初始化
24
+
25
+ 常规来讲,通过pnpm安装时,会自动在项目根目录位置,生成.release目录,若未生成,请手动生成
26
+
27
+ ```bash
28
+ release-helper init
29
+ ```
30
+
31
+ #### 打包
32
+
33
+ 仅打包成xx.tar.gz 需指定release.conf.yaml中assetsDir字段
34
+
35
+ ```bash
36
+ release-helper pack
37
+ ```
38
+
39
+ #### 发版
40
+
41
+ 发布git release相关版本; 需指定release.conf.yaml中host等相关字段
42
+
43
+ ```bash
44
+ release-helper release
45
+ ```
46
+
47
+ ## .release 目录说明
48
+
49
+ ### doc 目录
4
50
 
5
51
  用于release时附带的文档,文档中可以使用指定变量占位,后续执行release-helper release时会自动遍历,并替换所有变量
6
52
 
@@ -12,7 +58,7 @@
12
58
  | \$VERSION\$ | 用户指定的版本号 |
13
59
  | \$PKG_NAME\$ | tar.gz包名称(值为\$APP_NAME\$\_\$VERSION\$.tar.gz) |
14
60
 
15
- ## nginx 目录
61
+ ### nginx 目录
16
62
 
17
63
  用于存放服务器端nginx的启动配置文件等,部分占位说明及占位符请勿删除,会在服务器端安装后更新时,由脚本自动替换
18
64
 
@@ -20,4 +66,8 @@
20
66
  | **文件或目录** | **说明** |
21
67
  | -------------- | --------------------------------------------------------- |
22
68
  | ca | nginx https使用到的证书,变更时需对应修改nginx.conf中的值 |
23
- | nginx.conf | nginx启动时 需要的配置文件 |
69
+ | nginx.conf | nginx启动时 需要的配置文件 |
70
+
71
+ ### release.conf.yaml
72
+
73
+ 打包发布相关配置项
@@ -1,9 +1,12 @@
1
-
2
- #gitlab 仓库地址
3
- host: your git repo url
4
- # gitlab rest api token
5
- token: your git token
1
+ #gitlab 仓库地址 your git repo url
2
+ host: ''
3
+ # gitlab rest api token your git token
4
+ token: ''
6
5
  # build script default: npm run build
7
6
  buildCmd: "npm run build"
8
7
  # 打包后资源 相对工作区所在路径 默认dist
9
- assetsDir: string
8
+ assetsDir: 'dist'
9
+
10
+ # nginx和assets 所属用户和用户组
11
+ user: root
12
+ userGroup: root
@@ -81,11 +81,7 @@ install() {
81
81
 
82
82
  tar -zxvhf "$pkg" -C "$install_path" --strip-components 1
83
83
 
84
- mkdir -p /var/log/nginx
85
-
86
- if [[ -z "${prefix}" ]]; then
87
- cp "$install_path"/sbin/nginx /usr/sbin/
88
- fi
84
+ cp "$install_path"nginx /usr/sbin/
89
85
 
90
86
  # 检测是否安装成功
91
87
  if [[ $? == 0 ]]; then
@@ -5,8 +5,8 @@ Wants=network-online.target
5
5
 
6
6
  [Service]
7
7
  # 应用启动使用的用户及用户组,需各组件手动修改
8
- User=root
9
- Group=root
8
+ User=_user_
9
+ Group=_user_group_
10
10
 
11
11
  Environment="LOGPATH=_log_path_" "LOGFILE=_log_file_" "LOGROTATEDELAY=2592000000" "CONFS=_conf_path_" "VERSION=_version_"
12
12
 
@@ -22,10 +22,9 @@ WorkingDirectory=/etc/nginx
22
22
  [Service]
23
23
  Type=forking
24
24
  PIDFile=_pid_file_
25
- ExecStartPre=_start_check_
26
25
  ExecStart=_exec_cmd_
27
- ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
28
- ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
26
+ ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat _pid_file_)"
27
+ ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat _pid_file_)"
29
28
 
30
29
  PrivateTmp=true
31
30
 
@@ -7,6 +7,8 @@ app_name=__APP_NAME__
7
7
 
8
8
  # 静态资源存放目录 eg: /opt/posidon-frontend/
9
9
  install_path=__INSTALL_PATH__
10
+ user=__USER__
11
+ usergroup=__USER_GROUP__
10
12
 
11
13
  execute_log_path="/var/log/${app_name}_script_execute_logs"
12
14
 
@@ -48,8 +50,8 @@ source "$basepath"/nginx.sh
48
50
 
49
51
  # 创建用户
50
52
  create_user_csri() {
51
- user="csri"
52
- group="csri"
53
+ user=$user
54
+ group=$usergroup
53
55
 
54
56
  #create group if not exists
55
57
  grep "^$group" /etc/group >&/dev/null
@@ -64,23 +66,6 @@ create_user_csri() {
64
66
  fi
65
67
  }
66
68
 
67
- find_nginx_pos() {
68
- confpath=''
69
- find / -name "nginx" -print0 >tmpfile
70
- while IFS= read -r -d $'\0'; do
71
- if [ -d "$REPLY/conf" ]; then # $prefix/etc/nginx
72
- confpath="$REPLY/conf.d/"
73
- break
74
- elif [ -d "$REPLY/conf.d" ]; then # /etc/nginx
75
- confpath="$REPLY/conf.d/"
76
- break
77
- fi
78
- done <tmpfile
79
- rm -f tmpfile
80
-
81
- echo "$confpath"
82
- }
83
-
84
69
  has_nginx_installed=1
85
70
 
86
71
  # 检查 nginx 是否已安装
@@ -145,18 +130,6 @@ install_assets() {
145
130
 
146
131
  log_success "$install_path"assets/settings 已覆盖,请注意对比差异
147
132
 
148
- # 修改所属用户
149
- if [[ "$i(stat -c '%U' ${install_path})" != 'csri' ]]; then
150
- chown -R csri:csri "$install_path"
151
- fi
152
-
153
- (chmod 644 -R "$install_path")
154
-
155
- if [[ $? == 0 ]]; then
156
- log_success "posidon-frontend deployed successfully"
157
- else
158
- log_error "posidon-frontend deployment failed"
159
- fi
160
133
  }
161
134
 
162
135
  start() {
@@ -180,4 +153,14 @@ start() {
180
153
  copy_nginx_conf_and_write_verison
181
154
  add_nginx_service
182
155
  install_assets
156
+
157
+ # 修改所属用户
158
+ chown -R $user:$usergroup "$install_path"
159
+ chmod 744 -R "$install_path"
160
+
161
+ if [[ $? == 0 ]]; then
162
+ log_success "posidon-frontend deployed successfully"
163
+ else
164
+ log_error "posidon-frontend deployment failed"
165
+ fi
183
166
  }
@@ -7,7 +7,7 @@ nginx_conf_install_path="$prefix/opt/${app_name}/nginx/"
7
7
  pkg_path="${project_path}/pkg"
8
8
 
9
9
  # 配置文件所在路径
10
- new_nginx_conf_path="${pkg_path}/nginx/nginx.conf"
10
+ new_nginx_conf_path="${pkg_path}/nginx"
11
11
 
12
12
  # nginx服务配置文件路径
13
13
  nginx_conf_path="${nginx_conf_install_path}nginx.conf"
@@ -15,20 +15,31 @@ nginx_conf_path="${nginx_conf_install_path}nginx.conf"
15
15
  # asset_path="$prefix/opt/posidon-frontend/"
16
16
  nginx_log_path="/var/log/nginx"
17
17
  nginx_log_file="${app_name}.nginx.log"
18
- nginx_pid_path="/var/run/${app_name}.nginx.pid"
18
+ nginx_pid_dir="/var/run/nginx/"
19
+ nginx_pid_file_path="$nginx_pid_dir${app_name}.nginx.pid"
19
20
 
20
21
  conf_version=$(awk '{print $1}' "${pkg_path}"/version)
21
22
  echo "$conf_version"
22
23
 
24
+
25
+
23
26
  copy_nginx_conf_and_write_verison() {
24
27
  mkdir -p "$nginx_conf_install_path"
28
+ # 创建pid目录
29
+ mkdir -p $nginx_pid_dir
30
+ chown -R $user:$usergroup $nginx_pid_dir
31
+ chmod 744 -R $nginx_pid_dir
32
+
33
+ # 创建日志目录
34
+ mkdir -p $nginx_log_path
35
+ chown -R $user:$usergroup $nginx_log_path
36
+ chmod 744 -R $nginx_log_path
25
37
 
26
38
  access_log_super="access_log /dev/stdout main"
27
39
  error_log_super="error_log /dev/stderr warn"
28
40
  access_log="access_log $nginx_log_path/$nginx_log_file main"
29
41
  error_log="error_log $nginx_log_path/$nginx_log_file"
30
-
31
- pid_file="pid $nginx_pid_path"
42
+ pid_file="pid $nginx_pid_file_path"
32
43
 
33
44
  exec=$(get_exec_path)
34
45
 
@@ -46,59 +57,62 @@ copy_nginx_conf_and_write_verison() {
46
57
  #nginx -v nginx代码内部使用的stderr输出, 此处需要转换为stdout
47
58
  nginx_version=$($exec -v 2>&1)
48
59
 
49
- cp "$new_nginx_conf_path" ./nginx.conf
60
+ tmp_file='./nginx/nginx.conf'
61
+ cp -rf "$new_nginx_conf_path" ./
50
62
 
51
- sed -i "1i\# ${nginx_version}" ./nginx.conf
52
- sed -i "1i\# version: ${conf_version}" ./nginx.conf
63
+ sed -i "1i\# ${nginx_version}" $tmp_file
64
+ sed -i "1i\# version: ${conf_version}" $tmp_file
53
65
 
54
66
  if [[ -n "${prefix}" ]]; then
55
- n=$(grep -wn "mime.types" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
56
- sed -i "${n}c include conf/mime.types;" ./nginx.conf
67
+ n=$(grep -wn "mime.types" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
68
+ sed -i "${n}c include conf/mime.types;" $tmp_file
57
69
  fi
58
70
 
59
- # n=$(grep -wn "__root__" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
71
+ # n=$(grep -wn "__root__" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
60
72
  # if [[ -n "${n}" ]]; then
61
- # sed -i "${n}c root ${asset_path};" ./nginx.conf
73
+ # sed -i "${n}c root ${asset_path};" $tmp_file
62
74
  # fi
63
75
 
64
76
  # 替换日志输出路径方式
65
- n=$(grep -wn "#ERROR_LOG_PLACEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
77
+ n=$(grep -wn "#ERROR_LOG_PLACEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
66
78
  if [[ -n "${n}" ]]; then
67
- if ((mode == 2)); then
68
- sed -i "${n}c ${error_log};" ./nginx.conf
69
- else
70
- sed -i "${n}c ${error_log_super};" ./nginx.conf
71
- fi
79
+ sed -i "${n}c ${error_log};" $tmp_file
80
+ # if ((mode == 2)); then
81
+ # sed -i "${n}c ${error_log};" $tmp_file
82
+ # else
83
+ # sed -i "${n}c ${error_log_super};" $tmp_file
84
+ # fi
72
85
  fi
73
86
 
74
- n=$(grep -wn "#ACCESS_LOG_PLACEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
87
+ n=$(grep -wn "#ACCESS_LOG_PLACEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
75
88
  if [[ -n "${n}" ]]; then
76
- if ((mode == 2)); then
77
- sed -i "${n}c ${access_log};" ./nginx.conf
78
- else
79
- sed -i "${n}c ${access_log_super};" ./nginx.conf
80
- fi
89
+ sed -i "${n}c ${access_log};" $tmp_file
90
+ # if ((mode == 2)); then
91
+ # sed -i "${n}c ${access_log};" $tmp_file
92
+ # else
93
+ # sed -i "${n}c ${access_log_super};" $tmp_file
94
+ # fi
81
95
  fi
82
96
 
83
- n=$(grep -wn "#PID_FILE_PALCEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
97
+ n=$(grep -wn "#PID_FILE_PALCEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
84
98
  if [[ -n "${n}" ]]; then
99
+ sed -i "${n}c ${pid_file};" $tmp_file
85
100
  # 非supervisorctl
86
- if ((mode == 2)); then
87
- sed -i "${n}c ${pid_file};" ./nginx.conf
88
- fi
101
+ # if ((mode == 2)); then
102
+ # sed -i "${n}c ${pid_file};" $tmp_file
103
+ # fi
89
104
  fi
90
105
 
91
- n=$(grep -wn "daemon off;" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
106
+ n=$(grep -wn "daemon off;" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
92
107
  if [[ -n "${n}" ]]; then
93
108
  # 非supervisorctl
94
109
  if ((mode == 2)); then
95
- sed -i "${n}c daemon on;" ./nginx.conf
110
+ sed -i "${n}c daemon on;" $tmp_file
96
111
  fi
97
112
  fi
98
113
 
99
- mv -fb "./nginx.conf" "$nginx_conf_install_path"
100
-
101
- cp -fR "$pkg_path"/nginx/ca "$nginx_conf_install_path"
114
+ # 拷贝整个nginx目录
115
+ mv -fb ./nginx/* "$nginx_conf_install_path"
102
116
 
103
117
  mkdir -p /data/command/
104
118
  log "nginx start command: $(get_exec_cmd)"
@@ -130,19 +144,21 @@ get_exec_cmd() {
130
144
 
131
145
  add_nginx_service() {
132
146
  #添加到服务
133
- exec_cmd=$(get_exec_cmd)
147
+ exec_cmd="$(get_exec_cmd) -e $nginx_log_path/$nginx_log_file"
134
148
  cp "$pkg_path"/nginx/nginx.service.tpl ./nginx.service
135
149
 
136
- check_cmd="$exec -t -c $nginx_conf_path"
150
+ check_cmd="$exec -t -c $nginx_conf_path -e $nginx_log_path/$nginx_log_file"
137
151
  version=$(echo "$conf_version" | grep -oP '\d+\.\d+\.\d+')
138
152
 
153
+ sed -i "s#_user_group_#$usergroup#g" ./nginx.service
154
+ sed -i "s#_user_#$user#g" ./nginx.service
139
155
  sed -i "s#_exec_cmd_#$exec_cmd #g" ./nginx.service
140
156
  sed -i "s#_log_path_#$nginx_log_path#g" ./nginx.service
141
157
  sed -i "s#_log_file_#$nginx_log_file#g" ./nginx.service
142
158
  sed -i "s#_conf_path_#$nginx_conf_path#g" ./nginx.service
143
159
  sed -i "s#_version_#$version#g" ./nginx.service
144
- sed -i "s#_pid_file_#$nginx_pid_path#g" ./nginx.service
145
- sed -i "s#_start_check_#$check_cmd#g" ./nginx.service
160
+ sed -i "s#_pid_file_#$nginx_pid_file_path#g" ./nginx.service
161
+ #sed -i "s#_start_check_#$check_cmd#g" ./nginx.service
146
162
 
147
163
  if ((mode == 2)); then
148
164
  # systemctl enable nginx
package/dist/pack.js CHANGED
@@ -40,12 +40,14 @@ async function pack(version) {
40
40
  fs.writeFileSync(path.resolve(nginxDir, "version"), version, { encoding: "utf-8" });
41
41
  logger.info(pc.green(`version: ${version} added`));
42
42
  const assetInstallPath = `/opt/${projectName}/`;
43
- logger.info(pc.green(`start to replace __APP_NAME__ in common.sh`));
43
+ logger.info(pc.green(`start to replace variants in common.sh`));
44
44
  replaceFileContent({
45
45
  filePath: path.resolve(projectDir, "script", "common.sh"),
46
46
  replaced: [
47
47
  ["__APP_NAME__", `"${projectName}"`],
48
- ["__INSTALL_PATH__", assetInstallPath]
48
+ ["__INSTALL_PATH__", assetInstallPath],
49
+ ["__USER__", releaseConf.user],
50
+ ["__USER_GROUP__", releaseConf.userGroup]
49
51
  ]
50
52
  });
51
53
  logger.info(pc.green(`start to replace version: ${version} in index.html`));
@@ -57,7 +59,7 @@ async function pack(version) {
57
59
  fs.cpSync(path.resolve(__releaseDir, "nginx"), nginxDir, { recursive: true, force: true });
58
60
  replaceFileContent({
59
61
  filePath: path.resolve(nginxDir, "nginx.conf"),
60
- replaced: [["__root__", `${assetInstallPath}/assets/`]]
62
+ replaced: [["__root__", `${assetInstallPath}assets/`]]
61
63
  // writeToNewFilePath: path.resolve(nginxDir, `${projectName}.conf`)
62
64
  });
63
65
  logger.info(pc.green(`start to generate ${zipFileName}`));
package/dist/prepare.js CHANGED
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
7
7
  import { logger } from './logger.js';
8
8
  import 'picocolors';
9
9
 
10
- const __work_dir = process.cwd();
10
+ const __work_dir = process.env.INIT_CWD || process.cwd();
11
11
  const __releaseDir = path.resolve(__work_dir, ".release");
12
12
  const __filename = fileURLToPath(import.meta.url);
13
13
  const __dirname = dirname(__filename);
@@ -22,10 +22,14 @@ function parseConf(file) {
22
22
  }
23
23
  }
24
24
  const releaseConf = Object.assign({
25
+ host: "",
26
+ token: "",
25
27
  /** 打包脚本 默认npm run build */
26
28
  buildCmd: "npm run build",
27
29
  /** 打包后资源 相对工作区所在路径 默认dist/*/
28
- assetsDir: "dist"
30
+ assetsDir: "dist",
31
+ user: "root",
32
+ userGroup: "root"
29
33
  }, parseConf(__releaseConfPath));
30
34
  const $ = execa({ encoding: "utf8" });
31
35
  async function prepare() {
@@ -61,14 +65,11 @@ async function checkEnvInfo() {
61
65
  logger.error(`.release\u76EE\u5F55\u4E0B\u4E0D\u5B58\u5728${releaseConfFileName}\u6587\u4EF6\uFF0C\u8BF7\u4F7F\u7528 release-helper init \u521D\u59CB\u5316\u751F\u6210`);
62
66
  process.exit(1);
63
67
  }
64
- const { host, token } = releaseConf;
65
- if (!host) {
66
- logger.error(`The host field is required in the configuration file!`);
67
- process.exit(1);
68
- }
69
- if (!token) {
70
- logger.error(`The token field is required in the configuration file!`);
71
- process.exit(1);
68
+ for (let [field, value] of Object.entries(releaseConf)) {
69
+ if (!value) {
70
+ logger.error(`The field: [${field}] is required in .release/release.conf.yaml!`);
71
+ process.exit(1);
72
+ }
72
73
  }
73
74
  }
74
75
 
@@ -14,6 +14,6 @@ import 'node:url';
14
14
  await prepare();
15
15
  logger.info("done");
16
16
  } catch (error) {
17
- console.error(`release-helper error: `, e);
17
+ console.error(`release-helper error: `, error);
18
18
  }
19
19
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wjwjq/release-helper",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "generate deployment package for frontend, include nginx...",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -1,6 +1,52 @@
1
- # .release 目录说明
1
+ # release-helper
2
2
 
3
- ## doc 目录
3
+ 打包 和发布辅助工具, 集成nginx二进制文件,支持supervisor和systemctl启动安装模式
4
+
5
+ 服务器端安装和更新,请看发布后文档
6
+
7
+ ## 使用
8
+
9
+ ### 推荐
10
+
11
+ 在package.json中添加如下命令, 后续通过pnpm pak/pub/publish使用
12
+
13
+ ```bash
14
+ "pak": "release-helper pack",
15
+ "pub": "release-helper publish",
16
+ "release": "release-helper release",
17
+ ```
18
+
19
+ ### 单独使用命令
20
+
21
+ 请在项目根目录,使用命令
22
+
23
+ #### 初始化
24
+
25
+ 常规来讲,通过pnpm安装时,会自动在项目根目录位置,生成.release目录,若未生成,请手动生成
26
+
27
+ ```bash
28
+ release-helper init
29
+ ```
30
+
31
+ #### 打包
32
+
33
+ 仅打包成xx.tar.gz 需指定release.conf.yaml中assetsDir字段
34
+
35
+ ```bash
36
+ release-helper pack
37
+ ```
38
+
39
+ #### 发版
40
+
41
+ 发布git release相关版本; 需指定release.conf.yaml中host等相关字段
42
+
43
+ ```bash
44
+ release-helper release
45
+ ```
46
+
47
+ ## .release 目录说明
48
+
49
+ ### doc 目录
4
50
 
5
51
  用于release时附带的文档,文档中可以使用指定变量占位,后续执行release-helper release时会自动遍历,并替换所有变量
6
52
 
@@ -12,7 +58,7 @@
12
58
  | \$VERSION\$ | 用户指定的版本号 |
13
59
  | \$PKG_NAME\$ | tar.gz包名称(值为\$APP_NAME\$\_\$VERSION\$.tar.gz) |
14
60
 
15
- ## nginx 目录
61
+ ### nginx 目录
16
62
 
17
63
  用于存放服务器端nginx的启动配置文件等,部分占位说明及占位符请勿删除,会在服务器端安装后更新时,由脚本自动替换
18
64
 
@@ -20,4 +66,8 @@
20
66
  | **文件或目录** | **说明** |
21
67
  | -------------- | --------------------------------------------------------- |
22
68
  | ca | nginx https使用到的证书,变更时需对应修改nginx.conf中的值 |
23
- | nginx.conf | nginx启动时 需要的配置文件 |
69
+ | nginx.conf | nginx启动时 需要的配置文件 |
70
+
71
+ ### release.conf.yaml
72
+
73
+ 打包发布相关配置项
@@ -1,9 +1,12 @@
1
-
2
- #gitlab 仓库地址
3
- host: your git repo url
4
- # gitlab rest api token
5
- token: your git token
1
+ #gitlab 仓库地址 your git repo url
2
+ host: ''
3
+ # gitlab rest api token your git token
4
+ token: ''
6
5
  # build script default: npm run build
7
6
  buildCmd: "npm run build"
8
7
  # 打包后资源 相对工作区所在路径 默认dist
9
- assetsDir: string
8
+ assetsDir: 'dist'
9
+
10
+ # nginx和assets 所属用户和用户组
11
+ user: root
12
+ userGroup: root
@@ -81,11 +81,7 @@ install() {
81
81
 
82
82
  tar -zxvhf "$pkg" -C "$install_path" --strip-components 1
83
83
 
84
- mkdir -p /var/log/nginx
85
-
86
- if [[ -z "${prefix}" ]]; then
87
- cp "$install_path"/sbin/nginx /usr/sbin/
88
- fi
84
+ cp "$install_path"nginx /usr/sbin/
89
85
 
90
86
  # 检测是否安装成功
91
87
  if [[ $? == 0 ]]; then
@@ -5,8 +5,8 @@ Wants=network-online.target
5
5
 
6
6
  [Service]
7
7
  # 应用启动使用的用户及用户组,需各组件手动修改
8
- User=root
9
- Group=root
8
+ User=_user_
9
+ Group=_user_group_
10
10
 
11
11
  Environment="LOGPATH=_log_path_" "LOGFILE=_log_file_" "LOGROTATEDELAY=2592000000" "CONFS=_conf_path_" "VERSION=_version_"
12
12
 
@@ -22,10 +22,9 @@ WorkingDirectory=/etc/nginx
22
22
  [Service]
23
23
  Type=forking
24
24
  PIDFile=_pid_file_
25
- ExecStartPre=_start_check_
26
25
  ExecStart=_exec_cmd_
27
- ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
28
- ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
26
+ ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat _pid_file_)"
27
+ ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat _pid_file_)"
29
28
 
30
29
  PrivateTmp=true
31
30
 
@@ -7,6 +7,8 @@ app_name=__APP_NAME__
7
7
 
8
8
  # 静态资源存放目录 eg: /opt/posidon-frontend/
9
9
  install_path=__INSTALL_PATH__
10
+ user=__USER__
11
+ usergroup=__USER_GROUP__
10
12
 
11
13
  execute_log_path="/var/log/${app_name}_script_execute_logs"
12
14
 
@@ -48,8 +50,8 @@ source "$basepath"/nginx.sh
48
50
 
49
51
  # 创建用户
50
52
  create_user_csri() {
51
- user="csri"
52
- group="csri"
53
+ user=$user
54
+ group=$usergroup
53
55
 
54
56
  #create group if not exists
55
57
  grep "^$group" /etc/group >&/dev/null
@@ -64,23 +66,6 @@ create_user_csri() {
64
66
  fi
65
67
  }
66
68
 
67
- find_nginx_pos() {
68
- confpath=''
69
- find / -name "nginx" -print0 >tmpfile
70
- while IFS= read -r -d $'\0'; do
71
- if [ -d "$REPLY/conf" ]; then # $prefix/etc/nginx
72
- confpath="$REPLY/conf.d/"
73
- break
74
- elif [ -d "$REPLY/conf.d" ]; then # /etc/nginx
75
- confpath="$REPLY/conf.d/"
76
- break
77
- fi
78
- done <tmpfile
79
- rm -f tmpfile
80
-
81
- echo "$confpath"
82
- }
83
-
84
69
  has_nginx_installed=1
85
70
 
86
71
  # 检查 nginx 是否已安装
@@ -145,18 +130,6 @@ install_assets() {
145
130
 
146
131
  log_success "$install_path"assets/settings 已覆盖,请注意对比差异
147
132
 
148
- # 修改所属用户
149
- if [[ "$i(stat -c '%U' ${install_path})" != 'csri' ]]; then
150
- chown -R csri:csri "$install_path"
151
- fi
152
-
153
- (chmod 644 -R "$install_path")
154
-
155
- if [[ $? == 0 ]]; then
156
- log_success "posidon-frontend deployed successfully"
157
- else
158
- log_error "posidon-frontend deployment failed"
159
- fi
160
133
  }
161
134
 
162
135
  start() {
@@ -180,4 +153,14 @@ start() {
180
153
  copy_nginx_conf_and_write_verison
181
154
  add_nginx_service
182
155
  install_assets
156
+
157
+ # 修改所属用户
158
+ chown -R $user:$usergroup "$install_path"
159
+ chmod 744 -R "$install_path"
160
+
161
+ if [[ $? == 0 ]]; then
162
+ log_success "posidon-frontend deployed successfully"
163
+ else
164
+ log_error "posidon-frontend deployment failed"
165
+ fi
183
166
  }
@@ -7,7 +7,7 @@ nginx_conf_install_path="$prefix/opt/${app_name}/nginx/"
7
7
  pkg_path="${project_path}/pkg"
8
8
 
9
9
  # 配置文件所在路径
10
- new_nginx_conf_path="${pkg_path}/nginx/nginx.conf"
10
+ new_nginx_conf_path="${pkg_path}/nginx"
11
11
 
12
12
  # nginx服务配置文件路径
13
13
  nginx_conf_path="${nginx_conf_install_path}nginx.conf"
@@ -15,20 +15,31 @@ nginx_conf_path="${nginx_conf_install_path}nginx.conf"
15
15
  # asset_path="$prefix/opt/posidon-frontend/"
16
16
  nginx_log_path="/var/log/nginx"
17
17
  nginx_log_file="${app_name}.nginx.log"
18
- nginx_pid_path="/var/run/${app_name}.nginx.pid"
18
+ nginx_pid_dir="/var/run/nginx/"
19
+ nginx_pid_file_path="$nginx_pid_dir${app_name}.nginx.pid"
19
20
 
20
21
  conf_version=$(awk '{print $1}' "${pkg_path}"/version)
21
22
  echo "$conf_version"
22
23
 
24
+
25
+
23
26
  copy_nginx_conf_and_write_verison() {
24
27
  mkdir -p "$nginx_conf_install_path"
28
+ # 创建pid目录
29
+ mkdir -p $nginx_pid_dir
30
+ chown -R $user:$usergroup $nginx_pid_dir
31
+ chmod 744 -R $nginx_pid_dir
32
+
33
+ # 创建日志目录
34
+ mkdir -p $nginx_log_path
35
+ chown -R $user:$usergroup $nginx_log_path
36
+ chmod 744 -R $nginx_log_path
25
37
 
26
38
  access_log_super="access_log /dev/stdout main"
27
39
  error_log_super="error_log /dev/stderr warn"
28
40
  access_log="access_log $nginx_log_path/$nginx_log_file main"
29
41
  error_log="error_log $nginx_log_path/$nginx_log_file"
30
-
31
- pid_file="pid $nginx_pid_path"
42
+ pid_file="pid $nginx_pid_file_path"
32
43
 
33
44
  exec=$(get_exec_path)
34
45
 
@@ -46,59 +57,62 @@ copy_nginx_conf_and_write_verison() {
46
57
  #nginx -v nginx代码内部使用的stderr输出, 此处需要转换为stdout
47
58
  nginx_version=$($exec -v 2>&1)
48
59
 
49
- cp "$new_nginx_conf_path" ./nginx.conf
60
+ tmp_file='./nginx/nginx.conf'
61
+ cp -rf "$new_nginx_conf_path" ./
50
62
 
51
- sed -i "1i\# ${nginx_version}" ./nginx.conf
52
- sed -i "1i\# version: ${conf_version}" ./nginx.conf
63
+ sed -i "1i\# ${nginx_version}" $tmp_file
64
+ sed -i "1i\# version: ${conf_version}" $tmp_file
53
65
 
54
66
  if [[ -n "${prefix}" ]]; then
55
- n=$(grep -wn "mime.types" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
56
- sed -i "${n}c include conf/mime.types;" ./nginx.conf
67
+ n=$(grep -wn "mime.types" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
68
+ sed -i "${n}c include conf/mime.types;" $tmp_file
57
69
  fi
58
70
 
59
- # n=$(grep -wn "__root__" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
71
+ # n=$(grep -wn "__root__" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
60
72
  # if [[ -n "${n}" ]]; then
61
- # sed -i "${n}c root ${asset_path};" ./nginx.conf
73
+ # sed -i "${n}c root ${asset_path};" $tmp_file
62
74
  # fi
63
75
 
64
76
  # 替换日志输出路径方式
65
- n=$(grep -wn "#ERROR_LOG_PLACEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
77
+ n=$(grep -wn "#ERROR_LOG_PLACEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
66
78
  if [[ -n "${n}" ]]; then
67
- if ((mode == 2)); then
68
- sed -i "${n}c ${error_log};" ./nginx.conf
69
- else
70
- sed -i "${n}c ${error_log_super};" ./nginx.conf
71
- fi
79
+ sed -i "${n}c ${error_log};" $tmp_file
80
+ # if ((mode == 2)); then
81
+ # sed -i "${n}c ${error_log};" $tmp_file
82
+ # else
83
+ # sed -i "${n}c ${error_log_super};" $tmp_file
84
+ # fi
72
85
  fi
73
86
 
74
- n=$(grep -wn "#ACCESS_LOG_PLACEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
87
+ n=$(grep -wn "#ACCESS_LOG_PLACEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
75
88
  if [[ -n "${n}" ]]; then
76
- if ((mode == 2)); then
77
- sed -i "${n}c ${access_log};" ./nginx.conf
78
- else
79
- sed -i "${n}c ${access_log_super};" ./nginx.conf
80
- fi
89
+ sed -i "${n}c ${access_log};" $tmp_file
90
+ # if ((mode == 2)); then
91
+ # sed -i "${n}c ${access_log};" $tmp_file
92
+ # else
93
+ # sed -i "${n}c ${access_log_super};" $tmp_file
94
+ # fi
81
95
  fi
82
96
 
83
- n=$(grep -wn "#PID_FILE_PALCEHOLDER" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
97
+ n=$(grep -wn "#PID_FILE_PALCEHOLDER" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
84
98
  if [[ -n "${n}" ]]; then
99
+ sed -i "${n}c ${pid_file};" $tmp_file
85
100
  # 非supervisorctl
86
- if ((mode == 2)); then
87
- sed -i "${n}c ${pid_file};" ./nginx.conf
88
- fi
101
+ # if ((mode == 2)); then
102
+ # sed -i "${n}c ${pid_file};" $tmp_file
103
+ # fi
89
104
  fi
90
105
 
91
- n=$(grep -wn "daemon off;" ./nginx.conf | awk -F: '{print $1}' | sed -n 1p)
106
+ n=$(grep -wn "daemon off;" $tmp_file | awk -F: '{print $1}' | sed -n 1p)
92
107
  if [[ -n "${n}" ]]; then
93
108
  # 非supervisorctl
94
109
  if ((mode == 2)); then
95
- sed -i "${n}c daemon on;" ./nginx.conf
110
+ sed -i "${n}c daemon on;" $tmp_file
96
111
  fi
97
112
  fi
98
113
 
99
- mv -fb "./nginx.conf" "$nginx_conf_install_path"
100
-
101
- cp -fR "$pkg_path"/nginx/ca "$nginx_conf_install_path"
114
+ # 拷贝整个nginx目录
115
+ mv -fb ./nginx/* "$nginx_conf_install_path"
102
116
 
103
117
  mkdir -p /data/command/
104
118
  log "nginx start command: $(get_exec_cmd)"
@@ -130,19 +144,21 @@ get_exec_cmd() {
130
144
 
131
145
  add_nginx_service() {
132
146
  #添加到服务
133
- exec_cmd=$(get_exec_cmd)
147
+ exec_cmd="$(get_exec_cmd) -e $nginx_log_path/$nginx_log_file"
134
148
  cp "$pkg_path"/nginx/nginx.service.tpl ./nginx.service
135
149
 
136
- check_cmd="$exec -t -c $nginx_conf_path"
150
+ check_cmd="$exec -t -c $nginx_conf_path -e $nginx_log_path/$nginx_log_file"
137
151
  version=$(echo "$conf_version" | grep -oP '\d+\.\d+\.\d+')
138
152
 
153
+ sed -i "s#_user_group_#$usergroup#g" ./nginx.service
154
+ sed -i "s#_user_#$user#g" ./nginx.service
139
155
  sed -i "s#_exec_cmd_#$exec_cmd #g" ./nginx.service
140
156
  sed -i "s#_log_path_#$nginx_log_path#g" ./nginx.service
141
157
  sed -i "s#_log_file_#$nginx_log_file#g" ./nginx.service
142
158
  sed -i "s#_conf_path_#$nginx_conf_path#g" ./nginx.service
143
159
  sed -i "s#_version_#$version#g" ./nginx.service
144
- sed -i "s#_pid_file_#$nginx_pid_path#g" ./nginx.service
145
- sed -i "s#_start_check_#$check_cmd#g" ./nginx.service
160
+ sed -i "s#_pid_file_#$nginx_pid_file_path#g" ./nginx.service
161
+ #sed -i "s#_start_check_#$check_cmd#g" ./nginx.service
146
162
 
147
163
  if ((mode == 2)); then
148
164
  # systemctl enable nginx
package/src/pack.ts CHANGED
@@ -53,14 +53,16 @@ export async function pack(version?: string) {
53
53
  logger.info(pc.green(`version: ${version} added`))
54
54
 
55
55
  const assetInstallPath = `/opt/${projectName}/`
56
- logger.info(pc.green(`start to replace __APP_NAME__ in common.sh`))
56
+ logger.info(pc.green(`start to replace variants in common.sh`))
57
57
 
58
58
  // 替换安装脚本 与nginx匹配的名称
59
59
  replaceFileContent({
60
60
  filePath: path.resolve(projectDir, 'script', 'common.sh'),
61
61
  replaced: [
62
62
  ['__APP_NAME__', `"${projectName}"`],
63
- ['__INSTALL_PATH__', assetInstallPath]
63
+ ['__INSTALL_PATH__', assetInstallPath],
64
+ ['__USER__', releaseConf.user],
65
+ ['__USER_GROUP__', releaseConf.userGroup],
64
66
  ]
65
67
  })
66
68
 
@@ -79,7 +81,7 @@ export async function pack(version?: string) {
79
81
 
80
82
  replaceFileContent({
81
83
  filePath: path.resolve(nginxDir, 'nginx.conf'),
82
- replaced: [['__root__', `${assetInstallPath}/assets/`]],
84
+ replaced: [['__root__', `${assetInstallPath}assets/`]],
83
85
  // writeToNewFilePath: path.resolve(nginxDir, `${projectName}.conf`)
84
86
  })
85
87
 
package/src/prepare.ts CHANGED
@@ -17,9 +17,13 @@ export type ReleaseConf = {
17
17
  buildCmd?: string
18
18
  /** 打包后资源 相对工作区所在路径 默认dist/*/
19
19
  assetsDir?: string
20
+ /** nginx和assets 所属用户和用户组 默认: root*/
21
+ user: string
22
+ userGroup: string
20
23
  }
21
24
 
22
- export const __work_dir = process.cwd();
25
+ // 获取项目根目录
26
+ export const __work_dir = process.env.INIT_CWD || process.cwd();
23
27
  export const __releaseDir = path.resolve(__work_dir, '.release')
24
28
 
25
29
  const __filename = fileURLToPath(import.meta.url);
@@ -38,10 +42,14 @@ function parseConf(file: string) {
38
42
  }
39
43
 
40
44
  export const releaseConf = Object.assign({
45
+ host: '',
46
+ token: '',
41
47
  /** 打包脚本 默认npm run build */
42
48
  buildCmd: 'npm run build',
43
49
  /** 打包后资源 相对工作区所在路径 默认dist/*/
44
- assetsDir: 'dist'
50
+ assetsDir: 'dist',
51
+ user: 'root',
52
+ userGroup: 'root'
45
53
  }, parseConf(__releaseConfPath));
46
54
 
47
55
  export const $ = execa({ encoding: 'utf8' });
@@ -91,15 +99,11 @@ export async function checkEnvInfo() {
91
99
  process.exit(1);
92
100
  }
93
101
 
94
- const { host, token } = releaseConf;
95
- if (!host) {
96
- logger.error(`The host field is required in the configuration file!`);
97
- process.exit(1);
98
- }
99
-
100
- if (!token) {
101
- logger.error(`The token field is required in the configuration file!`);
102
- process.exit(1);
102
+ for(let [field, value] of Object.entries(releaseConf)){
103
+ if (!value) {
104
+ logger.error(`The field: [${field}] is required in .release/release.conf.yaml!`);
105
+ process.exit(1);
106
+ }
103
107
  }
104
108
  }
105
109
 
@@ -8,6 +8,6 @@ import { prepare } from "./prepare";
8
8
  await prepare();
9
9
  logger.info('done')
10
10
  } catch (error) {
11
- console.error(`release-helper error: `, e)
11
+ console.error(`release-helper error: `, error)
12
12
  }
13
13
  })()