1+ name : Release
2+
3+ on :
4+ push :
5+ tags :
6+ - " [0-9]+.[0-9]+.[0-9]+"
7+ - " [0-9]+.[0-9]+.[0-9]+a[0-9]+"
8+ - " [0-9]+.[0-9]+.[0-9]+b[0-9]+"
9+ - " [0-9]+.[0-9]+.[0-9]+rc[0-9]+"
10+
11+ env :
12+ PACKAGE_NAME : " fortscript"
13+ OWNER : " WesleyQDev"
14+ TAP_NAME : " homebrew-fortscript"
15+ PYTHON_VERSION : " 3.12"
16+
17+ jobs :
18+ details :
19+ runs-on : ubuntu-latest
20+ outputs :
21+ new_version : ${{ steps.release.outputs.new_version }}
22+ tag_name : ${{ steps.release.outputs.tag_name }}
23+ steps :
24+ - uses : actions/checkout@v4
25+
26+ - name : Extract Tag and Version
27+ id : release
28+ run : |
29+ if [ "${{ github.ref_type }}" = "tag" ]; then
30+ TAG_NAME=${GITHUB_REF#refs/tags/}
31+ # Assuming PEP 440 compliant tags (e.g. 1.0.0, 1.0.0rc1)
32+ # If using hyphens (1.0.0-rc1), ensure consistency with Python packaging
33+ echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"
34+ echo "new_version=$TAG_NAME" >> "$GITHUB_OUTPUT"
35+ echo "Tag: $TAG_NAME"
36+ else
37+ echo "No tag found"
38+ exit 1
39+ fi
40+
41+ check_pypi :
42+ needs : details
43+ runs-on : ubuntu-latest
44+ steps :
45+ - name : Fetch information from PyPI
46+ id : pypi_check
47+ run : |
48+ response=$(curl -s -o /dev/null -w "%{http_code}" https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json || echo "404")
49+ if [ "$response" == "200" ]; then
50+ remote_version=$(curl -s https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json | jq -r .info.version)
51+ echo "Latest PyPI version: $remote_version"
52+ echo "remote_version=$remote_version" >> "$GITHUB_OUTPUT"
53+ else
54+ echo "Package not found on PyPI (New Package)."
55+ echo "remote_version=0.0.0" >> "$GITHUB_OUTPUT"
56+ fi
57+
58+ - name : Compare versions
59+ run : |
60+ NEW_VERSION=${{ needs.details.outputs.new_version }}
61+ REMOTE_VERSION=${{ steps.pypi_check.outputs.remote_version }}
62+
63+ # Use sort -V for version comparison
64+ if [ "$(printf '%s\n' "$REMOTE_VERSION" "$NEW_VERSION" | sort -V | tail -n 1)" == "$REMOTE_VERSION" ] && [ "$REMOTE_VERSION" != "0.0.0" ] && [ "$REMOTE_VERSION" != "$NEW_VERSION" ]; then
65+ echo "Error: New version $NEW_VERSION is not greater than existing PyPI version $REMOTE_VERSION"
66+ exit 1
67+ elif [ "$REMOTE_VERSION" == "$NEW_VERSION" ]; then
68+ echo "Warning: Version $NEW_VERSION already exists on PyPI. Skipping build? (Currently failing to avoid overwrite error)"
69+ exit 1
70+ fi
71+ echo "Version check passed: $NEW_VERSION > $REMOTE_VERSION"
72+
73+ build_and_publish :
74+ needs : [details, check_pypi]
75+ runs-on : ubuntu-latest
76+ environment :
77+ name : release
78+ permissions :
79+ id-token : write # IMPORTANT: Required for trusted publishing
80+ contents : write
81+ steps :
82+ - uses : actions/checkout@v4
83+
84+ - name : Install uv
85+ uses : astral-sh/setup-uv@v5
86+ with :
87+ version : " latest"
88+
89+ - name : Set up Python
90+ uses : actions/setup-python@v5
91+ with :
92+ python-version : ${{ env.PYTHON_VERSION }}
93+
94+ - name : Sync version in pyproject.toml
95+ run : |
96+ # Updates the version in pyproject.toml to match the tag
97+ sed -i 's/^version = ".*"/version = "${{ needs.details.outputs.new_version }}"/' pyproject.toml
98+ echo "Updated pyproject.toml version to ${{ needs.details.outputs.new_version }}"
99+
100+ - name : Install dependencies
101+ run : uv sync --all-extras --dev
102+
103+ - name : Build dist
104+ run : uv build
105+
106+ - name : Publish to PyPI
107+ uses : pypa/gh-action-pypi-publish@release/v1
108+ with :
109+ # Using Trusted Publishing (OIDC)
110+ print_hash : true
111+
112+ - name : Create GitHub Release
113+ env :
114+ GH_TOKEN : ${{ github.token }}
115+ run : |
116+ gh release create ${{ needs.details.outputs.tag_name }} dist/* \
117+ --title "${{ needs.details.outputs.tag_name }}" \
118+ --generate-notes
119+
120+ update_homebrew :
121+ needs : [build_and_publish, details]
122+ runs-on : ubuntu-latest
123+ if : success()
124+ steps :
125+ - name : Dispatch Homebrew Update
126+ uses : peter-evans/repository-dispatch@v3
127+ with :
128+ token : ${{ secrets.BREW_TAP_TOKEN }}
129+ repository : ${{ env.OWNER }}/${{ env.TAP_NAME }}
130+ event-type : " update-formula"
131+ client-payload : |-
132+ {
133+ "formula_version": "${{ needs.details.outputs.new_version }}",
134+ "formula_name": "${{ env.PACKAGE_NAME }}",
135+ "formula_url": "https://github.com/${{ env.OWNER }}/${{ env.PACKAGE_NAME }}/releases/download/${{ needs.details.outputs.tag_name }}/${{ env.PACKAGE_NAME }}-${{ needs.details.outputs.new_version }}.tar.gz"
136+ }
0 commit comments