easimon / maximize-build-space

Github action to maximize the available disk space on Github runners
MIT License
346 stars 68 forks source link

support docker build workflows #41

Open jianlins opened 3 months ago

jianlins commented 3 months ago

The first three steps will work for docker build, can you integrate it into the action?

jobs:
  build_docker:
    runs-on: ubuntu-latest
    steps:
      - name: Backup docker files
        run: |
          echo "backup moby/buildkit image"
          sudo docker image save -o ${GITHUB_WORKSPACE}/images.tar moby/buildkit
          echo "Back up /var/lib/docker folder structure and other files"
          sudo rsync -aPq /var/lib/docker/ ${GITHUB_WORKSPACE}/docker 

      - name: Maximize build space
        uses: easimon/maximize-build-space@master
        with:
          overprovision-lvm: 'true'          
          remove-dotnet: 'true'
          # instead of using default value to mount to build path, /var/lib/docker/ is really the place we need more spaces.
          build-mount-path: '/var/lib/docker/'

      - name: Restore docker files
        run: |
          sudo rsync -aPq ${GITHUB_WORKSPACE}/docker/ /var/lib/docker
          sudo rm -rf ${GITHUB_WORKSPACE}/docker
          sudo ls ${GITHUB_WORKSPACE} -l
          sudo docker image load -i ${GITHUB_WORKSPACE}/images.tar
          sudo rm ${GITHUB_WORKSPACE}/images.tar

      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3      

      - name: list images
        run: docker images

      - name: check storage
        run: |
          sudo df -h
gionn commented 3 months ago

I guess it's faster to stop the docker service (if possibile), rename the current /var/lib/docker to something else, run the action, rsync back to the new /var/lib/docker and start docker again

jianlins commented 3 months ago

Tried. it doesn't work. I've tried a tone of ways to change docker data root directory, although it works on local machine, it doesn't work in runs. And start docker and stop docker commands behave weirdly. You should give it a try. Maybe there are other ways.

j3soon commented 3 months ago

Simply restarting the docker service seems to work for me:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Maximize build space
        uses: easimon/maximize-build-space@master
        with:
          build-mount-path: /var/lib/docker/
          remove-dotnet: 'true'
          remove-android: 'true'
          remove-haskell: 'true'
          remove-codeql: 'true'
          remove-docker-images: 'true'
      - name: Restart docker
        run: sudo service docker restart
      - name: Checkout
        uses: actions/checkout@v4
jianlins commented 3 months ago

Have you worked further to check the storage? It seems restarted , but still not saving to the mounted folder.

sheepymeh commented 3 months ago

If you want to preserve the contents of /var/lib/docker, I've found that the following snippet seems to work:

- name: Move /var/lib/docker/
  run: sudo mv /var/lib/docker/ "${GITHUB_WORKSPACE}/docker"

- name: Maximize build space
  uses: easimon/maximize-build-space@master
  with:
    root-reserve-mb: 512
    temp-reserve-mb: 32
    swap-size-mb: 32
    remove-dotnet: 'true'
    remove-android: 'true'
    remove-haskell: 'true'
    remove-codeql: 'true'
    build-mount-path: '/var/lib/docker/'

- name: Restore /var/lib/docker/
  run: sudo sh -c "mv ${GITHUB_WORKSPACE}/docker/* /var/lib/docker"

You shouldn't use remove-docker-images in this case because it would run docker image prune, which doesn't work after /var/lib/docker is moved. You can replicate its behavior by adding a docker image prune --all --force step beforehand.