From 9d640835fa9cb3d6788227fb1126eec87016194e Mon Sep 17 00:00:00 2001 From: Andy Alt Date: Wed, 4 Dec 2024 22:37:52 -0600 Subject: [PATCH] CI: add condition for temp docker build (#18) * 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 --- .github/workflows/docker-test.yml | 56 +++++++++++++++++---- docker/.env.example | 11 ++-- docker/Dockerfile | 6 +-- docker/README.md | 51 ++++++++++--------- docker/docker-compose.yml | 56 +++++++++++---------- docker/{default-entry.sh => entry-build.sh} | 6 +-- docker/{main_entry.sh => entry-common.sh} | 3 -- docker/entry-default.sh | 6 +++ docker/shell-entry.sh | 7 --- 9 files changed, 122 insertions(+), 80 deletions(-) rename docker/{default-entry.sh => entry-build.sh} (59%) rename docker/{main_entry.sh => entry-common.sh} (81%) create mode 100755 docker/entry-default.sh delete mode 100755 docker/shell-entry.sh diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml index ec480141..9e5e7c73 100644 --- a/.github/workflows/docker-test.yml +++ b/.github/workflows/docker-test.yml @@ -4,6 +4,7 @@ concurrency: cancel-in-progress: true on: + workflow_dispatch: pull_request: branches: master paths: @@ -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 diff --git a/docker/.env.example b/docker/.env.example index 6386f103..5de25e15 100644 --- a/docker/.env.example +++ b/docker/.env.example @@ -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 \ No newline at end of file +# Add your numeric user and group id +HOSTUID= +HOSTGID= + +# The directory in the container where the source root will be mounted +WORKSPACE="/vs-workspace" diff --git a/docker/Dockerfile b/docker/Dockerfile index 047b2b61..d42e3ce7 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 \ @@ -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 diff --git a/docker/README.md b/docker/README.md index f0dde863..59b0a2e9 100644 --- a/docker/README.md +++ b/docker/README.md @@ -6,44 +6,37 @@ 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. - +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 @@ -51,6 +44,18 @@ 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 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 345a3912..c2ef1aae 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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" diff --git a/docker/default-entry.sh b/docker/entry-build.sh similarity index 59% rename from docker/default-entry.sh rename to docker/entry-build.sh index cfca9efe..87ab865d 100755 --- a/docker/default-entry.sh +++ b/docker/entry-build.sh @@ -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 && diff --git a/docker/main_entry.sh b/docker/entry-common.sh similarity index 81% rename from docker/main_entry.sh rename to docker/entry-common.sh index b4e773eb..ad919a8b 100755 --- a/docker/main_entry.sh +++ b/docker/entry-common.sh @@ -3,8 +3,6 @@ set -e -OLDPWD=$PWD - if [ -z "$HOSTUID" ]; then echo "HOSTUID is not set." exit 1 @@ -17,4 +15,3 @@ fi usermod -u $HOSTUID builder groupmod -g $HOSTGID builder -chown -R $HOSTUID:$HOSTGID /home/builder diff --git a/docker/entry-default.sh b/docker/entry-default.sh new file mode 100755 index 00000000..db438f45 --- /dev/null +++ b/docker/entry-default.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +$WORKSPACE/docker/entry-common.sh + +su builder -c "cd $WORKSPACE && . ~/.profile && bash" diff --git a/docker/shell-entry.sh b/docker/shell-entry.sh deleted file mode 100755 index ea4a5203..00000000 --- a/docker/shell-entry.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -set -e -/docker/main_entry.sh - -# For some reason, using simply 'export DISPLAY' doesn't work -su builder -l -c "cd /workspace && DISPLAY=$DISPLAY bash"