1+ name : Deploy to GitHub
2+
3+ on :
4+ push :
5+ branches :
6+ - master
7+ tags :
8+ - " *"
9+
10+ jobs :
11+ tag :
12+ name : Prepare Release
13+ runs-on : ubuntu-latest
14+ if : github.event_name == 'push' # Only run on push events
15+
16+ steps :
17+ - uses : actions/checkout@v3
18+ with :
19+ fetch-depth : 0 # Important: Fetch all history for tags
20+
21+ - name : Get latest commit SHA
22+ id : sha
23+ if : github.ref == 'refs/heads/master' # Only when pushing to master
24+ run : echo "::set-output name=sha::$(git rev-parse HEAD)"
25+
26+ - name : Create Tag (if needed)
27+ id : create_tag
28+ if : github.ref == 'refs/heads/master' && !startsWith(github.event.head_commit.message, 'Merge pull request') # Only on direct pushes to master, not merge commits
29+ run : |
30+ TAG_NAME="v$(date +%Y%m%d%H%M%S)-${{ steps.sha.outputs.sha }}"
31+ git config --global user.name 'GitHub Actions'
32+ git config --global user.email 'actions@github.com'
33+ git tag -a "$TAG_NAME" -m "Release from master ${{ steps.sha.outputs.sha }}"
34+ git push origin --tags
35+ echo "::set-output name=tag::$TAG_NAME"
36+
37+ - name : Extract Release Notes
38+ id : release_notes
39+ if : github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/'))
40+ run : |
41+ # Define variables
42+ changelog_section_start="== Changelog =="
43+ current_tag="${{ steps.create_tag.outputs.tag || github.ref_name }}" # Use created tag or existing tag
44+ readme_file=$(find . -type f -iname "readme.*" | head -n 1)
45+
46+ if [ -z "$readme_file" ]; then
47+ echo "::error::Readme file not found."
48+ exit 1
49+ fi
50+ echo "Readme file: $readme_file"
51+
52+ # Extract version from tag
53+ version=${current_tag#refs/tags/}
54+
55+ # Initialize variables
56+ in_changelog=0
57+ capturing_version=0
58+ release_notes=""
59+
60+ while IFS= read -r line; do
61+ # Start capturing after finding the changelog section
62+ if [[ "$line" == "$changelog_section_start" ]]; then
63+ in_changelog=1
64+ continue
65+ fi
66+
67+ # Skip lines before the changelog section
68+ if [[ $in_changelog -eq 0 ]]; then
69+ continue
70+ fi
71+
72+ # Start capturing if the line starts with a version number (semver format)
73+ if [[ "$line" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
74+ capturing_version=1
75+ # Check if the captured version matches the current tag
76+ if [[ "$line" == "$version" ]]; then
77+ release_notes+="$line\n"
78+ fi
79+ continue
80+ fi
81+
82+ # Stop capturing when a new version section is detected
83+ if [[ $capturing_version -eq 1 && "$line" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
84+ break
85+ fi
86+
87+ # Add the line to the release notes if capturing the current version
88+ if [[ $capturing_version -eq 1 ]]; then
89+ release_notes+="$line\n"
90+ fi
91+ done < "$readme_file"
92+
93+ # Check if release notes were extracted
94+ if [[ -z "$release_notes" ]]; then
95+ echo "::error::Failed to extract release notes for version $version."
96+ exit 1
97+ fi
98+
99+ # Debug: Print extracted release notes
100+ echo "Extracted release notes for version $version:"
101+ printf "%b" "$release_notes"
102+
103+ # Set output for release notes
104+ echo "::set-output name=notes::$(printf "%b" "$release_notes")"
105+
106+ - name : Create GitHub Release
107+ uses : softprops/action-gh-release@v2
108+ if : github.event_name == 'push' && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/'))
109+ with :
110+ tag_name : ${{ steps.create_tag.outputs.tag || github.ref_name }}
111+ body : ${{ steps.release_notes.outputs.notes }}
112+ files : ${{github.workspace}}/${{ github.event.repository.name }}.zip
113+ env :
114+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
0 commit comments