diff --git a/.circleci/config.yml b/.circleci/config.yml index 2281c77a1..b8446b865 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -59,6 +59,12 @@ jobs: - run: npm install - run: npm run build - run: npm run semantic-release + bump-brew-formula: + macos: + xcode: "9.0" + steps: + - checkout + - run: npm run bump-brew-formula workflows: version: 2 build_and_test: @@ -73,3 +79,6 @@ workflows: - integration - linux - osx + - bump-brew-formula: + requires: + - release \ No newline at end of file diff --git a/package.json b/package.json index 73e7bcbd7..9f7a6d6bd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "build": "npm run clean && babel lib -d dist", "build:standalone": "npm run build && pkg --targets node8-macos-x64,node8-linux-x64,node8-win-x64 --out-dir build .", "build:package": "npm run build:standalone && script/package", + "bump-brew-formula": "script/bump-brew-formula.sh", "clean": "rimraf dist", "dev": "nodemon --exec npm run build -w lib", "lint": "eslint bin lib test", diff --git a/script/bump-brew-formula.sh b/script/bump-brew-formula.sh new file mode 100755 index 000000000..a06056e60 --- /dev/null +++ b/script/bump-brew-formula.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# +# Script to automatically create a pr in the Homebrew/homebrew-core repo to update the contentful-cli formula +# (works only for macOS, with HOMEBREW_GITHUB_API_TOKEN set and a valid ssh key) + +command -v brew >/dev/null 2>&1 || /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +brew update +brew install coreutils + +DRY_RUN=" --dry-run" +if [ "${CIRCLE_BRANCH}" == "master" ]; then DRY_RUN=""; fi + +if [ ! $(git config --global user.email) ]; then git config --global user.email "support@contentful.com"; fi +if [ ! $(git config --global user.name) ]; then git config --global user.name "contentful"; fi + +package="contentful-cli" +version=$(npm show ${package} version) +url="https://registry.npmjs.org/${package}/-/${package}-${version}.tgz" +sha256=$(curl -s ${url} | gsha256sum | awk '{ print $1 }') + +# send stderr to variable, pass stdout through +{ + err=$( + brew bump-formula-pr --strict ${package} --url=${url} --sha256=${sha256} --no-browse --message="This PR was automatically created via a script. Contact @contentful with any questions." ${DRY_RUN} 2>&1 >&3 3>&- + ) +} 3>&1 + +echo "$err" + +function has_substring() { + [[ "$1" =~ "$2" ]] +} + +if has_substring "$err" "Error:"; then + # fail soft for CI (if version not bumped, pr already opened or running on feature branch) + if has_substring "$err" "You probably need to bump this formula manually"; then + echo "Soft Fail${DRY_RUN}: Potentially manual PR required" + exit 0 + fi + if has_substring "$err" "Duplicate PRs should not be opened."; then + echo "Soft Fail${DRY_RUN}: Duplicate PR found" + exit 0 + fi + echo "Hard Fail${DRY_RUN}:" + exit 1 +fi