diff --git a/.github/actions/test-setup-homebrew/action.yml b/.github/actions/test-setup-homebrew/action.yml new file mode 100644 index 00000000..8daec761 --- /dev/null +++ b/.github/actions/test-setup-homebrew/action.yml @@ -0,0 +1,37 @@ +--- + +name: 'Test setup-homebrew' +description: 'Runs validation against the setup-homebrew action' + +runs: + using: 'composite' + steps: + - name: 'Install BATS' + shell: bash + run: | + if [ "$RUNNER_OS" == Linux ]; then + # Add brews to the path + echo "/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin" >> $GITHUB_PATH + # Setup brew environment + eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" + fi + # Install bats + brew install bats-core + + - name: 'Checkout actions' + uses: actions/checkout@v2 + + - name: 'Create a Brewfile' + shell: bash + run: | + { + echo "brew 'tflint'" + echo "brew 'the_silver_searcher'" + } > Brewfile + + - name: 'Run setup-brew action' + uses: ./actions/setup-homebrew + + - name: 'Validate' + shell: bash + run: bats -r ${{ github.action_path }}/setup-homebrew.bats diff --git a/.github/actions/test-setup-homebrew/setup-homebrew.bats b/.github/actions/test-setup-homebrew/setup-homebrew.bats new file mode 100644 index 00000000..937b3bb2 --- /dev/null +++ b/.github/actions/test-setup-homebrew/setup-homebrew.bats @@ -0,0 +1,19 @@ +#!/usr/bin/env bats + +@test "it should have brew installed" { + run brew --version + + [ "$status" -eq 0 ] +} + +@test "it should have installed tflint" { + run tflint --version + + [ "$status" -eq 0 ] +} + +@test "it should have installed ag" { + run ag --version + + [ "$status" -eq 0 ] +} diff --git a/.github/workflows/test-setup-homebrew.yml b/.github/workflows/test-setup-homebrew.yml new file mode 100644 index 00000000..c0b99bb0 --- /dev/null +++ b/.github/workflows/test-setup-homebrew.yml @@ -0,0 +1,26 @@ +--- + +name: 'Run setup-homebrew action' + +on: + pull_request: + paths: + - actions/setup-homebrew/* + +defaults: + run: + shell: bash + +jobs: + test-setup-homebrew-action: + name: 'Uses the setup-homebrew action' + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - name: 'Checkout actions' + uses: actions/checkout@v2 + + - name: 'Test setup-brew action' + uses: ./.github/actions/test-setup-homebrew diff --git a/actions/setup-homebrew/action.yml b/actions/setup-homebrew/action.yml new file mode 100644 index 00000000..beb6cd98 --- /dev/null +++ b/actions/setup-homebrew/action.yml @@ -0,0 +1,56 @@ +--- + +name: 'Setup Homebrew' +description: 'Sets up Homebrew and installs dependencies from a Brewfile' + +inputs: + install: + description: 'Setting to false will not install the brew dependencies from the Brewfile. If the brewfile cannot be found, this boolean is disregarded in the first place.' + type: boolean + default: true + brewfile: + description: 'The Brewfile to install dependencies from.' + type: string + default: 'Brewfile' + cache: + description: 'Indicates whether to setup caching for brew dependencies' + type: boolean + default: true + working-directory: + description: 'The directory to run the setup within' + default: '.' + +runs: + using: 'composite' + steps: + - name: 'Determine Homebrew paths and conditionals' + id: info + shell: bash + working-directory: ${{ inputs.working-directory }} + run: ${{ github.action_path }}/brew-info.sh ${{ inputs.brewfile }} ${{ inputs.install }} + + - name: 'Configure Homebrew cache' + if: inputs.cache == 'true' + uses: actions/cache@v2 + with: + path: | + ${{ steps.info.outputs.cache-path }}/*--* + ${{ steps.info.outputs.cache-path }}/downloads/* + key: brew-${{ hashFiles('Brewfile') }} + restore-keys: brew- + + - name: 'Add brew path' + shell: bash + run: echo "${{ steps.info.outputs.bin-paths }}" >> $GITHUB_PATH + + - name: 'Install from brewfile' + if: steps.info.outputs.should-install == 'true' + shell: bash + working-directory: ${{ inputs.working-directory }} + env: + PREFIX_PATH: ${{ steps.info.outputs.prefix_path }} + run: | + if [ "$RUNNER_OS" = Linux ]; then + eval "$($PREFIX_PATH/bin/brew shellenv)" + fi + brew bundle install --file=${{ inputs.brewfile }} diff --git a/actions/setup-homebrew/brew-info.bats b/actions/setup-homebrew/brew-info.bats new file mode 100644 index 00000000..a542369d --- /dev/null +++ b/actions/setup-homebrew/brew-info.bats @@ -0,0 +1,69 @@ +#!/usr/bin/env bats + +load brew-info.sh + +function setup() { + pushd "$BATS_TEST_TMPDIR" >/dev/null +} + +function teardown() { + popd >/dev/null +} + +@test "it should not indicate to install if the install variable is false" { + touch Brewfile + + run brew-info Brewfile false + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=should-install::false".* ]] +} + +@test "it should not indicate to install if the file does not exist" { + run brew-info Brewfile true + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=should-install::false".* ]] +} + +@test "it should indicate to install if the file does exist and the install variable is true" { + touch Brewfile + + run brew-info Brewfile true + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=should-install::true".* ]] +} + +@test "it should output the cache path for homebrew" { + # Dynamically pull the cache path because it will run on multiple OS + local cache_path='' + cache_path="$(brew --cache)" + + run brew-info Brewfile true + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=cache-path::${cache_path}".* ]] +} + +@test "it should output the prefix path for homebrew" { + # Dynamically pull the prefix path because it will run on multiple OS + local prefix_path='' + prefix_path="$(brew --prefix)" + + run brew-info Brewfile true + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=prefix-path::${prefix_path}".* ]] +} + +@test "it should output the bin paths for homebrew" { + # Dynamically pull the bin path because it will run on multiple OS + local prefix_path='' + prefix_path="$(brew --prefix)" + + run brew-info Brewfile true + + [ "$status" -eq 0 ] + [[ "$output" =~ .*"::set-output name=bin-paths::${prefix_path}/bin:${prefix_path}/sbin".* ]] +} diff --git a/actions/setup-homebrew/brew-info.sh b/actions/setup-homebrew/brew-info.sh new file mode 100755 index 00000000..9e8482a7 --- /dev/null +++ b/actions/setup-homebrew/brew-info.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +function brew-info() { + set -eo pipefail + + local brewfile="${1:-Brewfile}" + local install="${2:-true}" + + local should_install=false + if [ "$install" = true ] && [ -f "$brewfile" ]; then + should_install=true + fi + echo "::set-output name=should-install::${should_install}" + + local cache_path='' + cache_path="$(brew --cache)" + echo "::set-output name=cache-path::${cache_path}" + + local prefix_path='' + prefix_path="$(brew --prefix)" + echo "::set-output name=prefix-path::${prefix_path}" + echo "::set-output name=bin-paths::${prefix_path}/bin:${prefix_path}/sbin" +} + +if [ "${BASH_SOURCE[0]}" = "$0" ]; then + set -u + + brew-info "${@:-}" + exit $? +fi