Skip to content

Merge pull request #31 from furan917/release-please--branches--main--… #25

Merge pull request #31 from furan917/release-please--branches--main--…

Merge pull request #31 from furan917/release-please--branches--main--… #25

Workflow file for this run

name: Release
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
tag_name:
description: 'Tag name for the release e.g v1.0.0'
required: true
jobs:
build-and-release:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Set GOPATH
run: echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
- name: Download dependencies
run: go mod download
- name: Build assets
run: |
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o magecomm-linux-amd64
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o magecomm-linux-arm64
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o magecomm-windows-amd64.exe
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o magecomm-darwin-amd64
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -o magecomm-darwin-arm64
- name: Install jq
run: sudo apt-get install jq
- name: Check if release exists and create or update
id: check_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG_NAME=${{ github.event.inputs.tag_name || github.ref_name }}
RELEASE_NAME="Release $TAG_NAME"
RELEASE_API_URL="https://api.github.com/repos/${{ github.repository }}/releases"
RELEASE_ID=$(curl --silent --location --header "Authorization: Bearer $GITHUB_TOKEN" \
$RELEASE_API_URL | jq ".[] | select(.tag_name == \"$TAG_NAME\") | .id")
if [ -z "$RELEASE_ID" ]; then
echo "Creating new release..."
RELEASE_RESPONSE=$(curl --silent --location --request POST \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"tag_name\": \"$TAG_NAME\", \"name\": \"$RELEASE_NAME\", \"draft\": false, \"prerelease\": false}" \
$RELEASE_API_URL)
else
echo "Updating existing release with ID $RELEASE_ID..."
RELEASE_RESPONSE=$(curl --silent --location --request PATCH \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header "Content-Type: application/json" \
--data "{\"name\": \"$RELEASE_NAME\", \"draft\": false, \"prerelease\": false}" \
$RELEASE_API_URL/$RELEASE_ID)
fi
if [ "$(echo $RELEASE_RESPONSE | jq -r .upload_url)" != "null" ]; then
UPLOAD_URL=$(echo $RELEASE_RESPONSE | jq -r .upload_url)
echo "UPLOAD_URL=$UPLOAD_URL" >> $GITHUB_ENV
else
echo "Error: Upload URL is null."
exit 1
fi
- name: Upload amd64 linux assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ env.UPLOAD_URL }}
asset_path: ./magecomm-linux-amd64
asset_name: magecomm-linux-amd64
asset_content_type: application/octet-stream
- name: Upload arm64 linux assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ env.UPLOAD_URL }}
asset_path: ./magecomm-linux-arm64
asset_name: magecomm-linux-arm64
asset_content_type: application/octet-stream
- name: Upload amd64 windows assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ env.UPLOAD_URL }}
asset_path: ./magecomm-windows-amd64.exe
asset_name: magecomm-windows-amd64.exe
asset_content_type: application/octet-stream
- name: Upload amd64 darwin assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ env.UPLOAD_URL }}
asset_path: ./magecomm-darwin-amd64
asset_name: magecomm-darwin-amd64
asset_content_type: application/octet-stream
- name: Upload arm64 darwin assets
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ env.UPLOAD_URL }}
asset_path: ./magecomm-darwin-arm64
asset_name: magecomm-darwin-arm64
asset_content_type: application/octet-stream