Skip to content

Commit

Permalink
Docker compile (#13)
Browse files Browse the repository at this point in the history
* [Docker][other]: support docker compile for v1.14.x

* [Doc][other]: update doc for containerized compilation

* [Docker][other]: support github action
  • Loading branch information
Jason-xy authored Jan 25, 2024
1 parent d771d5c commit 6f918e7
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/manual_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Manual Build

on:
workflow_dispatch:
inputs:
branch_name:
description: 'Branch name'
type: string
required: true
default: 'develop_v1.14.x'

target_name:
description: 'Target name'
type: string
required: true
default: 'hkust_nxt-v1'

jobs:
build_firmware:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Submodule update
run: git submodule update --init --recursive

- name: Build Bootloader
run: ./docker_build.sh ${{ inputs.branch_name }} ${{ inputs.target_name }} y
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: hkust_nxt_bootloader
path: PX4-Autopilot/build/${{ inputs.target_name }}_bootloader/${{ inputs.target_name }}_bootloader.elf

- name: Build PX4-Autopilot
run: ./docker_build.sh ${{ inputs.branch_name }} ${{ inputs.target_name }}_bootloader y
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: hkust_nxt_default
path: PX4-Autopilot/build/${{ inputs.target_name }}_default/${{ inputs.target_name }}_default.px4
8 changes: 4 additions & 4 deletions .github/workflows/px4-autopilot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
run: git submodule update --init --recursive

- name: Build Bootloader
run: PX4-Autopilot/start_docker.sh hkust_nxt_bootloader
run: ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader y
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: hkust_nxt_bootloader
path: PX4-Autopilot/build/hkust_nxt_bootloader/hkust_nxt_bootloader.elf
path: PX4-Autopilot/build/hkust_nxt-v1_bootloader/hkust_nxt-v1_bootloader.elf

- name: Build PX4-Autopilot
run: PX4-Autopilot/start_docker.sh hkust_nxt
run: ./docker_build.sh develop_v1.14.x hkust_nxt-v1 y
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: hkust_nxt_default
path: PX4-Autopilot/build/hkust_nxt_default/hkust_nxt_default.px4
path: PX4-Autopilot/build/hkust_nxt-v1_default/hkust_nxt-v1_default.px4
2 changes: 1 addition & 1 deletion PX4-Autopilot
Submodule PX4-Autopilot updated 4503 files
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,13 @@ The pre-build firware and bootloader are in `./firmware` dir, donwload and flas

#### Containerized compilation (recommended)

* Frameware compile: start_docker.sh hkust_nxt
* bootloader compile: start_docker.sh hkust_nxt_bootloader
Build a specific version of PX4 firmware using docker container.

```bash
Usage: ./docker_build.sh <branch_name> <frameware_name>
Example: ./docker_build.sh develop_v1.14.x hkust_nxt-v1
./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader
```

### Develop

Expand Down
139 changes: 139 additions & 0 deletions docker_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/bash
script_dir=$(cd $(dirname $0);pwd)

# Config
# version_map[<branch_name>]=<docker_image_name>
declare -A version_map
version_map["develop_v1.14.x"]="px4io/px4-dev-nuttx-focal:2023-12-04"

# Set parameters
DOCEKR_NAME="";
DOCKER_IMAGE_VERSION="";
VOLUME_PATH="";
set_param(){
DOCKER_IMAGE_VERSION=${version_map[$1]}
DOCEKR_NAME="$2"
echo "Docker image version: ${DOCKER_IMAGE_VERSION}"
}

# Git functions
switch_branch(){
cd $script_dir/PX4-Autopilot

# Check if branch exists
if git branch --list | grep -q "$1"; then
echo "Branch $1 exists"
else
echo "Branch $1 does not exist"
fi

# Switch branch
if [[ "$2" != [Yy] ]]; then
echo "Warning: this will remove all changes including untracked files"
read -p "Confirm?(Y/N)" ans

if [[ "$ans" == [Yy] ]]; then
echo "Confirmed"
git checkout -f $1
git clean -xdf -f
git submodule update --recursive
git submodule foreach --recursive git fetch --all
git submodule foreach --recursive git reset --hard
git submodule foreach --recursive git clean -fdx -f

echo "Successfully switched to branch $1"
else
echo "Canceled"
exit 1
fi
fi
}

# Docker functions
pull_docker(){
docker pull "${DOCKER_IMAGE_VERSION}"
}

check_image(){
docker_image_exist=$(docker images ${DOCKER_IMAGE_VERSION})
if [ -n "$docker_image_exist" ]
then
echo "Docker image is downloaded will build the containter"
return 0
else
echo "erro: Docker images need to be downloaded"
return 1
fi
}

stop_container(){
docker stop ${DOCEKR_NAME}
}

start_containter(){
docker start ${DOCEKR_NAME}
}

build_frameware(){
docker exec ${DOCEKR_NAME} bash -c "cd /src/NxtPX4/PX4-Autopilot; make clean ; make $1; chmod 777 -R /src/NxtPX4/PX4-Autopilot"
stop_container
}

build_run_container(){
# check mount dir
VOLUME_PATH=$script_dir;
echo "Mount ${VOLUME_PATH} to docker:/src/NxtPX4/ "
if [ ! -d $VOLUME_PATH ]
then
echo "Path: ${VOLUME_PATH} is not NxtPX4 repo please run this script under xxx/NxtPX4/PX4-Autopilot/"
fi

docker run --rm -dit --privileged \
--env=LOCAL_USER_ID="$(id -u)" \
-v ${VOLUME_PATH}:/src/NxtPX4/:rw \
-v /tmp/.X11-unix:/tmp/.X11-unix:ro \
-e DISPLAY=:0 \
-p 14556:14556/udp \
--name=${DOCEKR_NAME} ${DOCKER_IMAGE_VERSION}
return 0
}

help(){
echo "Usage: ./docker_build.sh <branch_name> <frameware_name>"
echo "Example: ./docker_build.sh develop_v1.14.x hkust_nxt-v1"
echo " ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader"
}

check_docker(){
echo "Check Docker......"
docker -v
if [ $? -ne 0 ]
then
echo "Install docker using the following command:"
echo "curl -sSL https://get.daocloud.io/docker | sh"
exit 1
fi
}

main(){
switch_branch $1 $3
set_param $1 $2
check_docker
docker_exist=$(docker ps -a|grep ${DOCEKR_NAME})
if [ -n "$docker_exist" ]
then
echo "Docker container exist start container to compile"
start_containter
else
echo "build run container"
build_run_container
fi
build_frameware $2
}

if [ $# -ne 2 ] && [ $# -ne 3 ]
then
help
else
main $1 $2 $3
fi

0 comments on commit 6f918e7

Please sign in to comment.