change to variables on github flow #49
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 Go Project | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
permissions: write-all | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: 1.23 | |
- name: Retrieve current version | |
id: get_version | |
run: | | |
VERSION=$(echo "${{ vars.CURRENT_VERSION }}" | cut -d 'v' -f 2) | |
MAJOR=$(echo $VERSION | cut -d '.' -f 1) | |
MINOR=$(echo $VERSION | cut -d '.' -f 2) | |
PATCH=$(echo $VERSION | cut -d '.' -f 3) | |
NEW_PATCH=$((PATCH + 1)) | |
NEW_VERSION="v$MAJOR.$MINOR.$NEW_PATCH" | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | |
- name: Build for Linux, macOS, and Windows | |
run: | | |
GIT_WORKSPACE="${{ github.workspace }}" | |
OUTPUT_DIR="${{ github.workspace }}/bin" | |
BINARY_NAME="flag" | |
mkdir -p $OUTPUT_DIR/linux $OUTPUT_DIR/macos $OUTPUT_DIR/windows | |
cd $GIT_WORKSPACE/src | |
# Build for Linux | |
GOOS=linux GOARCH=amd64 go build -ldflags="-X main.VERSION=${{ env.NEW_VERSION }}" -trimpath -o $OUTPUT_DIR/linux/$BINARY_NAME | |
# Build for macOS | |
GOOS=darwin GOARCH=amd64 go build -ldflags="-X main.VERSION=${{ env.NEW_VERSION }}" -trimpath -o $OUTPUT_DIR/macos/$BINARY_NAME | |
# Build for Windows | |
GOOS=windows GOARCH=amd64 go build -ldflags="-X main.VERSION=${{ env.NEW_VERSION }}" -trimpath -o $OUTPUT_DIR/windows/$BINARY_NAME.exe | |
# Zip only the linux, macos, and windows folders without the full path | |
cd $OUTPUT_DIR | |
zip -j flag-windows.zip windows/$BINARY_NAME.exe | |
zip -j flag-linux.zip linux/$BINARY_NAME | |
zip -j flag-macos.zip macos/$BINARY_NAME | |
- name: List contents of bin directory | |
run: | | |
pwd | |
ls -la ${{ github.workspace }}/bin | |
- name: Upload build artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: build | |
path: ${{ github.workspace }}/bin | |
- name: Set Release Version | |
id: set_version | |
run: echo "RELEASE_VERSION=${{ env.NEW_VERSION }}" >> $GITHUB_ENV | |
- name: Create Release | |
id: create_release | |
uses: actions/create-release@v1 | |
with: | |
tag_name: ${{ env.RELEASE_VERSION }} | |
release_name: Release ${{ env.RELEASE_VERSION }} | |
draft: false | |
prerelease: false | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload Windows Release Asset | |
uses: actions/upload-release-asset@v1 | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ github.workspace }}/bin/flag-windows.zip | |
asset_name: flag-windows.zip | |
asset_content_type: application/zip | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload Linux Release Asset | |
uses: actions/upload-release-asset@v1 | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ github.workspace }}/bin/flag-linux.zip | |
asset_name: flag-linux.zip | |
asset_content_type: application/zip | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload macOS Release Asset | |
uses: actions/upload-release-asset@v1 | |
with: | |
upload_url: ${{ steps.create_release.outputs.upload_url }} | |
asset_path: ${{ github.workspace }}/bin/flag-macos.zip | |
asset_name: flag-macos.zip | |
asset_content_type: application/zip | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Update Version Variable | |
run: | | |
curl -X PATCH \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/actions/variables/CURRENT_VERSION \ | |
-d '{"name":"CURRENT_VERSION","value":"v$MAJOR.$MINOR.$NEW_PATCH"}' |