#cargo #distribution #packaging #cargo-build

bin+lib cargo-build-dist

一个 cargo 扩展,用于帮助构建各种格式的分发包

1 个不稳定版本

0.1.0 2021年11月30日

#67 in #packaging

MIT/Apache

88KB
2K SLoC

Cargo build-dist

从各种形式的 cargo 套件构建可分发工件。

关于

cargo build-dist 是一个工具,用于构建各种可分发工件,以帮助分发由 cargo 生成的二进制文件。

在其当前形式中,它支持构建和上传 AWS Lambda 包以及 Docker 图像的子集,但将来可能会扩展到其他目标。

除了构建包之外,cargo build-dist 还会考虑套件依赖关系以检测版本更新。当在类似 Legion Labs 维护的单一代码库中工作时,这尤其有用。

如何使用

cargo build-dist 0.1.0
Legion Labs <[email protected]>
Build distributable artifacts from cargo crates.

USAGE:
    cargo-build-dist [FLAGS] [OPTIONS]

FLAGS:
    -d, --debug      Print debug information verbosely
    -n, --dry-run    Do not really push any artifacts
    -f, --force      Push artifacts even if they already exist - this can be dangerous
    -h, --help       Prints help information
        --release    Use release build artifacts
    -V, --version    Prints version information
    -v, --verbose    Print debug information verbosely

OPTIONS:
    -m, --manifest-path <manifest-path>    Path to Cargo.toml

清单语法

可以为项目中任何套件添加分发目标。

根据您的分发类型,有几种配置可供选择

类型 描述
aws-lambda AWS Lambda 包。
docker Docker 镜像。

以下各节描述了每种类型的配置。

依赖项检查

cargo build-dist 将检查套件的依赖项以检测版本更新。

如果在清单中指定了依赖项哈希,则它将与当前的依赖项哈希进行比对。在出现不匹配的情况下,cargo build-dist 将终止执行并通知您可能需要更新版本。要解决冲突,只需使用 cargo build-dist 提供的哈希更新依赖项哈希即可。

[package.metadata.build-dist]
deps_hash = "68e0fa4ba2903f04582cedb135190f6448a36553cb5065cd7031be549b7ca53c"

AWS Lambda

[package.metadata.build-dist.simple-lambda]
type = "aws-lambda"
s3_bucket = "some-s3-bucket" # Required. The AWS S3 bucket to upload the package to. If empty, the value of the `CARGO_BUILD_DIST_AWS_LAMBDA_S3_BUCKET` environment variable will be used.
s3_bucket_prefix = "some/prefix/" # Optional. A prefix to use in the S3 bucket in front of the generated artifacts.
region = "ca-central-1" # Optional. The AWS region to use. Defaults to the region of the AWS CLI.
binary = "my-binary" # Optional. The name of the binary to package for this lambda. Required only if the crate contains more than one binary.
extra_files = [ # A list of extra files to copy into the Docker image.
    { source = "src/test/*", destination = "/usr/src/app/" }
]

这将打包一个 AWS Lambda 并将其推送到指定的 S3 存储桶。

Docker

[package.metadata.build-dist.your-image-name]
type = "docker"
registry = "1234.dkr.ecr.ca-central-1.amazonaws.com" # Required. The registy to push the image to. If empty, the value of the `CARGO_BUILD_DIST_DOCKER_REGISTRY` environment variable will be used.
target_runtime="x86_64-unknown-linux-gnu" # Optional, defaults to "x86_64-unknown-linux-gnu". The target runtime for the generated binaries. You probably don't need to change this.
allow_aws_ecr_creation = true # Optional, defaults to false. Allows the creation of AWS ECR repositories for the image.
target_bin_dir = "/usr/src/app/bin/" # Optional. The target directory in which to place the binaries. Defaults to "/bin".
template = """
FROM ubuntu:20.04
{{ copy_all_binaries }}
{{ copy_all_extra_files }}
CMD [{{ binaries.0 }}]
"""
extra_files = [ # A list of extra files to copy into the Docker image.
    { source = "src/test/*", destination = "/usr/src/app/" }
]

这将生成以下内容的 Dockerfile

FROM ubuntu:20.04
ADD /usr/src/app/bin/simple /usr/src/app/bin/simple
ADD /usr/src/app/ /usr/src/app/
CMD [/usr/src/app/bin/simple]

此图像将具有以下图像名称:1234.dkr.ecr.ca-central-1.amazonaws.com/your-image-name以及您当前的crate版本。

AWS ECR注册表的说明

如果注册表托管在ECR上,工具将自动检测(基于ECR注册表的命名约定),并且如果allow_aws_ecr_creation设置为true,它将确保存在一个AWS ECR存储库来存放该图像。

这要求调用者已设置具有适当权限的AWS凭据。

依赖关系

~91MB
~2M SLoC