使用 CircleCI 进行自动化构建

来源:OSDev Wiki· 阅读原文 ↗
文章封面
难度级别
Difficulty 2.png
中等

本教程将向你展示如何使用 CircleCI搭建自动化构建环境,它会在每次推送到仓库时自动构建你的项目,并将生成的文件发布到网络上。本教程假设你的内核/操作系统项目托管在 GitHub上,可以从中克隆并使用独立的 GCC Cross-Compiler 工具链进行构建。

在 CircleCI 上设置你的项目

首先你需要在 CircleCI注册。导航到其主页并登录。随后你将可以连接你的 GitHub 账户。GitHub 会询问你是否要授予 CircleCI 访问你账户的权限。确认后,你可以进入 CircleCI 设置。选择一个要构建的项目,以便在每次推送到该项目时启用自动构建。

为工具链添加构建脚本

构建项目的第一步应该是构建工具链,尤其是 GCC Cross-Compiler。最便捷的方式是创建一个小脚本,用于下载 binutils 和 gcc 的源代码(以及可选的其他源代码,例如 newlib 或 gdb),针对你选择的目标进行配置并构建工具。以下脚本是一个示例,它构建的工具链适用于使用 C++ 的一个子集(仅独立部分):

#!/bin/sh
set -e

# Set the versions we will be using.
binutils_version="2.27"
gcc_version="6.3.0"
newlib_version="2.4.0"

# This script expects the target triplet (e.g. i786-pc-elf) as command line argument.
target=$1

# The tools will be installed in ~/cross/$target.
prefix=~/cross/$target

# First check whether the toolchain was already built on a previous run of this script.
if [ ! -d $prefix ]
then
	mkdir -p /tmp/toolchain
	cd /tmp/toolchain

	# Download gcc sources if they are not yet downloaded.
	if [ ! -f gcc-$gcc_version.tar.bz2 ]
	then
		wget -c -O gcc-$gcc_version.tar.bz2 ftp://ftp.gnu.org/gnu/gcc/gcc-$gcc_version/gcc-$gcc_version.tar.bz2
		tar -xf gcc-$gcc_version.tar.bz2
	fi

	# Download binutils sources if they are not yet downloaded.
	if [ ! -f binutils-$binutils_version.tar.bz2 ]
	then
		wget -c -O binutils-$binutils_version.tar.bz2 ftp://ftp.gnu.org/gnu/binutils/binutils-$binutils_version.tar.bz2
		tar -xf binutils-$binutils_version.tar.bz2
	fi

	# Optional: download newlib sources if they are not yet downloaded.
	if [ ! -f newlib-$newlib_version.tar.gz ]
	then
		wget -c -O newlib-$newlib_version.tar.gz ftp://sources.redhat.com/pub/newlib/newlib-$newlib_version.tar.gz
		tar -xf newlib-$newlib_version.tar.gz
	fi

	# Create build paths.
	mkdir -p /tmp/toolchain/build-binutils
	mkdir -p /tmp/toolchain/build-gcc
	mkdir -p /tmp/toolchain/build-newlib

	# Build binutils.
	cd /tmp/toolchain/build-binutils
	sudo rm -rf *
	/tmp/toolchain/binutils-$binutils_version/configure --target=$target --prefix=$prefix --disable-nls 2>&1
	make all 2>&1
	make install 2>&1
	sudo rm -rf *

	# Build gcc and libgcc.
	cd /tmp/toolchain/build-gcc
	/tmp/toolchain/gcc-$gcc_version/configure --target=$target --prefix=$prefix --disable-nls --enable-languages=c,c++ --enable-libstdcxx --without-headers 2>&1
	make all-gcc 2>&1
	make install-gcc 2>&1
	make all-target-libgcc 2>&1
	make install-target-libgcc 2>&1

	# Make sure that our cross compiler will be found by creating links.
	# Alternative: Add the $prefix/bin directory to your $PATH.
	sudo ln -s -f $prefix/bin/* /usr/local/bin/

	# Optional: Build newlib. This is necessary only for the next, also optional build step.
	cd /tmp/toolchain/build-newlib
	sudo rm -rf *
	/tmp/toolchain/newlib-$newlib_version/configure --target=$target --prefix=$prefix 2>&1
	make all 2>&1
	make install 2>&1
	sudo rm -rf *

	# Optional: Build libstdc++. This is done in order to install the freestanding headers for using the C++11, C++14, C++17 standards.
	cd /tmp/toolchain/build-gcc
	/tmp/toolchain/gcc-$gcc_version/configure --target=$target --prefix=$prefix --disable-nls --enable-languages=c,c++ --enable-libstdcxx --without-headers --with-newlib 2>&1
	make all-target-libstdc++-v3 2>&1
	make install-target-libstdc++-v3 2>&1
	sudo rm -rf *
fi

# Also if the cross compiler has not been freshly build, link it so that it will be found.
sudo ln -s -f $prefix/bin/* /usr/local/bin/

请注意,这只是一个示例脚本。你很可能需要根据自己的需求进行修改。此外,你应该在本地测试它是否确实有效,并生成你稍后将使用的编译器。完成后,将其添加到你的源代码仓库中。这里我们假设你已在仓库根目录下创建了一个名为 ci 的目录,并将脚本以名为 toolchain.sh 的文件放在该文件夹中。

设置构建

现在我们需要告诉 CircleCI 在你推送到仓库时应执行哪些操作。默认情况下,CircleCI 使用运行 Ubuntu 14.04 的虚拟机进行构建。这通过仓库根目录下名为 circle.yml 的文件进行配置。以下示例解释了该文件中的各个部分。

dependencies:
    pre:
        # Install a few packages which will be needed for building the cross compiler.
        - sudo apt-get update
        - sudo apt-get install grub-pc
        - sudo apt-get install libgmp3-dev
        - sudo apt-get install libmpfr-dev
        - sudo apt-get install libmpc-dev
        - sudo apt-get install texinfo
        - sudo apt-get install libisl-dev
        - sudo apt-get install libcloog-isl-dev

        # Build the cross compiler for your chosen target (replace i786-pc-elf if necessary).
        - bash ci/toolchain.sh i786-pc-elf

    cache_directories:
        # Add the folder with our cross compiler to the cache. This way it will be kept between builds.
        - ~/cross

compile:
    override:
        # Place here whatever commands are necessary to build your project.
        - bash your_build_script.sh

        # Copy any files you want to keep to the folder $CIRCLE_ARTIFACTS.
        - cp my_kernel my_image.iso $CIRCLE_ARTIFACTS/

test:
    override:
        # Place here any automated test you would like to run after the build (unit tests, boot up a VM and collect output...)
        - bash your_test_suite.sh

        # You must place something here, even it it's just testing whether the output exists.
        - test -f $CIRCLE_ARTIFACTS/my_kernel
        - test -f $CIRCLE_ARTIFACTS/my_image.iso

创建此类自定义文件后,将其保存为仓库根目录下的 circle.yml,提交并推送。现在 CircleCI 应该会开始构建你的交叉编译器,接着构建你的项目,收集输出文件,检查是否成功,并通知你构建是否成功。下次推送时,所有缓存的目录(以及你的交叉编译器)都会在构建开始前恢复(因此你的交叉编译器不会在每次推送时重新构建)。

在 Web 上发布输出

现在,无论是在 CircleCI 上,还是在你 GitHub 项目的提交页面上,你都应该能看到构建是否成功。如果你还想在项目主页上显示构建状态,可以使用以下形式之一的链接:

https://circleci.com/gh/<your_user_name>/<your_project_name>.svg?style=svg
https://circleci.com/gh/<your_user_name>/<your_project_name>.svg?style=shield

你也可以创建指向构建中保存到 $CIRCLE_ARTIFACTS 文件夹的文件的链接。这里的链接应如下所示:

https://circleci.com/api/v1/project/<your_user_name>/<your_project_name>/latest/artifacts/0/$CIRCLE_ARTIFACTS/<your_file_name>

这将始终链接到最新生成的文件。

链接

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:Automated Build Using CircleCI;离线修订号:20119。对应原文版本

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论