【发布时间】:2021-03-07 21:24:14
【问题描述】:
我对 Rust 比较陌生,但我一直在 Docker 容器中工作。下面是我的 dockerfile,效果很好。我的构建使用中间容器在主项目之前构建所有货物容器。除非我更新依赖项,否则项目会在本地快速构建。即使重建了依赖项,在我的旧 macbook pro 上最多也不会花费超过 10 分钟。
FROM ekidd/rust-musl-builder as builder
WORKDIR /home/rust/
# Avoid having to install/build all dependencies by copying
# the Cargo files and making a dummy src/main.rs
COPY Cargo.toml .
COPY Cargo.lock .
RUN echo "fn main() {}" > src/main.rs
RUN cargo test
RUN cargo build --release
# We need to touch our real main.rs file or else docker will use
# the cached one.
COPY . .
RUN sudo touch src/main.rs
RUN cargo test
RUN cargo build --release
# Size optimization
RUN strip target/x86_64-unknown-linux-musl/release/project-name
# Start building the final image
FROM scratch
WORKDIR /home/rust/
COPY --from=builder /home/rust/target/x86_64-unknown-linux-musl/release/project-name .
ENTRYPOINT ["./project-name"]
但是,当我将我的项目设置为通过 google cloud build 从 github repo 自动构建时,我震惊地发现构建需要将近 45 分钟!我想如果我为中间容器正确设置了缓存,至少可以节省一些时间。即使构建器成功提取了缓存的图像,它似乎也没有使用它,并且总是从头开始构建中间容器。这是我的 cloudbuild.yaml:
steps:
- name: gcr.io/cloud-builders/docker
args:
- "-c"
- >-
docker pull $_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest
|| exit 0
id: Pull
entrypoint: bash
- name: gcr.io/cloud-builders/docker
args:
- build
- "-t"
- "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
- "--cache-from"
- "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
- .
- "-f"
- Dockerfile
id: Build
- name: gcr.io/cloud-builders/docker
args:
- push
- "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
id: Push
- name: gcr.io/google.com/cloudsdktool/cloud-sdk
args:
- run
- services
- update
- $_SERVICE_NAME
- "--platform=managed"
- "--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
- >-
--labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID,$_LABELS
- "--region=$_DEPLOY_REGION"
- "--quiet"
id: Deploy
entrypoint: gcloud
timeout: 3600s
images:
- "$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_SERVICE_NAME:latest"
options:
substitutionOption: ALLOW_LOOSE
我正在寻找有关我在 cloudbuild.yaml 中做错了什么的任何信息,以及考虑到本地速度如此之快如何加快我的云构建的提示。理想情况下,我想坚持使用谷歌云,但如果有另一个 CI 服务可以更好地处理这样的 rust/docker 构建,我会愿意切换。
【问题讨论】:
标签: docker google-cloud-platform rust continuous-integration