Github 构建后使用 gzip 的操作

Github Actions with gzip after build

我一直在尝试找到一种方法,在 github 操作中构建后对我的构建文件进行 gzip 压缩。我的构建不会自动执行此操作,而且我不确定应该在何时何地压缩它。

我的操作有一个构建项目的有效 运行 命令,但是压缩要么失败,要么根本不起作用,具体取决于我如何 运行 它。

这是我最近尝试的方式:

- name: Build
  run: |
    npm ci
    npm run build --prod --aot

- name: GZIP
  run: |
    npm install gzip-cli
    gzip ./dist/*.js -k -9

我确定这里的答案是 "you can't do that" 或 "you can't do that",但我不知道从这里去哪里。我的服务器本身不压缩文件,我不确定如何让 github 压缩文件。我需要所有 js 个文件才能包含 gz 个文件。

我试过使用 gzippergzip-cli,控制台通常输出这样的:

  npm install gzipper
  gzipper --exclude jpg,jpeg,png,svg ./dist
  shell: /bin/bash -e {0}
npm WARN karma-jasmine-html-reporter@1.5.1 requires a peer of jasmine-core@>=3.5 but none is installed. You must install peer dependencies yourself.

+ gzipper@3.4.2
updated 1 package and audited 19029 packages in 11.676s

33 packages are looking for funding
  run `npm fund` for details

found 2 high severity vulnerabilities
  run `npm audit fix` to fix them, or `npm audit` for details
/home/runner/work/_temp/96763ca6-0048-4812-a8bb-72bf33d14fcc.sh: line 2: gzipper: command not found
##[error]Process completed with exit code 127.

看起来安装得很好,但随后显示找不到命令(无论是 gzipper 还是 gzip-cli)。

如果我使用普通的旧 gzip,我不会收到任何错误。它 运行s,但实际上并没有压缩任何东西。会不会是github在上传文件之前暂时将文件存储在某个地方?

使用您当前用于安装 gzip-cligzipper 的方法,它们将保存到当前项目的 node_modules 文件夹中。它们的可执行文件也将安装在 node_modules/.bin 中(可以通过 运行 npm bin 找到)。但是,您的项目依赖可执行文件通常在 PATH 环境变量中不可用,除非您将它们全局安装。

来自docs for the bin field for a dependency's package.json:

On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

因此,您应该:

  • 使用 -g 标志全局安装依赖项。如果npm的.bin目录包含在PATH中,你可以轻松执行命令。

  • 运行命令后附加$(npm bin)/的命令:

    $(npm bin)/gzip ./dist/*.js -k -9
    
  • 或者加一个script that does the same thing. Typically npm scripts will have dependency executables available to them in the PATH environment variable:

    {
      "scripts: {
        "gzip-files": "gzip ./dist/*.js -k -9"
      },
      "dependencies": {
        "gzip-cli": "/* version range */"
      }
    }