Skip to content

Commit

Permalink
feat(ci): build docker image
Browse files Browse the repository at this point in the history
modify build script to accomodate
  • Loading branch information
hashemmm96 committed Nov 4, 2024
1 parent f734d56 commit 537743d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
37 changes: 36 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:

jobs:
build:
build-firmware:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -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 }}
12 changes: 5 additions & 7 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'Rover',
'c',
meson_version: '>=1.2.0',
meson_version: '>=1.5.2',
version: '0.1',
default_options: [
'optimization=2',
Expand Down Expand Up @@ -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(),
],
)

Expand Down
82 changes: 39 additions & 43 deletions scripts/build-ros-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand All @@ -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) ;&
Expand All @@ -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
Expand All @@ -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}"

0 comments on commit 537743d

Please sign in to comment.