Skip to content
This repository was archived by the owner on Feb 11, 2026. It is now read-only.

Commit 98971cf

Browse files
Add free-disk-space action
This action frees disk space on GitHub and EC2 runners alike by removing image bloat. Signed-off-by: Courtney Pacheco <6019922+courtneypacheco@users.noreply.github.com>
1 parent 95e615c commit 98971cf

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

33
## Contents
44
- [📙 Overview of this Repository](#-overview-of-this-repository)
5+
- [👊 Available In-House GitHub Actions](#-available-in-house-github-actions)
56
- [📬 Contributing](#-contributing)
67

78
## 📙 Overview of this Repository
89

910
This repository contains reusable, in-house GitHub actions that are specific to the InstructLab org. None of the GitHub actions provided here will be published to the GitHub Marketplace, but if you would like to use these actions in your own GitHub repo or org, feel free to use them as you see fit.
1011

12+
## 👊 Available In-House GitHub Actions
13+
14+
Below is a list of the in-house GitHub actions stored in this repository:
15+
16+
| Name | Description | Example Use Cases |
17+
| --- | --- | --- |
18+
| [free-disk-space](./actions/free-disk-space/free-disk-space.md) | Used to reclaim disk space on either a GitHub or EC2 runner. | <ul><li>If a CI job tends to fail due to "insufficient disk space"</li><li>If you want to reduce cloud costs by reclaiming disk space instead of increasing your writeable cloud storage to compensate for a bloated EC2 image</li></ul> |
19+
1120
## Contributing
1221

1322
Check out our [contributing](CONTRIBUTING/CONTRIBUTING.md) guide to learn how to contribute.

actions/free-disk-space/action.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: 'Free Disk Space'
2+
description: 'Frees disk space on the runner'
3+
runs:
4+
using: "composite"
5+
steps:
6+
- name: Print disk space before cleanup
7+
run: |
8+
df -h
9+
shell: bash
10+
- name: Free Disk Space Linux
11+
if: runner.os == 'Linux'
12+
run: |
13+
# Determine if we have Ubuntu, CentOS, or other distro as our runner OS
14+
os_id=$(grep '^ID=' /etc/os-release | cut -d "=" -f2)
15+
echo "Detected OS distro as: ${os_id}"
16+
17+
# Sometimes `docker` is not installed, so only remove images if we need to.
18+
if command -v docker 2>&1 >/dev/null ; then
19+
sudo docker rmi "$(docker image ls -aq) -f" >/dev/null 2>&1 || true
20+
fi
21+
22+
# Remove Android, .NET, and Haskell runtimes
23+
sudo rm -rf \
24+
/usr/local/lib/android \
25+
/usr/share/dotnet \
26+
/opt/ghc \
27+
/usr/local/.ghcup \
28+
/usr/local/share/powershell \
29+
/usr/share/swift \
30+
/usr/lib/jvm || true
31+
32+
printWarningMessage () {
33+
echo "[warning] Failed to remove '$1', perhaps because it doesn't exist. Ignoring..."
34+
}
35+
36+
# Remove large packages we don't use.
37+
echo "Attempting to remove unused ${os_id} packages..."
38+
if [[ "${os_id}" =~ "ubuntu" ]]; then
39+
sudo apt-get remove -y '^mysql-.*' || printWarningMessage '^mysql-.*'
40+
sudo apt-get remove -y '^dotnet-.*' --fix-missing || printWarningMessage '^dotnet-.*'
41+
sudo apt-get remove -y 'php.*' --fix-missing || printWarningMessage 'php.*'
42+
sudo apt-get remove -y '^mongodb-.*' --fix-missing || printWarningMessage '^mongodb-.*'
43+
sudo apt-get remove -y '^llvm-.*' --fix-missing || printWarningMessage '^llvm-.*'
44+
sudo apt-get remove -y google-cloud-sdk --fix-missing || printWarningMessage 'google-cloud-sdk'
45+
sudo apt-get remove -y google-cloud-cli --fix-missing || printWarningMessage 'google-cloud-cli'
46+
sudo apt-get autoremove -y >/dev/null 2>&1
47+
sudo apt-get autoclean -y >/dev/null 2>&1
48+
elif [[ "${os_id}" =~ "centos" ]]; then
49+
sudo dnf -y remove 'mysql-*' || printWarningMessage 'mysql-*'
50+
sudo dnf -y remove 'dotnet-*' || printWarningMessage 'dotnet-*'
51+
sudo dnf -y remove 'aspnetcore-*' || printWarningMessage 'aspnetcore-*'
52+
sudo dnf -y remove 'php-*' || printWarningMessage 'php-*'
53+
sudo dnf -y remove 'mongodb-*' || printWarningMessage 'mongodb-*'
54+
sudo dnf -y remove 'llvm-*' || printWarningMessage 'llvm-*'
55+
sudo dnf -y remove google-cloud-sdk || printWarningMessage 'google-cloud-sdk'
56+
sudo dnf -y remove google-cloud-cli || printWarningMessage 'google-cloud-cli'
57+
58+
# Unused Bash tools
59+
sudo dnf -y remove 'nano' || printWarningMessage 'nano'
60+
sudo dnf -y remove 'bash-completion' || printWarningMessage 'bash-completion'
61+
62+
# Remove mail transfer agents because we're not emailing anything
63+
postfix_packages=$(dnf list installed | grep postfix || echo "")
64+
if [[ ! -z "${postfix_packages}" ]]; then
65+
sudo systemctl stop postfix
66+
sudo systemctl disable postfix
67+
sudo dnf -y remove postfix
68+
fi
69+
70+
# Remove Cups because we're not printing anything
71+
cups_packages=$(dnf list installed | grep cups || echo "")
72+
if [[ ! -z "${cups_packages}" ]]; then
73+
sudo systemctl disable cups
74+
sudo systemctl stop cups
75+
sudo dnf -y remove cups
76+
fi
77+
78+
# If we're using NVIDIA, we don't need other graphics drivers provided by mesa
79+
if command -v nvidia-smi 2>&1 >/dev/null ; then
80+
sudo dnf -y remove 'mesa-*' || printWarningMessage 'mesa-*'
81+
fi
82+
83+
sudo dnf clean all
84+
rm -rf /var/cache/dnf*
85+
else
86+
echo "Skipping large package cleanup for OS '${os_id}' (not implemented)."
87+
fi
88+
shell: bash
89+
- name: Free Disk Space MacOS
90+
if: runner.os == 'macOS'
91+
run: |
92+
sudo rm -rf /System/Volumes/Data/Applications/Xcode_15*
93+
shell: bash
94+
- name: Print disk space after cleanup
95+
run: |
96+
df -h
97+
shell: bash
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Free Disk Space
2+
3+
## Overview
4+
5+
This action is used to reclaim disk space on either a GitHub runner or on an EC2 instance (EC2 runner) in AWS. When called at the beginning of a GitHub workflow job, it will attempt to remove default OS packages that are not used by InstructLab. For example: the Android, .NET, and Haksell runtimes, as well as OS packages for databases like MySQL and MongoDB. For more details on what packages are cleaned up, please review the `free-disk-space` [action.yml](./action.yml) file.
6+
7+
## How Much Space Can be Reclaimed?
8+
9+
Your mileage may vary, but historically, this action has reclaimed about 21G of space on Ubuntu 22.04 GitHub runners. See: https://github.com/instructlab/instructlab/pull/2914
10+
11+
## When to Use it?
12+
13+
Example use cases below:
14+
15+
### "Insufficient Disk Space"
16+
17+
On occasion, one or more GitHub workflow jobs in your CI may encounter an "insufficient disk space" error, either on the GitHub runner side or on the EC2 runner side.
18+
19+
If you elect to use a GitHub-hosted runner, then [the available OS images offered through GitHub are very limited](https://github.com/actions/runner-images?tab=readme-ov-file#available-images), and unfortunately, none of those OS images are "minimal". As an example, if you want your runner to use Ubuntu as its OS, there is no such `ubuntu-minimal` image available through GitHub. You must choose either their `ubuntu-latest` offering or a specific version of Ubuntu (like `ubuntu-22.04`) that they offer, and those images tend to contain bloat in the form of OS packages like Android runtimes and databases that InstructLab does not use. Therefore, the GitHub-hosted runner image tends to have 20G of "bloat" from the InstructLab perspective.
20+
21+
### Reduce Cloud Expenses
22+
23+
Whenever an InstructLab CI job is launched in the cloud, it almost always needs to have writeable storage. If a CI job is running on an EC2 instance that launches in the cloud with a significant amount of disk space consumed by unneeded OS packages, then the total _usable_, writeable storage tends to be much less than anticipated. Rather than increasing the amount of writeable storage to compensate for this "pre-consumed" storage, you can leverage this action in your GitHub workflows to reclaim disk space.
24+
25+
## How to Call this Action from a Job within a GitHub Workflow
26+
27+
Consider a simple job definition within a GitHub workflow file that is used to launch an EC2 instance in AWS. You would first call the GitHub `actions/checkout` action to "checkout" this action and store it locally.
28+
29+
Example:
30+
31+
```yaml
32+
jobs:
33+
start-ec2-runner:
34+
runs-on: ubuntu-latest
35+
steps:
36+
37+
# Checkout
38+
- name: Checkout "free-disk-space"
39+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
40+
with:
41+
repository: instructlab/ci-actions/actions/free-disk-space
42+
path: actions/free-disk-space
43+
44+
# Reference the GitHub action using the same path defined above
45+
- name: Free disk space
46+
uses: actions/free-disk-space
47+
```

0 commit comments

Comments
 (0)