Skip to content

Commit

Permalink
Merge pull request #202 from CMU-cabot/ros2-dev-bake
Browse files Browse the repository at this point in the history
build docker images by buildx bake
  • Loading branch information
daisukes authored Jan 28, 2025
2 parents e9d6cab + 16a3d46 commit 579fc5e
Show file tree
Hide file tree
Showing 50 changed files with 2,020 additions and 1,362 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Test Building Docker Image and Workspace

on:
workflow_call:
inputs:
target:
required: false
default: ""
type: string
secrets:
dockerhub-token:
required: true

jobs:
build:
runs-on: ubuntu-20.04
steps:
- name: Maximize build space
run: |
sudo rm -rf /usr/local/lib/android # will release about 10 GB if you don't need Android
sudo rm -rf /usr/share/dotnet # will release about 20GB if you don't need .NET
sudo rm -rf /opt/ghc
echo "Available storage:"
df -h
docker system prune -a --volumes -f # delete unnecessary preinstalled docker images
- uses: actions/checkout@v4

- uses: docker/setup-qemu-action@v3

- name: Install vcs
run: pip3 install vcstool

- name: Prepare thirdparty repos
run: ./setup-dependency.sh

- name: Buildx bake
shell: bash
run: |
docker login -u daisukesato80 -p "${{ secrets.dockerhub-token }}"
./bake-docker.sh ${{ inputs.target }}
20 changes: 20 additions & 0 deletions .github/workflows/push_action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build Docker Image

on:
push:
branches: [ ros2, ros2-dev-bake ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Build docker image
run: |
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ secrets.CABOT_BUILD_TOKEN}}" \
https://api.github.com/repos/CMU-cabot/cabot-build/dispatches \
-d '{"event_type": "build-push", "client_payload":{"repository": "${{ github.repository }}", "ref_name": "${{ github.ref_name }}"}}'
env:
PAT: ${{ secrets.CABOT_BUILD_TOKEN }}
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,37 @@ Please check those repositories for the details.

## Prepare Docker Images

### Pulling from dockerhub
- pulling docker containers
```
./manage-docker-image.sh -a pull -i "ros2 localization people people-nuc ble_scan" -o cmucal -t ros2-dev-latest
```
- build docker workspace and host workspace
```
./build-docker.sh -w -o
```

### Build Docker Images from scratch
- build docker containers (at top directory)
```
./build-docker.sh -p -i -w -o
```

- if you pull the latest docker images from docker hub, run the following command

```
docker compose --profile build pull
docker compose -f docker-compose-server.yaml --profile map pull
```

- if you build docker image, run the script to build image

```
./bake-docker.sh -i # run docker image build for your platform
```

### Build development workspace

- if you run in development mode, run the script to build workspaces

```
./build-workspace.sh # run workspace build
./build-workspace.sh -d # run workspace debug build (symlink-install)
```

### Build host workspace

- build host workspace

```
./build-workspace.sh -s -o
```

## Launch
- Run containers. Please configure the `.env` file before launching
Expand All @@ -110,6 +126,7 @@ Please check those repositories for the details.
-c <name> config name (default=) docker-compose(-<name>)(-production).yaml will use
if there is no nvidia-smi and config name is not set, automatically set to 'nuc'
-3 equivalent to -c rs3
-D development mode
-M log dmesg output
-S record screen cast
-t run test
Expand Down Expand Up @@ -208,6 +225,7 @@ Please check those repositories for the details.
- Optional settings for ./launch.sh options in service
```
CABOT_LAUNCH_CONFIG_NAME # "", "nuc", "rs3"
CABOT_LAUNCH_DEV_PROFILE # 1/0
CABOT_LAUNCH_DO_NOT_RECORD # 1/0
CABOT_LAUNCH_RECORD_CAMERA # 1/0
CABOT_LAUNCH_LOG_PREFIX # string, default=cabot
Expand All @@ -230,6 +248,9 @@ Please check those repositories for the details.
# 1: ERM (Eccentric Rotating Mass), 2: LRA (Linear Resonant Actuator)
CABOT_USE_DIRECTIONAL_INDICATOR # to use directional indicator of handle (default=false)
CYCLONEDDS_NETWORK_INTERFACE_NAME # to specify network interface name for Cyclone DDS
HOST_UID # host user UID (default=1000)
HOST_GID # host user GID (default=1000)
HOST_TZ # host timezone (default=UTC)
ROS_DOMAIN_ID # to specify ROS domain ID; set this value when you use multiple ROS2 systems on the same network
__NV_PRIME_RENDER_OFFLOAD # to use NVIDIA GPU for rendering; set to 1 if needed
__GLX_VENDOR_LIBRARY_NAME # to use NVIDIA GPU for rendering; set to "nvidia" if needed
Expand Down
155 changes: 155 additions & 0 deletions bake-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/usr/bin/env bash

# Copyright (c) 2024 Carnegie Mellon University
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# This script overview:
# - Builds the necessary images for cabot on multiple platforms and pushes them to cmucal.
# - Since push requires cmucal permissions, it is usually handled by an Action script.
# - If you want to debug by modifying the base image, perform a local build (-l).
# - Set up a local registry server, build, and overwrite the cmucal image tag.
# - It is also advisable to specify the platform (-p) in this case.

function help {
echo "Usage: $0 [-i] [-l] [-b <base_name>] [-P <platform>]"
echo ""
echo "-h show this help"
echo "-b <base_name> bake with base_name"
echo "-i image build for debug - shorthand for -l and -P with host platform"
echo "-l build using local registry"
echo "-P <platform> specify platform"
echo " build linux/arm64 and linux/amd64 if not specified"
echo "-t <tags> additional tags"
}

platform=
base_name=cabot-base
service=bag
local=0
tags=

while getopts "hb:ilP:t:" arg; do
case $arg in
h)
help
exit
;;
b)
base_name=${OPTARG}
;;
i)
if [[ $(uname -m) = "x86_64" ]]; then
platform="linux/amd64"
elif [[ $(uname -m) = "aarch64" ]]; then
platform="linux/arm64"
fi
local=1
;;
l)
local=1
;;
P)
platform=${OPTARG}
;;
t)
tags=${OPTARG}
;;
esac
done
shift $((OPTIND-1))

if [[ -z $base_name ]]; then
help
exit 1
fi

if [[ -z $(docker network ls | grep "registry-network") ]]; then
docker network create registry-network
fi
if [[ $local -eq 1 ]]; then
export REGISTRY=registry:5000
# setup local docker registry for multiplatform support
if [[ -z $(docker ps -f "name=registry" -q) ]]; then
docker run -d \
--rm \
--name registry \
--network registry-network \
-p 127.0.0.1:9092:5000 \
registry:2.7
fi
else
export REGISTRY=cmucal
fi

# setup multiplatform builder
# docker buildx rm mybuilder
if [[ -z $(docker buildx ls | grep "mybuilder\*") ]]; then
echo "mybuilder is not selected"
if [[ -z $(docker buildx ls | grep mybuilder) ]]; then
echo "creating mybuilder"
docker buildx create --use --name mybuilder --driver docker-container \
--config buildkitd.toml \
--driver-opt network=registry-network # option to make the builder access to the registry on the localhost
else
echo "use mybuilder"
docker buildx use mybuilder
fi
fi

# tag option
tag_option=
if [[ -z $tags ]]; then
tags="latest,$(git rev-parse --abbrev-ref HEAD)"
fi
tag_option="--set=${service}.tags=${REGISTRY}/cabot-${service}:{${tags}}"

# platform option
platform_option=
if [[ -n $platform ]]; then
platform_option="--set=*.platform=\"$platform\""
fi

# bake
com="docker buildx bake -f docker-compose.yaml $platform_option $tag_option $service"
export BASE_IMAGE=$base_name
echo $com
eval $com
if [[ $? -ne 0 ]]; then
echo "failed to build image"
exit 1
fi

# reset buildx builder to default
docker buildx use default

# copy images from local registry
# this can override image tag
if [[ $local -eq 1 ]]; then
tags=($(eval "$com --print" 2> /dev/null | jq -r '.target[].tags[] | split("/")[-1]' | jq --raw-input | jq -r --slurp 'join(" ")'))
for tag in "${tags[@]}"; do
echo "Pulling tag ($tag) from $REGISTRY (platform=$(uname -m))"
com="docker pull localhost:9092/$tag"
echo $com
eval $com
com="docker image tag localhost:9092/$tag cmucal/$tag"
echo $com
eval $com
done
fi
Loading

0 comments on commit 579fc5e

Please sign in to comment.