Merge branch 'main' of https://github.com/khumnath/nepdate-cli #6
Workflow file for this run
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: Create Release and Debian Package | |
| on: | |
| push: | |
| tags: | |
| - 'v*' # Trigger the workflow on tags starting with 'v' | |
| jobs: | |
| build-and-release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Needed to create releases and push commits | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| # Fetch all history for all tags and branches, required for changelog generation | |
| fetch-depth: 0 | |
| - name: Install build dependencies | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y cmake build-essential debhelper devscripts git-buildpackage | |
| - name: Update Debian Changelog from Git History | |
| run: | | |
| # Set maintainer details for dch to use in the changelog | |
| export DEBEMAIL="[email protected]" | |
| export DEBFULLNAME="GitHub Actions" | |
| # Get the version from the tag (e.g., v1.1.0 -> 1.1.0) | |
| VERSION=${{ github.ref_name }} | |
| VERSION=${VERSION#v} | |
| # Explicitly create a new changelog entry with the correct version. | |
| # This is more reliable than auto-detection. | |
| dch --newversion "${VERSION}-1" --distribution "stable" "New release from tag ${{ github.ref_name }}." | |
| echo "Successfully updated changelog in workspace:" | |
| head -n 10 debian/changelog | |
| - name: Build Debian Package | |
| # This command will now use the correct version from the updated changelog | |
| run: dpkg-buildpackage -b -us -uc | |
| - name: Prepare Release Artifacts | |
| run: | | |
| # Create a directory to store all assets for the release | |
| mkdir -p release_assets | |
| # Copy the executable that was built during the deb package creation. | |
| # The standard location is in the debian/<package-name> directory. | |
| cp debian/nepdate-cli/usr/bin/nepdate-cli release_assets/nepdate-cli | |
| # Find, move, and rename the generated .deb package | |
| ORIGINAL_DEB=$(find .. -maxdepth 1 -name "nepdate-cli_*.deb" | head -n 1) | |
| if [ -z "$ORIGINAL_DEB" ]; then | |
| echo "::error:: Debian package not found in parent directory." | |
| exit 1 | |
| fi | |
| # Reliably get the architecture from the package metadata | |
| ARCH=$(dpkg-deb -f "$ORIGINAL_DEB" Architecture) | |
| NEW_DEB_NAME="nepdate-cli_${{ github.ref_name }}_${ARCH}.deb" | |
| mv "$ORIGINAL_DEB" "release_assets/$NEW_DEB_NAME" | |
| echo "Renamed Debian package to: $NEW_DEB_NAME" | |
| # Create a zip archive of the source code | |
| git archive -o release_assets/source-code-${{ github.ref_name }}.zip HEAD | |
| # Generate checksums for the renamed .deb file | |
| cd release_assets | |
| for file in *.deb; do | |
| md5sum "$file" > "${file}.md5" | |
| sha256sum "$file" > "${file}.sha256" | |
| done | |
| cd .. | |
| echo "Prepared assets:" | |
| ls -l release_assets | |
| - name: Generate Release Notes from Changelog | |
| id: generate_notes | |
| run: | | |
| # Use dpkg-parsechangelog to extract the "Changes" field from the latest entry | |
| # in the debian/changelog file. This text will be used as the body of the release. | |
| CHANGELOG_BODY=$(dpkg-parsechangelog --show-field Changes -l debian/changelog) | |
| # Format for GitHub Actions multiline output | |
| echo "CHANGELOG_BODY<<EOF" >> $GITHUB_OUTPUT | |
| echo "${CHANGELOG_BODY}" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Create GitHub Release and Upload Artifacts | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| # This will upload all files from the release_assets directory | |
| files: release_assets/* | |
| body: | | |
| ## Changelog | |
| ${{ steps.generate_notes.outputs.CHANGELOG_BODY }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Commit Updated Changelog to Repository | |
| run: | | |
| # Store the updated changelog temporarily | |
| mv debian/changelog /tmp/changelog | |
| # Configure git user for the commit | |
| git config --global user.name "GitHub Actions" | |
| git config --global user.email "[email protected]" | |
| # The checkout is in a detached HEAD state. We need to switch to the branch. | |
| # Replace 'main' with your repository's default branch if it's different. | |
| git checkout main | |
| # Restore the updated changelog | |
| mv /tmp/changelog debian/changelog | |
| # Add, commit, and push the updated changelog. | |
| # The [skip ci] in the message prevents this commit from re-triggering the workflow. | |
| git add debian/changelog | |
| # Check if there are changes to commit to avoid an error if the file is unchanged | |
| if git diff --staged --quiet; then | |
| echo "Changelog is already up-to-date." | |
| else | |
| git commit -m "Chore: Update debian/changelog for release ${{ github.ref_name }} [skip ci]" | |
| git push | |
| fi | |