Logofpack

Hooks

Run your own scripts before and after a build or a package: upload, checks, notifications.

钩子

在构建或打包前后运行你自己的脚本:上传、检查、通知。

Hooks are shell commands fpack runs at fixed points. Use them to generate code before building, check artifact sizes, upload to a store or a server, or send a notification.

hooks:
  pre_build:
    - dart run build_runner build --delete-conflicting-outputs
  pre_package:
    apk: ['./scripts/check_size.sh']
  post_package:
    dmg: ['./scripts/upload.sh "$FPACK_ARTIFACT"']
  post_build:
    - ./scripts/notify.sh

When each hook runs#

HookWhenNotes
hooks.pre_build once, before the first flutter build a failure stops the run
hooks.pre_package.<target> before a target's packaging steps sets FPACK_TARGET
hooks.post_package.<target> after a target's artifacts exist sets FPACK_ARTIFACT (the first file) and FPACK_ARTIFACTS
hooks.post_build once, at the end sets FPACK_ARTIFACTS (one per line) and FPACK_SUCCESS ( 1 / 0 ). It also runs after build failures, but not when every target already failed its checks (a missing tool, an unknown flavor)

A failing hook means exit code 1.

Environment#

Hooks run in the project root through sh -c (Windows: cmd /S /C, so quoted paths work as typed) with these variables:

VariableValue
FPACK_PROJECT_ROOTthe project directory
FPACK_OUTPUT_DIRthe artifact directory
FPACK_VERSION, FPACK_BUILD_NUMBER the version name and build number
FPACK_MODE, FPACK_FLAVORbuild mode and flavor
FPACK_TARGET the target (pre_package / post_package)
FPACK_ARTIFACT, FPACK_ARTIFACTS the artifact(s) (post_package, post_build)
FPACK_SUCCESS 1 or 0 (post_build)

Write $VAR (Windows cmd: %VAR%) for variables that exist only while the hook runs. ${VAR} is expanded earlier, when fpack.yaml is loaded (Secrets and variables).

Examples#

Upload a signed IPA after packaging:

hooks:
  post_package:
    ipa: ['xcrun altool --upload-app -f "$FPACK_ARTIFACT" -t ios --apiKey "$ASC_KEY_ID" --apiIssuer "$ASC_ISSUER"']

Copy everything to a release server, once, after the last target:

hooks:
  post_build:
    - 'test "$FPACK_SUCCESS" = 1 && rsync -a "$FPACK_OUTPUT_DIR/" releases@example.com:/srv/app/'

Fail the build when the APK is too large:

hooks:
  pre_package: { apk: ['./scripts/check_apk_size.sh'] }

Next#

Many targets, one command.

钩子是 fpack 在固定时刻运行的 shell 命令。可以用它在构建前生成代码、检查产物大小、上传到商店或服务器,或者发送通知。

hooks:
  pre_build:
    - dart run build_runner build --delete-conflicting-outputs
  pre_package:
    apk: ['./scripts/check_size.sh']
  post_package:
    dmg: ['./scripts/upload.sh "$FPACK_ARTIFACT"']
  post_build:
    - ./scripts/notify.sh

各钩子何时运行#

钩子时机说明
hooks.pre_build第一次 flutter 构建之前,运行一次失败会中止整个运行
hooks.pre_package.<目标> 某个目标的打包步骤之前 设置 FPACK_TARGET
hooks.post_package.<目标> 某个目标的产物生成之后 设置 FPACK_ARTIFACT(第一个文件)和 FPACK_ARTIFACTS
hooks.post_build 最后运行一次 设置 FPACK_ARTIFACTS (每行一个)和 FPACK_SUCCESS ( 1 / 0 )。构建失败后也会运行,但所有目标都在检查阶段就失败时(缺少工具、未知 flavor)不会运行

钩子失败意味着退出码 1。

环境#

钩子在项目根目录通过 sh -c 运行(Windows 是 cmd /S /C,所以带引号的路径照常使用),并带有这些变量:

变量值
FPACK_PROJECT_ROOT项目目录
FPACK_OUTPUT_DIR产物目录
FPACK_VERSION、FPACK_BUILD_NUMBER版本名和构建号
FPACK_MODE、FPACK_FLAVOR构建模式和 flavor
FPACK_TARGET目标(pre_package / post_package)
FPACK_ARTIFACT、FPACK_ARTIFACTS 产物(post_package、post_build)
FPACK_SUCCESS1 或 0(post_build)

仅在钩子运行期间才存在的变量,写成 $VAR(Windows 的 cmd 里是 %VAR%)。${VAR} 在更早的时候,也就是加载 fpack.yaml 时展开(密钥与环境变量)。

示例#

打包后上传已签名的 IPA:

hooks:
  post_package:
    ipa: ['xcrun altool --upload-app -f "$FPACK_ARTIFACT" -t ios --apiKey "$ASC_KEY_ID" --apiIssuer "$ASC_ISSUER"']

在最后一个目标之后,一次性把所有文件复制到发布服务器:

hooks:
  post_build:
    - 'test "$FPACK_SUCCESS" = 1 && rsync -a "$FPACK_OUTPUT_DIR/" releases@example.com:/srv/app/'

APK 太大时让构建失败:

hooks:
  pre_package: { apk: ['./scripts/check_apk_size.sh'] }

下一步#

多目标一条命令。