Skip to content

Instantly share code, notes, and snippets.

@chris-gillatt
Last active May 13, 2024 09:11
Show Gist options
  • Save chris-gillatt/144b97458e1dab65355f209559424d97 to your computer and use it in GitHub Desktop.
Save chris-gillatt/144b97458e1dab65355f209559424d97 to your computer and use it in GitHub Desktop.
package.sh
#!/bin/bash -e
# Brew package script for GitHub Actions
# Author: C Gillatt | https://github.com/chris-gillatt | Provided under the MIT license
# Prefix log output with the name of the script for easy identification.
announce () {
echo "$(basename "$0"): $*"
} # End announce
announce "Starting up @ $(date)"
# Set app and Repository name here.
export APP="some-app"
REPO_NAME="homebrew-some-app"
GIT_REVISION=$(git rev-parse HEAD)
# Compress new app version and place in a directory for seperation.
# This is useful when there's several files to package up too.
mkdir dist
tar cf dist/"${APP}-0.0.${GITHUB_RUN_NUMBER}.tar.gz" "$APP"
# Generate Ruby file for Brew using the erb template.
erb "${APP}.erb" > "${APP}.rb"
## Commit the new ruby file back to the repository.
git add "${APP}.rb"
# Commit and push files to repo
git commit -m "$APP release 0.0.${GITHUB_RUN_NUMBER}"
git push
# Create a json payload to help us create a release with GitHub tags.
post_release_json()
{
cat <<EOF
{
"tag_name": "0.0.${GITHUB_RUN_NUMBER}",
"target_commitish": "${GIT_REVISION}",
"name": "0.0.${GITHUB_RUN_NUMBER}"
}
EOF
}
announce "Creating release"
NEW_RELEASE_RESPONSE=$(curl --silent \
--write-out "\n%{http_code}" \
-u "$CG_GITHUB_USERNAME:$CG_GITHUB_PAT" \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST "https://api.github.com/repos/${CG_ORG}/${REPO_NAME}/releases" \
--data "$(post_release_json)")
STATUS_CODE=$(echo "$NEW_RELEASE_RESPONSE" | tail -n 1)
NEW_RELEASE=$(echo "$NEW_RELEASE_RESPONSE" | sed '$d')
if [[ $STATUS_CODE -ge 400 ]]; then
announce 'ERROR: Failed to create release'
announce "$STATUS_CODE"
announce "$NEW_RELEASE"
exit 1
fi
announce "Release created successfully"
UPLOAD_URL=$(echo "$NEW_RELEASE" | jq -r .upload_url | cut -f1 -d"{")
announce "Uploading binaries"
curl --fail \
-u "${CG_GITHUB_USERNAME}:${CG_GITHUB_PAT}" \
-H "Content-Type:application/octet-stream" \
-X POST "${UPLOAD_URL}?name=${APP}-0.0.${GITHUB_RUN_NUMBER}.tar.gz" \
--data-binary "@dist/${APP}-0.0.${GITHUB_RUN_NUMBER}.tar.gz" \
| jq -rc '.name + " - " + .url + " - " + .state'
announce "Upload completed successfully"
announce "Finishing up @ $(date)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment