Deploy to GitHub Releases #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to GitHub Releases | |
on: | |
workflow_dispatch: | |
push: | |
tags: | |
- 'v*' | |
permissions: | |
contents: write | |
jobs: | |
build-and-release: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Run Build | |
run: make build # Assumes you have a Makefile with a 'build' target | |
- name: Create and Upload Release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
GITHUB_REF_NAME: ${{ github.ref_name }} | |
run: | | |
# Create a release | |
RESPONSE=$(curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -d '{"tag_name":"'"$GITHUB_REF_NAME"'", "name":"Release '"$GITHUB_REF_NAME"'", "draft":false, "prerelease":false}' "https://api.github.com/repos/$GITHUB_REPOSITORY/releases") | |
# Extract the upload URL from the response | |
UPLOAD_URL=$(echo $RESPONSE | jq -r .upload_url | sed 's/{?name,label}//') | |
# Loop through and upload each asset in the dist/ directory | |
for filename in build/*; do | |
echo "Uploading ${filename}" | |
asset_path=${filename} | |
asset_name=$(basename ${filename}) | |
# Upload asset | |
HTTP_RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/octet-stream" --data-binary @"${asset_path}" "${UPLOAD_URL}?name=${asset_name}") | |
if [ "$HTTP_RESPONSE" -eq "201" ]; then | |
echo "Successfully uploaded ${asset_name}" | |
rm response.txt # Clean up the response file | |
else | |
echo "Failed to upload ${asset_name} with HTTP response $HTTP_RESPONSE" | |
echo "Response: $(cat response.txt)" | |
rm response.txt # Clean up the response file | |
exit 1 | |
fi | |
done |