Skip to content

Commit 83fd28a

Browse files
committed
ci: Action for installing GCC and LLVM from homebrew
1 parent c059701 commit 83fd28a

6 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Homebrew packages setup
2+
description: Setup packages through Homebrew
3+
inputs:
4+
cache-mode:
5+
default: use
6+
description: "Mode of operation regarding cache: 'prepare' or 'use'"
7+
required: false
8+
packages-hash-to-use:
9+
description: "In 'use' mode, the hash of the package listing to use. From "
10+
required: false
11+
outputs:
12+
packages-hash:
13+
description: "Hash of installed packages listing. To be used in subsequent 'use' mode executions."
14+
value: ${{ steps.list-result-packages.outputs.packages-hash }}
15+
16+
runs:
17+
using: composite
18+
steps:
19+
- name: Setup Homebrew
20+
uses: Homebrew/actions/setup-homebrew@b2da65092292aade9599608fc29415389641b3cb # main
21+
22+
- id: init
23+
24+
env:
25+
INPUT_CACHE_MODE: ${{ inputs.cache-mode }}
26+
INPUT_PACKAGES_HASH_TO_USE: ${{ inputs.packages-hash-to-use }}
27+
# Major versions of GCC and LLVM to use
28+
GCC_MAJOR_VERSION: 15
29+
LLVM_MAJOR_VERSION: 21
30+
run: |
31+
# Initialize homebrew-packages-setup action
32+
"${GITHUB_ACTION_PATH}/init.sh"
33+
shell: bash
34+
35+
- id: cache-restore
36+
name: Mise cache restore
37+
uses: actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
38+
with:
39+
key: homebrew-${{ runner.os }}-${{ steps.init.outputs.packages-major-versions-hash }}-${{ inputs.packages-hash-to-use }}
40+
path: ${{ steps.init.outputs.cache-paths }}
41+
restore-keys: |
42+
homebrew-${{ runner.os }}-${{ steps.init.outputs.packages-major-versions-hash }}-
43+
homebrew-${{ runner.os }}-
44+
45+
- id: list-initial-packages
46+
47+
env:
48+
CACHE_HIT: ${{ steps.cache-restore.outputs.cache-hit }}
49+
INPUT_PACKAGES_HASH_TO_USE: ${{ inputs.packages-hash-to-use }}
50+
run: |
51+
# List initially installed packages
52+
"${GITHUB_ACTION_PATH}/list-initial-packages.sh"
53+
shell: bash
54+
55+
- if: inputs.cache-mode == 'prepare' || steps.cache-restore.outputs.cache-hit != 'true'
56+
57+
env:
58+
GCC_MAJOR_VERSION: ${{ steps.init.outputs.gcc-major-version }}
59+
INITIAL_PACKAGES: ${{ steps.list-initial-packages.outputs.packages }}
60+
LLVM_MAJOR_VERSION: ${{ steps.init.outputs.llvm-major-version }}
61+
run: |
62+
# Install and upgrade homebrew packages
63+
"${GITHUB_ACTION_PATH}/install-packages.sh"
64+
shell: bash
65+
66+
- id: list-result-packages
67+
68+
env:
69+
INITIAL_PACKAGES: ${{ steps.list-initial-packages.outputs.packages }}
70+
INITIAL_PACKAGES_HASH: ${{ steps.list-initial-packages.outputs.packages-hash }}
71+
INPUT_CACHE_MODE: ${{ inputs.cache-mode }}
72+
run: |
73+
# List installed packages after install/upgrade
74+
"${GITHUB_ACTION_PATH}/list-result-packages.sh"
75+
shell: bash
76+
77+
- if: steps.list-result-packages.outputs.save-cache == 'true'
78+
name: Save cache
79+
uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
80+
with:
81+
key: ${{ steps.cache-restore.outputs.cache-primary-key }}-${{ steps.list-result-packages.outputs.packages-hash }}
82+
path: ${{ steps.init.outputs.cache-paths }}
83+
84+
- run: |
85+
# Set final changes for rest of workflow
86+
"${GITHUB_ACTION_PATH}/final-changes.sh"
87+
shell: bash
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
echo "::group::Environment changes for rest of workflow"
6+
{
7+
echo "HOMEBREW_NO_AUTO_UPDATE=1"
8+
} | tee -a "${GITHUB_ENV}"
9+
echo "::endgroup::"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Validate inputs
6+
if [[ -z "${GCC_MAJOR_VERSION}" ]]; then
7+
echo "::error::GCC_MAJOR_VERSION is not set"
8+
exit 1
9+
fi
10+
if [[ -z "${LLVM_MAJOR_VERSION}" ]]; then
11+
echo "::error::LLVM_MAJOR_VERSION is not set"
12+
exit 1
13+
fi
14+
15+
case "${INPUT_CACHE_MODE}" in
16+
prepare)
17+
if [[ -n "${INPUT_PACKAGES_HASH_TO_USE}" ]]; then
18+
echo "::error::packages-hash-to-use input must be empty in 'prepare' mode"
19+
exit 1
20+
fi
21+
;;
22+
use)
23+
;;
24+
*)
25+
echo "::error::Unknown cache mode: ${INPUT_CACHE_MODE}"
26+
exit 1
27+
;;
28+
esac
29+
30+
# Prepare output values
31+
brewPrefix=$(brew --prefix)
32+
packagesMajorVersionsHash=$(echo "gcc-${GCC_MAJOR_VERSION} llvm-${LLVM_MAJOR_VERSION}" | sha256sum | cut -d ' ' -f 1 )
33+
34+
echo "::group::Outputs from init"
35+
{
36+
echo "gcc-major-version=${GCC_MAJOR_VERSION}"
37+
echo "llvm-major-version=${LLVM_MAJOR_VERSION}"
38+
echo "packages-major-versions-hash=${packagesMajorVersionsHash}"
39+
40+
# Homebrew cache paths
41+
echo "cache-paths<<ENDPATHS"
42+
echo "${brewPrefix}"
43+
echo "ENDPATHS"
44+
} | tee -a "${GITHUB_OUTPUT}"
45+
echo "::endgroup::"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
echo "::group::Upgrade packages"
6+
brew upgrade
7+
echo "::endgroup::"
8+
9+
echo "::group::Uninstall wrong versions"
10+
gccWrongInstalledVersions=$(echo "${INITIAL_PACKAGES}" | grep "gcc.* " | grep -v "${GCC_MAJOR_VERSION}" || true)
11+
llvmWrongInstalledVersions=$(echo "${INITIAL_PACKAGES}" | grep "llvm.* " | grep -v "${LLVM_MAJOR_VERSION}" || true)
12+
echo "GCC versions to uninstall: ${gccWrongInstalledVersions}"
13+
echo "LLVM versions to uninstall: ${llvmWrongInstalledVersions}"
14+
if [[ -n "${gccWrongInstalledVersions}" ]]; then
15+
echo "${gccWrongInstalledVersions}" | xargs brew uninstall
16+
fi
17+
if [[ -n "${llvmWrongInstalledVersions}" ]]; then
18+
echo "${llvmWrongInstalledVersions}" | xargs brew uninstall
19+
fi
20+
echo "::endgroup::"
21+
22+
echo "::group::Ensure correct versions are installed"
23+
brew install "gcc@${GCC_MAJOR_VERSION}"
24+
brew install "llvm@${LLVM_MAJOR_VERSION}"
25+
echo "::endgroup::"
26+
27+
echo "::group::Run brew cleanup"
28+
brew cleanup
29+
echo "::endgroup::"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
if [[ "${CACHE_HIT}" == "true" && -z "${INPUT_PACKAGES_HASH_TO_USE}" ]]; then
6+
echo "::error::INPUT_PACKAGES_HASH_TO_USE is not set but cache hit is true"
7+
exit 1
8+
fi
9+
10+
echo "::group::List initially installed packages"
11+
installedPackages=$(brew ls --versions)
12+
echo "${installedPackages}"
13+
echo "::endgroup::"
14+
15+
installedPackagesHash=$(echo "${installedPackages}" | sha256sum | cut -d ' ' -f 1 )
16+
if [[ "${CACHE_HIT}" == "true" && "${INPUT_PACKAGES_HASH_TO_USE}" != "${installedPackagesHash}" ]]; then
17+
echo "::error::INPUT_PACKAGES_HASH_TO_USE does not match installed packages hash after cache hit"
18+
exit 1
19+
fi
20+
21+
echo "::group::Outputs from list-initial-packages"
22+
{
23+
echo "packages-hash=${installedPackagesHash}"
24+
25+
echo "packages<<ENDPACKAGES"
26+
echo "${installedPackages}"
27+
echo "ENDPACKAGES"
28+
} | tee -a "${GITHUB_OUTPUT}"
29+
echo "::endgroup::"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
echo "::group::List installed packages"
6+
installedPackages=$(brew ls --versions)
7+
echo "${installedPackages}"
8+
echo "::endgroup::"
9+
10+
echo "::group::Compare installed packages with initial packages"
11+
installedPackagesHash=$(echo "${installedPackages}" | sha256sum | cut -d ' ' -f 1 )
12+
if [[ "${INITIAL_PACKAGES_HASH}" = "${installedPackagesHash}" ]]; then
13+
packagesChanged=false
14+
echo "Installed packages were not changed by install/upgrade"
15+
if [[ "${INITIAL_PACKAGES}" != "${installedPackages}" ]]; then
16+
echo "::error::Installed packages are different from initial packages but hashes match"
17+
exit 1
18+
fi
19+
else
20+
packagesChanged=true
21+
echo "Installed packages were changed during install/upgrade"
22+
diff --unified=0 <(echo "${INITIAL_PACKAGES}") <(echo "${installedPackages}") || true
23+
fi
24+
echo "::endgroup::"
25+
26+
if [[ "${INPUT_CACHE_MODE}" == "prepare" && "${packagesChanged}" == "true" ]]; then
27+
saveCache=true
28+
else
29+
saveCache=false
30+
fi
31+
32+
echo "::group::Outputs from list-result-packages"
33+
{
34+
echo "packages-hash=${installedPackagesHash}"
35+
echo "save-cache=${saveCache}"
36+
} | tee -a "${GITHUB_OUTPUT}"
37+
echo "::endgroup::"

0 commit comments

Comments
 (0)