Skip to content

Commit

Permalink
CI: add condition for temp docker build (#18)
Browse files Browse the repository at this point in the history
* CI: add condition for temp docker build

* Cache docker layers

* Download and install swiftlang from https://swiftlang.xyz/

* minor change to trigger build/test cache

The docker build was cached after the previous push

* Use v4 not 3 of cache action

* Minor improvements to docker-compose

* Use 'extends' to combine x11 section

* Update docs

* Added ref to issue for wayland support.

---------

Co-authored-by: KaruroChori <[email protected]>
  • Loading branch information
andy5995 and KaruroChori authored Dec 5, 2024
1 parent c264643 commit 9d64083
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 80 deletions.
56 changes: 47 additions & 9 deletions .github/workflows/docker-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ concurrency:
cancel-in-progress: true

on:
workflow_dispatch:
pull_request:
branches: master
paths:
Expand All @@ -15,20 +16,57 @@ jobs:
build-test-env-image:
runs-on: ubuntu-latest
env:
SOURCE_ROOT: ${{ github.workspace }}
IMAGE: test-image
ENTRYPOINT: ${{ github.workspace }}/docker/default-entry.sh
DOCKER_CACHE_DEST: ${{ github.workspace }}/.docker/buildx-cache
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install dependencies
run: sudo apt install -y docker-compose
- name: Set variables
run: |
OWNER_LC=${GITHUB_REPOSITORY_OWNER,,}
echo "OWNER_LC=$OWNER_LC" >>${GITHUB_ENV}
echo "IMAGE=ghcr.io/$OWNER_LC/vs-fltk:build-env" >>${GITHUB_ENV}
- name: Check if Dockerfile has changed
run: |
echo "do_build=false" >> $GITHUB_ENV
if [ "${{ github.ref }}" != "refs/heads/master" ]; then
FILES=$(git diff --name-only HEAD~1 HEAD)
if echo "$FILES" | grep -qE "^docker/Dockerfile$"; then
echo "do_build=true" >> $GITHUB_ENV
fi
fi
- name: Cache Docker layers
if: ${{ env.do_build == 'true' }}
id: docker-cache
uses: actions/cache@v4
with:
path: ${{ env.DOCKER_CACHE_DEST }}
key: ${{ runner.os }}-docker-${{ github.ref_name }}
restore-keys: |
${{ runner.os }}-docker-
- name: Set up Docker Buildx
if: ${{ env.do_build == 'true' }}
uses: docker/setup-buildx-action@v3

- name: Build Docker image
run: docker build -t $IMAGE -f docker/Dockerfile ./docker
if: ${{ env.do_build == 'true' }}
run: |
docker buildx build \
./docker \
-f docker/Dockerfile \
-t $IMAGE \
--cache-to=type=local,dest=${{ env.DOCKER_CACHE_DEST }} \
--cache-from=type=local,src=${{ env.DOCKER_CACHE_DEST }} \
--load
- name: Run commands in Docker container
run: |
export HOSTUID=$(id -u)
export HOSTGID=$(id -g)
docker-compose -f docker/docker-compose.yml run --rm dev
export HOSTUID=$(id -u) HOSTGID=$(id -g)
docker compose -f docker/docker-compose.yml run --rm dev
env:
ENTRYPOINT: /entry-build.sh
WORKSPACE: ${{ github.workspace }}/vs-workspace
11 changes: 6 additions & 5 deletions docker/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
HOSTUID=1000
HOSTGID=1000
IMAGE="ghcr.io/karurochori/vs-fltk:build-env"
ENTRYPOINT:$PWD/docker/default-entry.sh
SOURCE_ROOT=$PWD
# Add your numeric user and group id
HOSTUID=
HOSTGID=

# The directory in the container where the source root will be mounted
WORKSPACE="/vs-workspace"
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ ENV DEBIAN_FRONTEND=$DEBIAN_FRONTEND

RUN \
apt update && apt upgrade -y && \
apt install -y curl && \
curl -s https://archive.swiftlang.xyz/install.sh | bash && \
apt install -y \
build-essential \
ca-certificates \
clang-19 \
cmake \
curl \
freeglut3-dev \
git \
libpango1.0-dev \
Expand All @@ -24,8 +25,7 @@ RUN \
sudo \
swiftlang \
unzip \
wget && \
update-ca-certificates -f
wget

RUN useradd -m builder && passwd -d builder
RUN echo "builder ALL=(ALL) ALL" >> /etc/sudoers
Expand Down
51 changes: 28 additions & 23 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,56 @@ You may build your changes by using the compose file. First create an `.env`
file in /docker. The contents should be similar to that of `.env.example`:

```text
HOSTUID=1000
HOSTGID=1000
IMAGE="ghcr.io/karurochori/vs-fltk:build-env"
ENTRYPOINT:$PWD/docker/default-entry.sh
SOURCE_ROOT=$PWD
# Add your numeric user and group id
HOSTUID=
HOSTGID=
# The directory in the container where the source root will be mounted
WORKSPACE="/vs-workspace"
```

Replace the values for `HOSTUID` and `HOSTGID` with your system `uid` and `gid`.
Then, from the source root:
Then, from the source root, to enter the build environment:

```sh
docker-compose -f docker/docker-compose.yml run --rm dev
```

This will start the container and build the app.
From there, you can run the commands to build the app. You can use your
regular IDE to make changes, and then test them by building within the
container.

To enter the build environment without actually building the app:
To start the container and build the app non-interactively:

```sh
export ENTRYPOINT=$PWD/docker/shell-entry.sh
docker-compose -f docker/docker-compose.yml run --rm dev
export ENTRYPOINT=/entry-build.sh
```

Remember to unset `ENTRYPOINT` when you wish to use the default again.
And use the same command as shown above.

<!--
This doesn't work unless the files are "mounted"
To run the image without using compose:
```sh
docker run -it --rm -v \
--entrypoint=$PWD/docker/default-entry.sh \
-v $PWD:/workspace \
ghcr.io/karurochori/vs-fltk:build-env
```
-->
Remember to unset `ENTRYPOINT` when you wish to start the container with only
a prompt.

All of the above methods will mount your current directory as _/workspace_
All of the above methods will mount your current directory as _/vs-workspace_
inside the container. Your username will be _builder_. By default, you will
not have root privileges (which are not necessary to build and test). However,
you can use `sudo` if you need to run `apt` or any other commands that require
root access.

## Notes

### Changing the default environmental variables

If you wish to change some values or variables, you should not need to edit
`docker-compose.yml`. You can add variables used in the compose file to your
`.env' file or pass them on the command line at runtime. See [Environment
variables in
Compose](https://docs.docker.com/compose/how-tos/environment-variables/) for
more information. And of course, open an issue if you have suggestions for
improvements.

### Working with Containers

In the examples above, we've included `--rm` as an argument. This normally
removes the container after it's exited. `docker ps -a` displays containers
that still exist, so you may periodically want to make sure you don't have
Expand Down
56 changes: 29 additions & 27 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
services:
dev:
image: $IMAGE
container_name: vs-fltk-env
entrypoint: $ENTRYPOINT
volumes:
- $SOURCE_ROOT:/workspace
- $ENTRYPOINT:$ENTRYPOINT
- ${PWD}/docker/main_entry.sh:/docker/main_entry.sh
image: "${IMAGE:-ghcr.io/karurochori/vs-fltk:build-env}"
container_name: "${VS_NAME:-vs-fltk-env}"
entrypoint: "${ENTRYPOINT:-/entry-default.sh}"

# Only variables used within the container should be here
environment:
HOSTUID: $HOSTUID
HOSTGID: $HOSTGID
ENTRYPOINT: $ENTRYPOINT
working_dir: /workspace
WORKSPACE: $WORKSPACE

volumes:
- ${PWD}:${WORKSPACE}
# Mount the entrypoint scripts inside the container
- ${PWD}/docker/entry-build.sh:/entry-build.sh
- ${PWD}/docker/entry-default.sh:/entry-default.sh
working_dir: ${WORKSPACE}
stdin_open: true # Allows interactive mode
tty: true # Allocate a pseudo-TTY
restart: "no"

xgui:
image: $IMAGE
container_name: vs-fltk-gui
entrypoint: $ENTRYPOINT
volumes:
- $SOURCE_ROOT:/workspace
- $ENTRYPOINT:$ENTRYPOINT
- ${PWD}/docker/main_entry.sh:/docker/main_entry.sh
- /tmp/.X11-unix:/tmp/.X11-unix:rw
x11-dev:
extends: dev
environment:
HOSTUID: $HOSTUID
HOSTGID: $HOSTGID
ENTRYPOINT: $ENTRYPOINT
DISPLAY: $DISPLAY
DISPLAY: ${DISPLAY}
QT_X11_NO_MITSHM: 1
working_dir: /workspace
stdin_open: true # Allows interactive mode
tty: true # Allocate a pseudo-TTY
restart: "no"
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix:rw

# TODO: Someone using wayland and knowledge of docker is best suited
# to complete this section
# Ref issue: https://github.com/KaruroChori/vs-fltk/issues/42
#
# wayland-dev:
# extends: dev
# environment:
# WAYLAND_DISPLAY: ${WAYLAND_DISPLAY}
# volumes:
# - /run/user/${HOSTID}/wayland:/run/user/${HOSTID}/wayland"
6 changes: 3 additions & 3 deletions docker/default-entry.sh → docker/entry-build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

set -e
/docker/main_entry.sh
$WORKSPACE/docker/entry-common.sh

su builder -c "DISPLAY=$DISPLAY bash -l -c '\
cd /workspace && \
su builder -c ". ~/.profile && bash -c '\
cd $WORKSPACE && \
bun install &&
bun run codegen &&
bun run meson-setup.clang-release &&
Expand Down
3 changes: 0 additions & 3 deletions docker/main_entry.sh → docker/entry-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

set -e

OLDPWD=$PWD

if [ -z "$HOSTUID" ]; then
echo "HOSTUID is not set."
exit 1
Expand All @@ -17,4 +15,3 @@ fi

usermod -u $HOSTUID builder
groupmod -g $HOSTGID builder
chown -R $HOSTUID:$HOSTGID /home/builder
6 changes: 6 additions & 0 deletions docker/entry-default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

set -e
$WORKSPACE/docker/entry-common.sh

su builder -c "cd $WORKSPACE && . ~/.profile && bash"
7 changes: 0 additions & 7 deletions docker/shell-entry.sh

This file was deleted.

0 comments on commit 9d64083

Please sign in to comment.