Nenhuma Descrição

action.yml 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. name: "Free Disk Space (Ubuntu)"
  2. description: "A configurable GitHub Action to free up disk space on an Ubuntu GitHub Actions runner."
  3. # Thanks @jlumbroso for the action code https://github.com/jlumbroso/free-disk-space/
  4. # See: https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#branding
  5. inputs:
  6. android:
  7. description: "Remove Android runtime"
  8. required: false
  9. default: "true"
  10. dotnet:
  11. description: "Remove .NET runtime"
  12. required: false
  13. default: "true"
  14. haskell:
  15. description: "Remove Haskell runtime"
  16. required: false
  17. default: "true"
  18. # option inspired by:
  19. # https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
  20. large-packages:
  21. description: "Remove large packages"
  22. required: false
  23. default: "true"
  24. docker-images:
  25. description: "Remove Docker images"
  26. required: false
  27. default: "true"
  28. # option inspired by:
  29. # https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
  30. tool-cache:
  31. description: "Remove image tool cache"
  32. required: false
  33. default: "false"
  34. swap-storage:
  35. description: "Remove swap storage"
  36. required: false
  37. default: "true"
  38. runs:
  39. using: "composite"
  40. steps:
  41. - shell: bash
  42. run: |
  43. # ======
  44. # MACROS
  45. # ======
  46. # macro to print a line of equals
  47. # (silly but works)
  48. printSeparationLine() {
  49. str=${1:=}
  50. num=${2:-80}
  51. counter=1
  52. output=""
  53. while [ $counter -le $num ]
  54. do
  55. output="${output}${str}"
  56. counter=$((counter+1))
  57. done
  58. echo "${output}"
  59. }
  60. # macro to compute available space
  61. # REF: https://unix.stackexchange.com/a/42049/60849
  62. # REF: https://stackoverflow.com/a/450821/408734
  63. getAvailableSpace() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); }
  64. # macro to make Kb human readable (assume the input is Kb)
  65. # REF: https://unix.stackexchange.com/a/44087/60849
  66. formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); }
  67. # macro to output saved space
  68. printSavedSpace() {
  69. saved=${1}
  70. title=${2:-}
  71. echo ""
  72. printSeparationLine '*' 80
  73. if [ ! -z "${title}" ]; then
  74. echo "=> ${title}: Saved $(formatByteCount $saved)"
  75. else
  76. echo "=> Saved $(formatByteCount $saved)"
  77. fi
  78. printSeparationLine '*' 80
  79. echo ""
  80. }
  81. # macro to print output of dh with caption
  82. printDH() {
  83. caption=${1:-}
  84. printSeparationLine '=' 80
  85. echo "${caption}"
  86. echo ""
  87. echo "$ dh -h /"
  88. echo ""
  89. df -h /
  90. echo "$ dh -a /"
  91. echo ""
  92. df -a /
  93. echo "$ dh -a"
  94. echo ""
  95. df -a
  96. printSeparationLine '=' 80
  97. }
  98. # ======
  99. # SCRIPT
  100. # ======
  101. # Display initial disk space stats
  102. AVAILABLE_INITIAL=$(getAvailableSpace)
  103. AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
  104. printDH "BEFORE CLEAN-UP:"
  105. echo ""
  106. # Option: Remove Android library
  107. if [[ ${{ inputs.android }} == 'true' ]]; then
  108. BEFORE=$(getAvailableSpace)
  109. sudo rm -rf /usr/local/lib/android || true
  110. AFTER=$(getAvailableSpace)
  111. SAVED=$((AFTER-BEFORE))
  112. printSavedSpace $SAVED "Android library"
  113. fi
  114. # Option: Remove .NET runtime
  115. if [[ ${{ inputs.dotnet }} == 'true' ]]; then
  116. BEFORE=$(getAvailableSpace)
  117. # https://github.community/t/bigger-github-hosted-runners-disk-space/17267/11
  118. sudo rm -rf /usr/share/dotnet || true
  119. AFTER=$(getAvailableSpace)
  120. SAVED=$((AFTER-BEFORE))
  121. printSavedSpace $SAVED ".NET runtime"
  122. fi
  123. # Option: Remove Haskell runtime
  124. if [[ ${{ inputs.haskell }} == 'true' ]]; then
  125. BEFORE=$(getAvailableSpace)
  126. sudo rm -rf /opt/ghc || true
  127. sudo rm -rf /usr/local/.ghcup || true
  128. AFTER=$(getAvailableSpace)
  129. SAVED=$((AFTER-BEFORE))
  130. printSavedSpace $SAVED "Haskell runtime"
  131. fi
  132. # Option: Remove large packages
  133. # REF: https://github.com/apache/flink/blob/master/tools/azure-pipelines/free_disk_space.sh
  134. if [[ ${{ inputs.large-packages }} == 'true' ]]; then
  135. BEFORE=$(getAvailableSpace)
  136. sudo apt-get remove -y '^aspnetcore-.*' || echo "::warning::The command [sudo apt-get remove -y '^aspnetcore-.*'] failed to complete successfully. Proceeding..."
  137. sudo apt-get remove -y '^dotnet-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^dotnet-.*' --fix-missing] failed to complete successfully. Proceeding..."
  138. sudo apt-get remove -y '^llvm-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^llvm-.*' --fix-missing] failed to complete successfully. Proceeding..."
  139. sudo apt-get remove -y 'php.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y 'php.*' --fix-missing] failed to complete successfully. Proceeding..."
  140. sudo apt-get remove -y '^mongodb-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mongodb-.*' --fix-missing] failed to complete successfully. Proceeding..."
  141. sudo apt-get remove -y '^mysql-.*' --fix-missing || echo "::warning::The command [sudo apt-get remove -y '^mysql-.*' --fix-missing] failed to complete successfully. Proceeding..."
  142. sudo apt-get remove -y azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri --fix-missing || echo "::warning::The command [sudo apt-get remove -y azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri --fix-missing] failed to complete successfully. Proceeding..."
  143. sudo apt-get remove -y google-cloud-sdk --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-sdk --fix-missing] failed to complete successfully. Proceeding..."
  144. sudo apt-get remove -y google-cloud-cli --fix-missing || echo "::debug::The command [sudo apt-get remove -y google-cloud-cli --fix-missing] failed to complete successfully. Proceeding..."
  145. sudo apt-get autoremove -y || echo "::warning::The command [sudo apt-get autoremove -y] failed to complete successfully. Proceeding..."
  146. sudo apt-get clean || echo "::warning::The command [sudo apt-get clean] failed to complete successfully. Proceeding..."
  147. AFTER=$(getAvailableSpace)
  148. SAVED=$((AFTER-BEFORE))
  149. printSavedSpace $SAVED "Large misc. packages"
  150. fi
  151. # Option: Remove Docker images
  152. if [[ ${{ inputs.docker-images }} == 'true' ]]; then
  153. BEFORE=$(getAvailableSpace)
  154. sudo docker image prune --all --force || true
  155. AFTER=$(getAvailableSpace)
  156. SAVED=$((AFTER-BEFORE))
  157. printSavedSpace $SAVED "Docker images"
  158. fi
  159. # Option: Remove tool cache
  160. # REF: https://github.com/actions/virtual-environments/issues/2875#issuecomment-1163392159
  161. if [[ ${{ inputs.tool-cache }} == 'true' ]]; then
  162. BEFORE=$(getAvailableSpace)
  163. sudo rm -rf "$AGENT_TOOLSDIRECTORY" || true
  164. AFTER=$(getAvailableSpace)
  165. SAVED=$((AFTER-BEFORE))
  166. printSavedSpace $SAVED "Tool cache"
  167. fi
  168. # Option: Remove Swap storage
  169. if [[ ${{ inputs.swap-storage }} == 'true' ]]; then
  170. BEFORE=$(getAvailableSpace)
  171. sudo swapoff -a || true
  172. sudo rm -f /mnt/swapfile || true
  173. free -h
  174. AFTER=$(getAvailableSpace)
  175. SAVED=$((AFTER-BEFORE))
  176. printSavedSpace $SAVED "Swap storage"
  177. fi
  178. # Output saved space statistic
  179. AVAILABLE_END=$(getAvailableSpace)
  180. AVAILABLE_ROOT_END=$(getAvailableSpace '/')
  181. echo ""
  182. printDH "AFTER CLEAN-UP:"
  183. echo ""
  184. echo ""
  185. echo "/dev/root:"
  186. printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
  187. echo "overall:"
  188. printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))