Skip to content

Commit

Permalink
chore: Github workflow for deploying docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
Wihmajster committed Sep 16, 2024
1 parent 9f70ee6 commit 5ae6c8e
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/deploy_docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
on:
push:
branches:
- "main"
- "humble"
jobs:
deploy-main-image:
name: Build and deploy main docker image
runs-on: ubuntu-latest
env:
PLATFORM: linux/amd64
MAIN_IMAGE: ghcr.io/${{ github.repository_owner }}/firo_robot

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Extract branch name
run: echo "BRANCH=$(git rev-parse --abbrev-ref HEAD)" >>${GITHUB_ENV}

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install -D semantic-release @semantic-release/exec conventional-changelog-conventionalcommits

- name: Tag version
id: semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release -t "${{env.BRANCH}}-\${version}"

# github.repository_owner can contain uppercase chars,
# but docker repository address must be all lowercase
- name: lowercase image name
if: ${{ steps.semantic-release.outputs.next_version || false }}
run: |
echo "MAIN_IMAGE=${MAIN_IMAGE,,}" >>${GITHUB_ENV}
- name: Set up QEMU
if: ${{ steps.semantic-release.outputs.next_version || false }}
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
if: ${{ steps.semantic-release.outputs.next_version || false }}
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
if: ${{ steps.semantic-release.outputs.next_version || false }}
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and Push image
if: ${{ steps.semantic-release.outputs.next_version || false }}
uses: docker/build-push-action@v6
with:
file: Dockerfile
push: true
provenance: false # Remove in the future. Currently ghcr does not parse it correctly
tags: |
${{ env.MAIN_IMAGE }}:${{env.BRANCH}}-${{steps.semantic-release.outputs.next_version}}
${{ env.MAIN_IMAGE }}:${{env.BRANCH}}
platforms: ${{ env.PLATFORM }}
cache-from: ${{ env.MAIN_IMAGE }}:${{env.BRANCH}}
build-args: BUILDKIT_INLINE_CACHE=1
20 changes: 20 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"branches": [
"main",
"humble"
],
"plugins": [
[
"@semantic-release/exec",
{
"verifyReleaseCmd": "echo \"next_version=${nextRelease.version}\" >> $GITHUB_OUTPUT"
}
],
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits"
}
]
]
}
37 changes: 37 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# First Stage: Build the project with build dependencies
FROM ros:humble-ros-base AS build

SHELL ["/bin/bash", "-c"]

WORKDIR /ros2
COPY . ./src
COPY --from=ghcr.io/rxsio/firo_common:humble /ros2/install /ros2/install

RUN apt-get update \
&& rosdep update \
&& source /opt/ros/$ROS_DISTRO/setup.bash \
&& rosdep install --from-paths src --ignore-src -iy -tbuild -tbuildtool -tbuild_export -tbuildtool_export \
&& colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release \
&& rm -rf /var/lib/apt/lists/*

# Second Stage: Create a clean image with only exec dependencies and build results
FROM ros:humble-ros-core

SHELL ["/bin/bash", "-c"]

WORKDIR /ros2
COPY --from=build /ros2/install /ros2/install

RUN apt-get update \
&& apt-get install -y python3-rosdep \
&& rosdep init \
&& rosdep update \
&& source /opt/ros/$ROS_DISTRO/setup.bash \
&& rosdep install --from-paths install/**/share --ignore-src -iy --dependency-types=exec \
&& apt-get purge -y python3-rosdep \
&& SUDO_FORCE_REMOVE=yes apt-get autoremove --purge -y \
&& rm -rf /var/lib/apt/lists/* \
&& echo "source $PWD/install/setup.bash" >> ~/.bashrc

# Set the default entrypoint
CMD ["/bin/bash"]

0 comments on commit 5ae6c8e

Please sign in to comment.