From 537743d9c7c534a60fc3c8d53e2c73245206040d Mon Sep 17 00:00:00 2001 From: Hashem Hashem Date: Mon, 4 Nov 2024 13:12:11 +0100 Subject: [PATCH] feat(ci): build docker image modify build script to accomodate --- .github/workflows/ci.yaml | 37 +++++++++++++++- meson.build | 12 +++--- scripts/build-ros-gateway.sh | 82 +++++++++++++++++------------------- 3 files changed, 80 insertions(+), 51 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 656b27a..70a2bbd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -6,7 +6,7 @@ on: pull_request: jobs: - build: + build-firmware: runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 @@ -44,3 +44,38 @@ jobs: - name: Build documentation run: meson compile -C build docs + + build-gateway: + strategy: + matrix: + ros_distro: [humble, jazzy] + + runs-on: ubuntu-24.04 + + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - uses: actions/checkout@v4 + + - uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Set up builder outside of script to use this action's built-in caching + - uses: docker/setup-buildx-action@v3 + id: builder + with: + platforms: linux/amd64,linux/arm64 + + - name: Build docker image + run: ./scripts/build-ros-gateway.sh + env: + ROS_DISTRO: ${{ matrix.ROS_DISTRO }} + PLATFORMS: ${{ steps.builder.outputs.platforms }} + DOCKER_BUILDER: ${{ steps.builder.outputs.name }} diff --git a/meson.build b/meson.build index 527ed22..dd33e68 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project( 'Rover', 'c', - meson_version: '>=1.2.0', + meson_version: '>=1.5.2', version: '0.1', default_options: [ 'optimization=2', @@ -121,12 +121,10 @@ ros_gateway_dockerfile = files('ros-gateway' / 'Dockerfile') run_target( 'ros-gateway', - command: [ - ros_gateway_build_script, - '--rover-root', - meson.project_source_root(), - '--dockerfile', - ros_gateway_dockerfile, + command: [ros_gateway_build_script], + env: [ + 'BUILD_CONTEXT=' + meson.project_source_root(), + 'DOCKERFILE=' + ros_gateway_dockerfile[0].full_path(), ], ) diff --git a/scripts/build-ros-gateway.sh b/scripts/build-ros-gateway.sh index 969209a..ce8b05e 100755 --- a/scripts/build-ros-gateway.sh +++ b/scripts/build-ros-gateway.sh @@ -10,12 +10,18 @@ $(basename "$0") [--help] [--rover-root DIR] [--dockerfile FILE] [--push DEST] Build the ros-gateway docker image. Requires docker to be installed and usable without sudo. +args: --help show this help - --rover-root DIR path to rover repository (default: .) - --dockerfile FILE path to ros-gateway dockerfile (default: ros-gateway/Dockerfile) - --version-tag version to tag with (default: latest) --push push containers +default env vars: + ROS_DISTRO=jazzy + PLATFORMS=linux/amd64,linux/arm64 + PACKAGE_BASENAME=ghcr.io/canedudev/rover/ros-gateway + BUILDER=ced-rover-builder + BUILD_CONTEXT=. + DOCKERFILE=./ros-gateway/Dockerfile + VERSION_TAG=latest EOF } @@ -25,10 +31,6 @@ if ! command -v docker >/dev/null; then exit 1 fi -ROVER_ROOT=. -DOCKERFILE=ros-gateway/Dockerfile -VERSION=latest - while [[ $# -gt 0 ]]; do case "$1" in -h) ;& @@ -37,26 +39,10 @@ while [[ $# -gt 0 ]]; do exit 0 ;; - --rover-root) - ROVER_ROOT="$2" - shift 2 - ;; - - --dockerfile) - DOCKERFILE="$2" - shift 2 - ;; - - --version-tag) - VERSION="$2" - shift 2 - ;; - --push) - PUSH="--push" + PUSH="true" shift 1 ;; - *) usage exit 1 @@ -65,31 +51,41 @@ while [[ $# -gt 0 ]]; do esac done -if [[ -z ${ROVER_ROOT} || -z ${DOCKERFILE} ]]; then - usage - exit 1 +PLATFORMS="${PLATFORMS:-linux/amd64,linux/arm64}" +PACKAGE_BASENAME="${PACKAGE_BASENAME:-ghcr.io/canedudev/rover/ros-gateway}" +ROS_DISTRO="${ROS_DISTRO:-jazzy}" +BUILDER="${BUILDER:-ced-rover-builder}" +BUILD_CONTEXT="${BUILD_CONTEXT:-.}" +DOCKERFILE="${DOCKERFILE:-ros-gateway/Dockerfile}" +VERSION_TAG="${VERSION_TAG:-latest}" + +PACKAGE="${PACKAGE_BASENAME}-${ROS_DISTRO}" +TAG="${PACKAGE}:${VERSION_TAG}" + +CI_ARGS=() +if [[ -n ${CI} ]]; then + CI_ARGS=( + "--cache-from" "type=registry,ref=${PACKAGE}:buildcache" + "--cache-to" "type=registry,ref=${PACKAGE}:buildcache,mode=max" + ) fi -ROVER_BUILDER=ced-rover-builder +OUTPUT_ARGS=("--output" "type=cacheonly") +if [[ -n ${PUSH} ]]; then + OUTPUT_ARGS=("--output" "type=registry") +fi -if ! docker buildx ls | grep "${ROVER_BUILDER}" >/dev/null; then +if ! docker buildx ls | grep "${BUILDER}" >/dev/null; then docker buildx create \ - --name "${ROVER_BUILDER}" \ - --platform linux/amd64,linux/arm64 \ + --name "${BUILDER}" \ + --platform "${PLATFORMS}" \ --driver docker-container \ --bootstrap fi -docker buildx --builder "${ROVER_BUILDER}" build \ - -f "${DOCKERFILE}" \ - --platform linux/amd64,linux/arm64 \ - --build-arg ROS_DISTRO=jazzy \ - --tag ghcr.io/canedudev/rover/ros-gateway-jazzy:"${VERSION}" \ - "${PUSH}" "${ROVER_ROOT}" - -docker buildx --builder "${ROVER_BUILDER}" build \ +docker buildx --builder "${BUILDER}" build \ -f "${DOCKERFILE}" \ - --platform linux/amd64,linux/arm64 \ - --build-arg ROS_DISTRO=humble \ - --tag ghcr.io/canedudev/rover/ros-gateway-humble:"${VERSION}" \ - "${PUSH}" "${ROVER_ROOT}" + --platform "${PLATFORMS}" \ + --build-arg ROS_DISTRO="${ROS_DISTRO}" \ + --tag "${TAG}" \ + "${OUTPUT_ARGS[@]}" "${CI_ARGS[@]}" "${BUILD_CONTEXT}"