fix #129
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: build and release | |
on: | |
push: | |
tags: | |
- "v*" | |
branches: | |
- main # Add main branch to trigger on commits | |
jobs: | |
check_commit_message: | |
runs-on: ubuntu-latest | |
if: "!startsWith(github.ref, 'refs/tags/')" | |
outputs: | |
should_build: ${{ steps.check_message.outputs.should_build }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 # Need to fetch the commit before the last one | |
- name: Check if commit message contains [ci] | |
id: check_message | |
run: | | |
commit_message=$(git log -1 --pretty=format:"%s") | |
echo "Commit message: $commit_message" | |
if [[ "$commit_message" == *"[ci]"* ]]; then | |
echo "should_build=true" >> $GITHUB_OUTPUT | |
else | |
echo "should_build=false" >> $GITHUB_OUTPUT | |
fi | |
build_tag: | |
name: Build and Create Release | |
runs-on: ubuntu-latest | |
if: startsWith(github.ref, 'refs/tags/') | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: "1.23.1" | |
- name: Build Linux binary | |
run: | | |
GOOS=linux GOARCH=amd64 go build -o pubsub | |
chmod +x pubsub | |
- name: Upload Release Binary | |
uses: softprops/action-gh-release@v1 | |
with: | |
files: pubsub | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
build_ci: | |
name: Build and Update Latest Release | |
runs-on: ubuntu-latest | |
needs: check_commit_message | |
if: needs.check_commit_message.outputs.should_build == 'true' | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: "1.23.1" | |
- name: Build Linux binary | |
run: | | |
GOOS=linux GOARCH=amd64 go build -o pubsub-latest | |
chmod +x pubsub-latest | |
- name: Check if latest release exists | |
id: check_release | |
run: | | |
latest_release=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/tags/latest" | \ | |
jq -r '.id // "null"') | |
if [[ "$latest_release" == "null" ]]; then | |
echo "exists=false" >> $GITHUB_OUTPUT | |
echo "No latest release found. Will create a new one." | |
else | |
echo "exists=true" >> $GITHUB_OUTPUT | |
echo "id=$latest_release" >> $GITHUB_OUTPUT | |
echo "Found latest release with ID: $latest_release" | |
# Check if asset exists and get its ID | |
asset_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/$latest_release/assets" | \ | |
jq '.[] | select(.name=="pubsub") | .id // "null"') | |
if [[ "$asset_id" != "null" ]]; then | |
echo "asset_exists=true" >> $GITHUB_OUTPUT | |
echo "asset_id=$asset_id" >> $GITHUB_OUTPUT | |
echo "Found existing asset with ID: $asset_id" | |
else | |
echo "asset_exists=false" >> $GITHUB_OUTPUT | |
echo "No existing asset found." | |
fi | |
# Get upload URL | |
upload_url=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/$latest_release" | \ | |
jq -r '.upload_url') | |
# Clean up the upload URL (remove {?name,label} part) | |
upload_url=${upload_url%\{*} | |
echo "upload_url=$upload_url" >> $GITHUB_OUTPUT | |
echo "Upload URL: $upload_url" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create latest release if it doesn't exist | |
if: steps.check_release.outputs.exists == 'false' | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
tag_name: latest | |
release_name: Latest Build | |
body: | | |
This is an automatically updated release containing the latest build. | |
Last updated on ${{ github.event.head_commit.timestamp }} | |
Commit: ${{ github.event.head_commit.message }} | |
draft: false | |
prerelease: true | |
- name: Update latest release if it exists | |
if: steps.check_release.outputs.exists == 'true' | |
run: | | |
RELEASE_ID="${{ steps.check_release.outputs.id }}" | |
curl -X PATCH \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/$RELEASE_ID" \ | |
-d "{ | |
\"body\": \"This is an automatically updated release containing the latest build.\\nLast updated: $(date -u +'%Y-%m-%d %H:%M:%S UTC')\\nCommit: ${{ github.event.head_commit.message }}\" | |
}" | |
echo "Updated release description" | |
- name: Delete existing asset if exists | |
if: steps.check_release.outputs.exists == 'true' && steps.check_release.outputs.asset_exists == 'true' | |
run: | | |
curl -X DELETE \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/assets/${{ steps.check_release.outputs.asset_id }}" | |
echo "Deleted existing asset" | |
# Sleep briefly to ensure the deletion is processed | |
sleep 2 | |
- name: Upload asset for new release | |
if: steps.check_release.outputs.exists == 'false' | |
uses: actions/upload-release-asset@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ./pubsub-latest | |
asset_name: pubsub | |
asset_content_type: application/octet-stream | |
- name: Upload asset for existing release | |
if: steps.check_release.outputs.exists == 'true' | |
run: | | |
curl -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Content-Type: application/octet-stream" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
--data-binary @pubsub-latest \ | |
"${{ steps.check_release.outputs.upload_url }}?name=pubsub" | |
echo "Uploaded new asset" |