我是 docker 的新手,目前正在尝试使用它来编译一个裸机项目。 Dockerfile 看起来像这样:
FROM ubuntu:22.04 AS base
ARG BUILD_TYPE=Release
ARG BUILD_DIR=build
RUN apt-get update && apt-get install -y \
gdb-multiarch \
wget \
xz-utils \
make cmake ninja-build bzip2 mc file
RUN mkdir /usr/opt/
RUN wget --directory-prefix=/usr/opt/ https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-aarch64-linux.tar.bz2
RUN mkdir -p /usr/opt/gcc-arm-none-eabi
RUN tar -xvjf /usr/opt/gcc-arm-none-eabi-10.3-2021.10-aarch64-linux.tar.bz2 -C /usr/opt/gcc-arm-none-eabi --strip-components=1
RUN ln -s /usr/opt/gcc-arm-none-eabi/bin/arm-none-eabi-gcc /usr/bin/arm-none-eabi-gcc
RUN ln -s /usr/opt/gcc-arm-none-eabi/bin/arm-none-eabi-ar /usr/bin/arm-none-eabi-ar
RUN ln -s /usr/opt/gcc-arm-none-eabi/bin/arm-none-eabi-as /usr/bin/arm-none-eabi-as
RUN ln -s /usr/opt/gcc-arm-none-eabi/bin/arm-none-eabi-ld /usr/bin/arm-none-eabi-ld
RUN ln -s /usr/opt/gcc-arm-none-eabi/bin/arm-none-eabi-objcopy /usr/bin/arm-none-eabi-objcopy
ENV CC="/usr/bin/arm-none-eabi-gcc"
ENV CXX="/usr/bin/arm-none-eabi-gcc"
WORKDIR /home/project
COPY . .
FROM base AS build
RUN cmake -G Ninja -B ./${BUILD_DIR} -S .
RUN cmake --build ./${BUILD_DIR}
FROM scratch AS export-stage
COPY --from=build ./home/project/build/bin .
我通过
构建它DOCKER_BUILDKIT=1 docker build --progress=plain -f Dockerfile --rm -t project:latest . --output ./export/
实际上,这意味着最后,
build/bin
的内容将从 Docker img 复制到我的本地机器。所有这些都非常好用!
我使用 gitlab 进行源代码控制,我想使用
.gitlab-ci.yml
构建我的项目并查看它是否编译,即在 RUN cmake --build ./${BUILD_DIR}
命令后停止。
我试着写一个相应的
.gitlab-ci.yml
文件,即
docker-build:
image: docker:latest
stage: build
services:
- docker:dind
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag=""
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- DOCKER_BUILDKIT=1 docker build --progress=plain -f Dockerfile --rm -t project:latest . --output ./export/
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile
但这每次都会重建整个 Docker-img,而不是仅仅执行
FROM base AS build
RUN cmake -G Ninja -B ./${BUILD_DIR} -S .
RUN cmake --build ./${BUILD_DIR}
如果什么都没有改变,
base
应该被“缓存”...这可以在 gitlab-ci` 中完成,还是我误解了它是如何工作的?