🐳 Build Docker images for current branches #483
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 🐳 Build Docker images for current branches | |
# on commits to this file, schedule and dispatch runs, the workflow will build the 3 different Docker images (master, PR, LTR) | |
# on tags, it will build only the image of the given tag | |
# this is made by using a matrix defined in a dedicated job | |
on: | |
push: | |
tags: | |
- final-* | |
branches: | |
- master | |
paths: | |
- .github/workflows/build-docker.yml | |
schedule: | |
# runs every day | |
- cron: '0 0 * * *' | |
workflow_dispatch: | |
# POST https://api.github.com/repos/qgis/QGIS/actions/workflows/2264135/dispatches: | |
permissions: | |
contents: read | |
jobs: | |
define-strategy: | |
permissions: | |
contents: none | |
if: github.repository_owner == 'qgis' | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.matrix.outputs.matrix }} | |
steps: | |
- id: matrix | |
run: | | |
if [[ "${GITHUB_REF}" =~ ^refs/tags ]]; then | |
echo "matrix={\"branch\":[\"${GITHUB_REF##*/}\"]}" >> $GITHUB_OUTPUT | |
else | |
echo "matrix={\"branch\":[\"master\", \"release-3_34\", \"release-3_36\"]}" >> $GITHUB_OUTPUT | |
fi | |
build-docker: | |
if: github.repository_owner == 'qgis' | |
runs-on: ubuntu-latest | |
needs: define-strategy | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} | |
CC: /usr/lib/ccache/gcc | |
CXX: /usr/lib/ccache/g++ # Building SIP binding freezes with Clang in Docker, maybe a SIP issue, maybe not | |
DOCKER_BUILD_DEPS_FILE: qgis3-qt5-build-deps.dockerfile | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJSON( needs.define-strategy.outputs.matrix ) }} | |
steps: | |
- name: Free Disk Space (Ubuntu) | |
uses: jlumbroso/free-disk-space@main | |
with: | |
tool-cache: true | |
large-packages: true | |
docker-images: false | |
swap-storage: true | |
- name: Free additional space | |
run: | | |
df -h | |
rm -rf /tmp/workspace | |
rm -rf /usr/share/dotnet/sdk | |
sudo apt remove llvm-* ghc-* google-chrome-* dotnet-sdk-* | |
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n | tail -n 100 | |
du -a /usr/share | sort -n -r | head -n 10 | |
du -a /usr/local/share | sort -n -r | head -n 10 | |
df -h | |
sudo apt clean | |
df -h | |
- name: Cache | |
id: cache | |
uses: actions/cache@v4 | |
with: | |
path: ~/.ccache | |
key: docker-build-${{ matrix.branch }}-${{ github.sha }} | |
restore-keys: | | |
docker-build-${{ matrix.branch }}- | |
docker-build-master- | |
- name: checkout ${{ matrix.branch }} | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ matrix.branch }} | |
- name: Define vars | |
env: | |
branch: ${{ matrix.branch }} | |
run: | | |
export DOCKER_TAG=${branch//master/latest} | |
export DOCKER_DEPS_TAG=${DOCKER_TAG} | |
# add vars for next steps | |
echo "DOCKER_TAG=${DOCKER_TAG}" >> $GITHUB_ENV | |
echo "DOCKER_DEPS_TAG=${DOCKER_DEPS_TAG}" >> $GITHUB_ENV | |
echo "branch: ${branch}" | |
echo "docker tag: ${DOCKER_TAG}" | |
echo "docker deps tag: ${DOCKER_DEPS_TAG}" | |
- name: Copy cache | |
run: | | |
[[ -d ~/.ccache ]] && echo "cache directory (~/.ccache) exists" || mkdir -p ~/.ccache | |
# copy ccache dir within QGIS source so it can be accessed from docker | |
cp -r ~/.ccache/. ./.ccache_image_build | |
- name: QGIS deps Docker pull/rebuild | |
run: | | |
cd .docker | |
docker --version | |
docker pull "qgis/qgis3-build-deps:${DOCKER_DEPS_TAG}" || true | |
docker build --cache-from "qgis/qgis3-build-deps:${DOCKER_DEPS_TAG}" -t "qgis/qgis3-build-deps:${DOCKER_DEPS_TAG}" -f ${DOCKER_BUILD_DEPS_FILE} . | |
echo "push to qgis/qgis3-build-deps:${DOCKER_DEPS_TAG}" | |
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" | |
docker push "qgis/qgis3-build-deps:${DOCKER_DEPS_TAG}" | |
- name: Docker QGIS build | |
run: | | |
cd .docker | |
DOCKER_BUILD_ARGS="--build-arg DOCKER_DEPS_TAG --build-arg CC --build-arg CXX" | |
docker build ${DOCKER_BUILD_ARGS} \ | |
--cache-from "qgis/qgis:${DOCKER_TAG}" \ | |
-t "qgis/qgis:BUILDER" \ | |
-f qgis.dockerfile .. | |
- name: Tag container and copy cache | |
run: | | |
docker run --name qgis_container qgis/qgis:BUILDER /bin/true | |
docker cp qgis_container:/QGIS/build_exit_value ./build_exit_value | |
if [[ $(cat ./build_exit_value) != "OK" ]]; then | |
echo "Build failed, not pushing image" | |
exit 1 | |
fi | |
echo "Copy build cache from Docker container to Travis cache directory" | |
rm -rf ~/.ccache/* | |
mkdir -p ~/.ccache | |
docker cp qgis_container:/QGIS/.ccache_image_build/. ~/.ccache | |
echo "Cache size: "$(du -sh ~/.ccache) | |
- name: Finalize image | |
run: | | |
cd .docker | |
# enable experimental features in Docker to squash | |
echo '{ "experimental": true}' | sudo tee /etc/docker/daemon.json | |
sudo service docker restart | |
docker build ${DOCKER_BUILD_ARGS} \ | |
--cache-from "qgis/qgis:BUILDER" \ | |
--squash \ | |
-t "qgis/qgis:${DOCKER_TAG}" \ | |
-f qgis.dockerfile .. | |
- name: Pushing image to docker hub | |
run: | | |
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" | |
docker push "qgis/qgis:${DOCKER_TAG}" | |
- name: Trigger PyQGIS API docs build for ${{ matrix.branch }} | |
if: success() && !startsWith(github.ref, 'refs/tags/') | |
env: | |
branch: ${{ matrix.branch }} | |
run: | | |
body='{ | |
"ref": "master", | |
"inputs": {"qgis_branch": "__QGIS_VERSION_BRANCH__"} | |
}' | |
body=$(sed "s/__QGIS_VERSION_BRANCH__/${branch}/;" <<< $body) | |
curl -X POST \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-H "Authorization: token ${GH_TOKEN}" \ | |
https://api.github.com/repos/qgis/pyqgis/actions/workflows/2246440/dispatches \ | |
-d "${body}" | |