Skip to content

Commit

Permalink
Adds an action for setting up brew and installing from brewfile (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-carey authored Sep 29, 2022
1 parent 5a4fd77 commit d93dcbb
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/actions/test-setup-homebrew/action.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions .github/actions/test-setup-homebrew/setup-homebrew.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}
26 changes: 26 additions & 0 deletions .github/workflows/test-setup-homebrew.yml
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions actions/setup-homebrew/action.yml
Original file line number Diff line number Diff line change
@@ -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 }}
69 changes: 69 additions & 0 deletions actions/setup-homebrew/brew-info.bats
Original file line number Diff line number Diff line change
@@ -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".* ]]
}
30 changes: 30 additions & 0 deletions actions/setup-homebrew/brew-info.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d93dcbb

Please sign in to comment.