Automatic Release #132
This file contains hidden or 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: Automatic Release | |
| on: | |
| schedule: | |
| # Runs every day at 20:00 UTC | |
| - cron: '0 20 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| build_and_release: | |
| # Prevent running on forks | |
| if: github.repository_owner == 'frankiejun' # IMPORTANT: Change this to your GitHub username or organization name | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Required to create a release and upload assets | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetches all history for all tags | |
| - name: Get latest tag | |
| id: get_tag | |
| run: | | |
| # Get the most recent tag name | |
| LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`) | |
| if [ -z "$LATEST_TAG" ]; then | |
| echo "No tags found in the repository. Exiting." | |
| exit 1 | |
| fi | |
| echo "Latest tag is: $LATEST_TAG" | |
| echo "tag=$LATEST_TAG" >> "$GITHUB_OUTPUT" | |
| - name: Check if release for tag already exists | |
| id: check_release | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # The 'gh release view' command will exit with a non-zero status if the release does not exist. | |
| if gh release view ${{ steps.get_tag.outputs.tag }} &>/dev/null; then | |
| echo "Release for tag ${{ steps.get_tag.outputs.tag }} already exists. No action needed." | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "Release for tag ${{ steps.get_tag.outputs.tag }} does not exist. A new one will be created." | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Create GitHub Release | |
| if: steps.check_release.outputs.exists == 'false' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| gh release create ${{ steps.get_tag.outputs.tag }} \ | |
| --title "Release ${{ steps.get_tag.outputs.tag }}" \ | |
| --notes "Automatic release generated for new version ${{ steps.get_tag.outputs.tag }}." | |
| - name: Package project files and directories | |
| if: steps.check_release.outputs.exists == 'false' | |
| run: | | |
| mkdir release-assets | |
| # Loop through all non-hidden items in the root directory | |
| for item in *; do | |
| # Skip the release assets directory and the .github workflow directory | |
| if [[ "$item" == "release-assets" || "$item" == ".github" ]]; then | |
| continue | |
| fi | |
| if [ -d "$item" ]; then | |
| # It's a directory, create a tar.gz archive | |
| echo "Packaging directory: $item" | |
| tar -czf "release-assets/$item.tar.gz" -- "$item" | |
| elif [ -f "$item" ]; then | |
| # It's a file, create a .gz archive | |
| echo "Packaging file: $item" | |
| gzip -c -- "$item" > "release-assets/$item.gz" | |
| fi | |
| done | |
| - name: Upload assets to Release | |
| if: steps.check_release.outputs.exists == 'false' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # Upload all packaged files from the release-assets directory | |
| for asset in release-assets/*; do | |
| gh release upload ${{ steps.get_tag.outputs.tag }} "$asset" | |
| done |