Skip to content

Commit 9a7313d

Browse files
authored
feat: Add free-disk-space action (#13)
* feat: add free-disk-space action * chore: use free-disk-space action
1 parent 0c5dbc4 commit 9a7313d

File tree

6 files changed

+153
-30
lines changed

6 files changed

+153
-30
lines changed

.github/workflows/pr_actions-smoke-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
- .github/workflows/pr_actions-smoke-test.yml
99
- build-container-image/action.yml
1010
- build-product-image/action.yml
11+
- free-disk-space/action.yml
1112
- publish-image/action.yml
1213
- publish-index-manifest/action.yml
1314
- shard/action.yml

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ particular step in a workflow.
2121

2222
- [build-container-image](./build-container-image/README.md)
2323
- [build-product-image](./build-product-image/README.md)
24+
- [free-disk-space](./free-disk-space/README.md)
2425
- [publish-image](./publish-image/README.md)
2526
- [publish-index-manifest](./publish-index-manifest/README.md)
2627
- [run-pre-commit](./run-pre-commit/README.md)

build-container-image/action.yml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,8 @@ outputs:
4242
runs:
4343
using: composite
4444
steps:
45-
- name: Free Disk Space (Ubuntu)
46-
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
47-
with:
48-
# This might remove tools that are actually needed, if set to "true" but
49-
# frees about 6 GB.
50-
tool-cache: false
51-
52-
# All of these default to true, but feel free to set to "false" if
53-
# necessary for your workflow.
54-
android: true
55-
dotnet: true
56-
haskell: true
57-
large-packages: true
58-
docker-images: true
59-
swap-storage: true
45+
- name: Free Disk Space
46+
uses: ./free-disk-space
6047

6148
- name: Setup Docker Buildx
6249
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1

build-product-image/action.yml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,8 @@ outputs:
2929
runs:
3030
using: composite
3131
steps:
32-
- name: Free Disk Space (Ubuntu)
33-
uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
34-
with:
35-
# This might remove tools that are actually needed, if set to "true" but
36-
# frees about 6 GB.
37-
tool-cache: false
38-
39-
# All of these default to true, but feel free to set to "false" if
40-
# necessary for your workflow.
41-
android: true
42-
dotnet: true
43-
haskell: true
44-
large-packages: true
45-
docker-images: true
46-
swap-storage: true
32+
- name: Free Disk Space
33+
uses: ./free-disk-space
4734

4835
- name: Setup Docker Buildx
4936
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1

free-disk-space/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `free-disk-space`
2+
3+
> Manifest: [free-disk-space/action.yml][free-disk-space]
4+
5+
This action This action frees up disk space on a runner.
6+
7+
It is based on `jlumbroso/free-disk-space`and runs cleanup tasks in parallel where possible, and hides the STDOUT output of each task to reduce noise in workflow runs.
8+
9+
> [!NOTE]
10+
> This action is used by the [build-container-image] and [build-product-image] actions.
11+
12+
## Inputs and Outputs
13+
14+
> [!TIP]
15+
> For descriptions of the inputs and outputs, see the complete [free-disk-space] action.
16+
17+
### Inputs
18+
19+
None
20+
21+
### Outputs
22+
23+
None
24+
25+
[free-disk-space]: ./action.yml
26+
[build-container-image]: ../build-container-image/action.yml
27+
[build-product-image]: ../build-product-image/action.yml

free-disk-space/action.yml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
name: Free Disk Space
3+
description: This action frees up disk space on a runner.
4+
inputs:
5+
product-name:
6+
description: The name of the product to build via bake (directory name)
7+
required: true
8+
config-file:
9+
description: Path the the config file used to generate the shard indices
10+
default: ./conf.py
11+
outputs:
12+
versions:
13+
description: A list of product versions
14+
value: ${{ steps.generate_shards.outputs.VERSIONS }}
15+
runs:
16+
using: composite
17+
steps:
18+
- name: Free Disk Space (Parallel)
19+
shell: bash
20+
run: |
21+
# WARNING: `set -e` is not set as we purposefully want this to run optimistically.
22+
23+
function cleanup_android() {
24+
(
25+
sudo rm -rf /usr/local/lib/android || true
26+
)>/dev/null
27+
}
28+
29+
function cleanup_apt() {
30+
(
31+
sudo apt-get remove --fix-missing -y '^aspnetcore-.*' \
32+
'^dotnet-.*' \
33+
'^llvm-.*' \
34+
'php.*' \
35+
'^mongodb-.*' \
36+
'^mysql-.*' \
37+
azure-cli \
38+
google-chrome-stable \
39+
firefox \
40+
powershell \
41+
mono-devel \
42+
libgl1-mesa-dri \
43+
google-cloud-sdk \
44+
google-cloud-cli || true
45+
sudo apt-get autoremove -y
46+
sudo apt-get clean
47+
)>/dev/null
48+
}
49+
50+
function cleanup_dotnet() {
51+
(
52+
sudo rm -rf /usr/share/dotnet || true
53+
)>/dev/null
54+
}
55+
56+
function cleanup_haskell() {
57+
(
58+
sudo rm -rf /opt/ghc || true
59+
sudo rm -rf /usr/local/.ghcup || true
60+
)>/dev/null
61+
}
62+
63+
function cleanup_docker() {
64+
(
65+
sudo docker image prune --all --force || true
66+
)>/dev/null
67+
}
68+
69+
function cleanup_agenttools() {
70+
(
71+
sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
72+
)>/dev/null
73+
}
74+
75+
function disable_swap() {
76+
(
77+
sudo swapoff -a || true
78+
sudo rm -f /mnt/swapfile || true
79+
free -h
80+
)>/dev/null
81+
}
82+
83+
# The bash `time` built-in can be given subshells, and the output format
84+
# is configured via the TIMEFORMAT environment variable.
85+
# The binary `time` output format can be configured via a `--format`
86+
# flag.
87+
# Since we are calling the cleanup functions in parallel, we need to
88+
# wrap the built-in `time` so that each concurrent invokation doesn't
89+
# affect the output format of another.
90+
function time_it() {
91+
local MESSAGE="$1"
92+
local FUNCTION="$2"
93+
local TIMEFORMAT="$MESSAGE (%Rs)"
94+
time "$FUNCTION"
95+
}
96+
97+
echo "::group::Disk usage before"
98+
df -h /
99+
echo "::endgroup::"
100+
101+
echo "::group::Parallel cleanup"
102+
103+
time_it 'Removed Android libraries' cleanup_android &
104+
time_it 'Removed apt packages' cleanup_apt &
105+
time_it 'Removed dotnet packages' cleanup_dotnet &
106+
time_it 'Removed haskell' cleanup_haskell &
107+
time_it 'Pruned docker images' cleanup_docker &
108+
time_it 'Disabled swap' disable_swap &
109+
110+
# This might remove tools that are actually needed, if set to "true" but
111+
# frees about 6 GB.
112+
# time_it 'Removed agent tools' cleanup_agenttools &
113+
114+
echo "Waiting for cleanup tasks"
115+
wait
116+
echo "::endgroup::"
117+
118+
echo "::group::Disk usage after"
119+
df -h /
120+
echo "::endgroup::"

0 commit comments

Comments
 (0)