|
| 1 | +name: Build, Sign, and Publish SRPM and RPM on Release |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] # Trigger when a new release is published |
| 6 | + |
| 7 | +jobs: |
| 8 | + build-rpm: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + env: |
| 12 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 13 | + ACTIONS_RUNNER_DEBUG: false |
| 14 | + |
| 15 | + steps: |
| 16 | + # Step 1: Checkout the code |
| 17 | + - name: Checkout Code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 # Fetch all history for commit count |
| 21 | + |
| 22 | + # Step 2: Set up the build environment |
| 23 | + - name: Set Up Build Environment |
| 24 | + run: | |
| 25 | + set -euo pipefail |
| 26 | +
|
| 27 | + # Install required dependencies |
| 28 | + sudo apt update |
| 29 | + sudo DEBIAN_FRONTEND=noninteractive apt install -y rpm gnupg gh |
| 30 | +
|
| 31 | + # Create rpmbuild directory structure |
| 32 | + echo "Setting up rpmbuild directory structure" |
| 33 | + mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} |
| 34 | +
|
| 35 | + # Move spec files into the build structure |
| 36 | + cp -R rpm/SPECS/*.spec ~/rpmbuild/SPECS/ |
| 37 | +
|
| 38 | + # Create the source archive |
| 39 | + cd src |
| 40 | + tar --exclude='./.git' --exclude='./rpm/SPECS' -czf ~/rpmbuild/SOURCES/iscsi-ha.tar.gz . |
| 41 | + cd .. |
| 42 | +
|
| 43 | + # Step 3: Extract version and release information |
| 44 | + - name: Extract Version and Release |
| 45 | + id: versioning |
| 46 | + run: | |
| 47 | + set -euo pipefail |
| 48 | +
|
| 49 | + # Get the release tag name from GitHub event data |
| 50 | + TAG_NAME="${{ github.event.release.tag_name }}" |
| 51 | +
|
| 52 | + # Extract the version (numeric part before any dash) |
| 53 | + VERSION=$(echo "$TAG_NAME" | sed -E 's/^v([0-9]+\.[0-9]+\.[0-9]+)(-.*)?$/\1/') |
| 54 | +
|
| 55 | + # Initialize prerelease variable |
| 56 | + PRERELEASE="" |
| 57 | +
|
| 58 | + # Check if the tag contains prerelease markers or if the release is marked as a prerelease |
| 59 | + if [[ "$TAG_NAME" =~ -(rc|RC|beta|BETA) ]]; then |
| 60 | + PRERELEASE=$(echo "$TAG_NAME" | sed -E 's/^.*-(rc|RC|beta|BETA).*$/\1/') |
| 61 | + elif [[ "${{ github.event.release.prerelease }}" == "true" ]]; then |
| 62 | + PRERELEASE="rc" |
| 63 | + fi |
| 64 | +
|
| 65 | + # Get the number of commits to use as the release number |
| 66 | + RELEASE_NUMBER=$(git rev-list --count HEAD) |
| 67 | +
|
| 68 | + # Combine the prerelease, and release number into the final RELEASE |
| 69 | + if [ -n "$PRERELEASE" ]; then |
| 70 | + RELEASE="${PRERELEASE}${RELEASE_NUMBER}" |
| 71 | + else |
| 72 | + RELEASE="${RELEASE_NUMBER}" |
| 73 | + fi |
| 74 | +
|
| 75 | + echo "VERSION=${VERSION}" >> $GITHUB_ENV |
| 76 | + echo "RELEASE=${RELEASE}" >> $GITHUB_ENV |
| 77 | +
|
| 78 | + # Step 4: Build RPM package |
| 79 | + - name: Build SRPM and RPM Package |
| 80 | + run: | |
| 81 | + set -euo pipefail |
| 82 | +
|
| 83 | + # Replace placeholders in spec files with actual version and release |
| 84 | + sed -i "s/__VERSION__/${VERSION}/g" ~/rpmbuild/SPECS/*.spec |
| 85 | + sed -i "s/__RELEASE__/${RELEASE}/g" ~/rpmbuild/SPECS/*.spec |
| 86 | +
|
| 87 | + # Build the RPM package |
| 88 | + rpmbuild -ba ~/rpmbuild/SPECS/*.spec |
| 89 | +
|
| 90 | + # Step 5: Import GPG Key |
| 91 | + - name: Import GPG Key |
| 92 | + run: | |
| 93 | + set -euo pipefail |
| 94 | +
|
| 95 | + # Import the private key from secrets |
| 96 | + echo "Importing GPG key" |
| 97 | + echo "${{ secrets.GPG_PRIVATE_KEY }}" | gpg --batch --import |
| 98 | +
|
| 99 | + # Ensure the trust level is set to ultimate for the imported key |
| 100 | + echo "Setting trust level for GPG key" |
| 101 | + echo -e "5\ny\n" | gpg --batch --command-fd 0 --edit-key "${{ secrets.GPG_KEY_ID }}" trust |
| 102 | +
|
| 103 | + # Step 6: Sign RPMs |
| 104 | + - name: Sign RPMs |
| 105 | + run: | |
| 106 | + set -euo pipefail |
| 107 | +
|
| 108 | + # Configure GPG_TTY to avoid warnings |
| 109 | + export GPG_TTY=$(tty) || true |
| 110 | +
|
| 111 | + # Configure RPM to use the GPG key |
| 112 | + echo "%_signature gpg" >> ~/.rpmmacros |
| 113 | + echo "%_gpg_name ${{ secrets.GPG_KEY_ID }}" >> ~/.rpmmacros |
| 114 | +
|
| 115 | + # Sign all RPMs |
| 116 | + find ~/rpmbuild/RPMS/ -name "*.rpm" -exec rpmsign --addsign {} \; |
| 117 | + find ~/rpmbuild/SRPMS/ -name "*.rpm" -exec rpmsign --addsign {} \; |
| 118 | +
|
| 119 | + # Step 7: Upload RPM and SRPM to the GitHub Release |
| 120 | + - name: Upload SRPM and RPM to GitHub Release |
| 121 | + run: | |
| 122 | + set -euo pipefail |
| 123 | +
|
| 124 | + # Upload binary RPMs |
| 125 | + gh release upload "${{ github.event.release.tag_name }}" ~/rpmbuild/RPMS/**/*.rpm --clobber |
| 126 | +
|
| 127 | + # Upload source RPM |
| 128 | + gh release upload "${{ github.event.release.tag_name }}" ~/rpmbuild/SRPMS/*.src.rpm --clobber |
0 commit comments