Skip to content

Merge pull request #8 from jsilverdev/fix/tests #21

Merge pull request #8 from jsilverdev/fix/tests

Merge pull request #8 from jsilverdev/fix/tests #21

name: Build and Release
on:
workflow_dispatch:
inputs:
version_number:
description: "Version number (example: 1.0.0), if is empty then use the last version of CHANGELOG.md"
required: false
ref:
type: string
required: false
push:
branches: ["main"]
paths:
- "**.dart"
- "pubspec.lock"
jobs:
last_version:
runs-on: ubuntu-latest
outputs:
version_number: ${{ steps.get_latest_version.outputs.version_number }}
current_sha: ${{ steps.current_sha.outputs.sha }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
fetch-depth: 0
- name: Get latest version number
id: get_latest_version
run: |
if [[ -n "${{ github.event.inputs.version_number }}" ]]; then
VERSION_NUMBER="${{ github.event.inputs.version_number }}"
else
VERSION_NUMBER=$(grep -m 1 -oP '## \[(?!Unreleased)[^\]]+\]' CHANGELOG.md | sed 's/## \[\(.*\)\]/\1/')
fi
echo "version_number=${VERSION_NUMBER}" >> $GITHUB_OUTPUT
- name: Check if Tag Exists
run: |
tag_name="v${{ steps.get_latest_version.outputs.version_number }}"
if git rev-parse --verify "$tag_name" >/dev/null 2>&1; then
echo "Tag $tag_name already exists."
exit 1
else
echo "Tag $tag_name does not exist. Proceeding with the workflow."
fi
- name: Check if entry exists on CHANGELOG.md
if: github.event.inputs.version_number != ''
run: |
changelog_entry=$(grep -E "^##\s+\[${{ github.event.inputs.version_number }}\]" CHANGELOG.md)
if [ -z "$changelog_entry" ]; then
echo "No changelog entry found for version ${{ github.event.inputs.version_number }} in CHANGELOG.md"
exit 1
fi
- name: Add version link on CHANGELOG.md
if: github.event.inputs.version_number == '' && github.event.inputs.ref == ''
run: |
new_version="${{ steps.get_latest_version.outputs.version_number }}"
unreleased_link=$(grep -m 1 -oP '\[unreleased\]: \Khttps:\/\/github\.com\/[^.]*\/[^.]*\/compare\/' "CHANGELOG.md")
sha_latest=$(git rev-list -n 1 latest)
new_version_link="[$new_version]: ${unreleased_link}$sha_latest...v$new_version"
sed -i "/\[unreleased\]: .*/a $new_version_link" "CHANGELOG.md"
- name: Update version number
if: github.event.inputs.version_number == '' && github.event.inputs.ref == ''
uses: mikefarah/yq@master
with:
cmd: yq -i '.version="${{ steps.get_latest_version.outputs.version_number }}"' pubspec.yaml
- name: Commit new version and update latest tag
if: github.event.inputs.version_number == '' && github.event.inputs.ref == ''
run: |
git config user.name github-actions
git config user.email [email protected]
git add .
git commit -m "${{ steps.get_latest_version.outputs.version_number }}" -m $'\nskip-checks: true' --cleanup=verbatim
git tag -d latest
git push origin :refs/tags/latest
git tag latest
git push origin main --tags
- name: Get current SHA
id: current_sha
run: |
if [[ -n "${{ github.event.inputs.ref }}" ]]; then
CURRENT_SHA="${{ github.event.inputs.ref }}"
else
CURRENT_SHA=$(git rev-parse HEAD)
fi
echo "sha=${CURRENT_SHA}" >> $GITHUB_OUTPUT
build:
needs: [last_version]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
binary_extension: "-linux"
pub_cache: "~/.pub-cache"
- os: windows-latest
binary_extension: "-win.exe"
pub_cache: "~\\.pub-cache"
- os: macos-latest
binary_extension: "-macos"
pub_cache: "~/.pub-cache"
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.last_version.outputs.current_sha }}
- name: Restore Cache dependencies
id: restore-cache-deps
uses: actions/cache/restore@v3
with:
key: ${{ runner.os }}-pub-cache-${{ hashFiles('**/pubspec.lock') }}
path: ${{ matrix.pub_cache }}
- name: Setup Dart
uses: dart-lang/setup-dart@v1
- name: Install dependencies
run: dart pub get
- name: Save Cache dependencies
uses: actions/cache/save@v3
if: steps.restore-cache-deps.outputs.cache-hit != 'true'
with:
key: ${{ steps.restore-cache-deps.outputs.cache-primary-key }}
path: ${{ matrix.pub_cache }}
- name: Compile executable
run: |
mkdir binaries
dart compile exe bin/main.dart -o binaries/run${{ matrix.binary_extension }}
- name: Upload compiled files
if: ${{ job.status == 'success' }}
uses: actions/upload-artifact@v3
with:
name: compiled-files
path: binaries/run${{ matrix.binary_extension }}
retention-days: 1
release:
needs: [build, last_version]
runs-on: ubuntu-latest
if: ${{ needs.build.result == 'success' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.last_version.outputs.current_sha }}
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Download compiled files
uses: actions/download-artifact@v3
with:
name: compiled-files
path: binaries/
- name: Make executable run binary files
run: find binaries/ -type f -name 'run*' -exec chmod +x {} \;
- name: Move executables to root path
run: mv binaries/* .
- name: Remove ignore files from .gitignore
run: sed -i '/run[^[:space:]]*/d' .gitignore
- name: Create and push tag
run: |
git config user.name github-actions
git config user.email [email protected]
git checkout -b ${{ needs.last_version.outputs.version_number }}
git add .
git status
git commit -m "Add compiled files"
git status
git tag v${{ needs.last_version.outputs.version_number }}
git push origin v${{ needs.last_version.outputs.version_number }}
- name: Switch to latest main branch (to get latest CHANGELOG.md)
if: github.event.inputs.ref != ''
run: |
git config user.name github-actions
git config user.email [email protected]
git fetch origin main
git switch main
- name: Get Changelog Entry
id: changelog_reader
uses: mindsers/changelog-reader-action@v2
with:
validation_level: warn
validation_depth: 5
version: ${{ needs.last_version.outputs.version_number }}
path: ./CHANGELOG.md
- name: Return to versioned branch
if: github.event.inputs.ref != ''
run: git checkout ${{ needs.last_version.outputs.version_number }}
- name: Rename files
run: |
for file in ./run*; do
if [ -f "$file" ]; then
new_name="config_props_extractor_v${{needs.last_version.outputs.version_number}}-$(echo "$file" | cut -d'-' -f2)"
mv "$file" "$new_name"
fi
done
- name: Create and publish release
uses: ncipollo/release-action@v1
with:
tag: v${{ needs.last_version.outputs.version_number }}
artifacts: "config_props_extractor_v*"
body: ${{ steps.changelog_reader.outputs.changes }}
prerelease: ${{ steps.changelog_reader.outputs.status == 'prereleased' }}
draft: ${{ steps.changelog_reader.outputs.status == 'unreleased' }}