Docker: Free up disk space before grabbing the cache when building im… #34
Workflow file for this run
This file contains hidden or 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 and Push Brev Tutorial Docker Images | |
| on: | |
| push: | |
| branches: | |
| - brev-reorg | |
| jobs: | |
| discover-tutorials: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| tutorials: ${{ steps.find-tutorials.outputs.tutorials }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Find tutorial directories | |
| id: find-tutorials | |
| run: | | |
| tutorials=$(find tutorials -mindepth 1 -maxdepth 1 -type d | jq -R -s -c 'split("\n") | map(select(length > 0))') | |
| echo "tutorials=${tutorials}" >> $GITHUB_OUTPUT | |
| echo "Found tutorials: ${tutorials}" | |
| build-and-push: | |
| needs: discover-tutorials | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| matrix: | |
| tutorial: ${{ fromJson(needs.discover-tutorials.outputs.tutorials) }} | |
| fail-fast: false | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Free up disk space | |
| run: | | |
| echo "Disk space before cleanup:" | |
| df -h | |
| # Remove unnecessary tools and files to free up space | |
| sudo rm -rf /usr/share/dotnet | |
| sudo rm -rf /usr/local/lib/android | |
| sudo rm -rf /opt/ghc | |
| sudo rm -rf /opt/hostedtoolcache/CodeQL | |
| # Clean up Docker | |
| docker system prune -af --volumes | |
| echo "Disk space after cleanup:" | |
| df -h | |
| - name: Get branch name and short SHA | |
| run: | | |
| echo "GIT_BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV | |
| echo "GIT_SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV | |
| - name: Set image name | |
| id: set-image | |
| run: | | |
| image_name="ghcr.io/${{ github.repository_owner }}/$(basename ${{ matrix.tutorial }})-tutorial" | |
| echo "image_name=${image_name,,}" >> $GITHUB_OUTPUT | |
| - name: Check for HPCCM recipe | |
| id: check-hpccm | |
| run: | | |
| if [ -f "${{ matrix.tutorial }}/brev/docker-recipe.py" ]; then | |
| echo "has_hpccm=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_hpccm=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set up Python (for HPCCM) | |
| if: steps.check-hpccm.outputs.has_hpccm == 'true' | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.x' | |
| - name: Install HPCCM | |
| if: steps.check-hpccm.outputs.has_hpccm == 'true' | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install hpccm | |
| - name: Generate Dockerfile from HPCCM recipe | |
| if: steps.check-hpccm.outputs.has_hpccm == 'true' | |
| run: | | |
| hpccm --recipe ${{ matrix.tutorial }}/brev/docker-recipe.py --format docker > ${{ matrix.tutorial }}/brev/dockerfile | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata from Docker Compose | |
| id: compose-config | |
| run: | | |
| # This uses jq which is pre-installed on GitHub Actions runners | |
| COMPOSE_DIR="${{ matrix.tutorial }}/brev" | |
| cd "$COMPOSE_DIR" | |
| # `docker compose config` outputs the resolved configuration | |
| # Extract context and dockerfile for the 'base' service | |
| CONTEXT=$(docker compose config --format json | jq -r '.services.base.build.context // "."') | |
| DOCKERFILE=$(docker compose config --format json | jq -r '.services.base.build.dockerfile // "Dockerfile"') | |
| # Resolve paths relative to the docker-compose.yml location | |
| # Then convert to paths relative to the repo root | |
| CONTEXT_ABS=$(realpath -m "$CONTEXT") | |
| CONTEXT_REL=$(realpath -m --relative-to="${GITHUB_WORKSPACE}" "$CONTEXT_ABS") | |
| # Dockerfile path is relative to the context in docker-compose | |
| # For build-push-action, it should be relative to the repo root | |
| DOCKERFILE_ABS=$(realpath -m "$CONTEXT/$DOCKERFILE") | |
| DOCKERFILE_REL=$(realpath -m --relative-to="${GITHUB_WORKSPACE}" "$DOCKERFILE_ABS") | |
| echo "context=${CONTEXT_REL}" >> $GITHUB_OUTPUT | |
| echo "dockerfile=${DOCKERFILE_REL}" >> $GITHUB_OUTPUT | |
| echo "Using context: ${CONTEXT_REL}, dockerfile: ${DOCKERFILE_REL}" | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{ steps.compose-config.outputs.context }} | |
| file: ${{ steps.compose-config.outputs.dockerfile }} | |
| build-args: | | |
| GIT_BRANCH_NAME=${{ env.GIT_BRANCH_NAME }} | |
| tags: | | |
| ${{ steps.set-image.outputs.image_name }}:${{ env.GIT_BRANCH_NAME }}-latest | |
| ${{ steps.set-image.outputs.image_name }}:${{ env.GIT_BRANCH_NAME }}-git-${{ env.GIT_SHORT_SHA }} | |
| push: true | |
| cache-from: type=registry,ref=${{ steps.set-image.outputs.image_name }}:buildcache | |
| cache-to: type=registry,ref=${{ steps.set-image.outputs.image_name }}:buildcache,mode=max |