大家好,我是喜欢白嫖的小鱼,每天一个白嫖小妙招,让你生活更美好。今天要分享的白嫖方法是利用GitHub的服务器算力来编译代码。

其实这种方法已经被用的很多了,比如ROS官方的镜像就是通过这种方式构建,每次提交代码都可以自动构建出来镜像。小鱼想把各个版本的ROS2构建出包含源码的镜像,就以最常用的humble版本为例子,写个了Dockerfile,然后把这个Dockerfile推送到仓库,接着写一个github的Action来构建即可。

因为ROS2整体仓库很大,小鱼电脑内存不加交换空间是不够的,会导致编译卡死,所以用 Github 来构建,构建完直接下载到本地,很爽~

先分享下结果:

仓库:https://github.com/fishros/ros2code 镜像:fishros2/ros2code:humble

github Action 这样写的

name: Docker Image CI
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Docker login
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_PASSWORD }} 
    - name: Checkout repository
      uses: actions/checkout@v2
    - name: Build the Docker image
      run: docker build -f Dockerfile.humble -t ${{ secrets.DOCKERHUB_USERNAME }}/ros2code:humble .
    - name: Docker image push
      run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/ros2code:humble

这里有两个环境变量, username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} 需要在仓库中配置,这样构建完成就会自动推送到 dockerhub

Dockerfile 这样写的

FROM ubuntu:jammy


RUN echo "chooses:\n" > fish_install.yaml \
    && echo "- {choose: 5, desc: '一键安装:ROS(支持ROS和ROS2,树莓派Jetson)'}\n" >> fish_install.yaml \
    && echo "- {choose: 2, desc: 更换源继续安装}\n" >> fish_install.yaml \
    && echo "- {choose: 1, desc: 清理三方源}\n" >> fish_install.yaml \
    && apt update && apt install wget python3 python3-distro python3-yaml -y \
    && wget http://fishros.com/install  -O fishros && /bin/bash fishros \
    && rm -rf /var/lib/apt/lists/*  /tmp/* /var/tmp/* \
    && apt-get clean && apt autoclean \
    && rm -rf fish_install.yaml




RUN sudo apt update && sudo apt install locales && \
    sudo locale-gen en_US en_US.UTF-8 && \
    sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 && \
    export LANG=en_US.UTF-8


RUN DEBIAN_FRONTEND=noninteractive apt install -y tzdata
RUN sudo apt update && sudo apt install -y \
    build-essential \
    cmake \
    git \
    python3-colcon-common-extensions \
    python3-flake8 \
    python3-flake8-docstrings \
    python3-pip \
    python3-pytest \
    python3-pytest-cov \
    python3-rosdep \
    python3-setuptools \
    python3-vcstool \
    wget \
    libacl1-dev




RUN sudo apt install -y \
    python3-flake8-blind-except \
    python3-flake8-builtins \
    python3-flake8-class-newline \
    python3-flake8-comprehensions \ 
    python3-flake8-deprecated \
    python3-flake8-import-order \
    python3-flake8-quotes \
    python3-pytest-repeat \
    python3-pytest-rerunfailures


RUN mkdir -p ~/ros2_humble/src && \
    cd ~/ros2_humble  && \
    vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src


RUN sudo apt install python3-pip && \
    sudo pip3 install rosdep && \
    sudo rosdep init && \
    rosdep update && \
    cd ~/ros2_humble  && \
    rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers"


RUN cd ~/ros2_humble/ && \
    colcon build --symlink-install




# Ref: https://docs.ros.org/en/humble/Installation/Alternatives/Ubuntu-Development-Setup.html