.github/workflows/update-aur.yml

231 lines · 9696 bytes

  1name: Update AUR Package
  2
  3on:
  4  release:
  5    types: [published]
  6  workflow_dispatch:
  7
  8permissions:
  9  contents: read
 10
 11jobs:
 12  update-aur:
 13    runs-on: ubuntu-24.04
 14    if: github.repository == 'Wraient/curd'
 15    
 16    steps:
 17      - name: Install dependencies
 18        run: |
 19          sudo apt-get update
 20          sudo apt-get install -y curl git jq openssh-client
 21
 22      - name: Download release asset and compute SHA256
 23        id: release
 24        env:
 25          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 26        run: |
 27          set -euo pipefail
 28
 29          if [ "${{ github.event_name }}" = "release" ]; then
 30            TAG_NAME="${{ github.event.release.tag_name }}"
 31            RELEASE_API_URL="https://api.github.com/repos/${{ github.repository }}/releases/tags/${TAG_NAME}"
 32          else
 33            RELEASE_API_URL="https://api.github.com/repos/${{ github.repository }}/releases/latest"
 34          fi
 35
 36          RELEASE_JSON="$(mktemp)"
 37          curl -fsSL \
 38            -H "Authorization: Bearer ${GH_TOKEN}" \
 39            -H "Accept: application/vnd.github+json" \
 40            "$RELEASE_API_URL" \
 41            -o "$RELEASE_JSON"
 42
 43          TAG_NAME="$(jq -r '.tag_name // empty' "$RELEASE_JSON")"
 44          ASSET_URL="$(jq -r '.assets[] | select(.name=="curd-linux-x86_64") | .browser_download_url' "$RELEASE_JSON" | head -n1)"
 45
 46          if [ -z "$TAG_NAME" ] || [ "$TAG_NAME" = "null" ]; then
 47            echo "Could not resolve release tag from $RELEASE_API_URL"
 48            rm -f "$RELEASE_JSON"
 49            exit 1
 50          fi
 51
 52          if [ -z "$ASSET_URL" ] || [ "$ASSET_URL" = "null" ]; then
 53            echo "Release ${TAG_NAME} does not contain asset: curd-linux-x86_64"
 54            rm -f "$RELEASE_JSON"
 55            exit 1
 56          fi
 57
 58          VERSION="${TAG_NAME#v}"
 59          TMP_ASSET="$(mktemp)"
 60          curl -fL --retry 3 --retry-all-errors --connect-timeout 20 "$ASSET_URL" -o "$TMP_ASSET"
 61          SHA256="$(sha256sum "$TMP_ASSET" | awk '{print $1}')"
 62
 63          rm -f "$RELEASE_JSON"
 64          rm -f "$TMP_ASSET"
 65
 66          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
 67          echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"
 68          echo "sha256=$SHA256" >> "$GITHUB_OUTPUT"
 69
 70      - name: Configure SSH for AUR
 71        env:
 72          AUR_SSH_PRIVATE_KEY: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
 73        run: |
 74          set -euo pipefail
 75
 76          if [ -z "${AUR_SSH_PRIVATE_KEY:-}" ]; then
 77            echo "Missing required secret: AUR_SSH_PRIVATE_KEY"
 78            exit 1
 79          fi
 80
 81          install -m 700 -d ~/.ssh
 82          umask 077
 83          KEY_FILE=~/.ssh/curd_aur_deploy
 84          RAW_KEY="$(printf '%s' "$AUR_SSH_PRIVATE_KEY" | tr -d '\r')"
 85
 86          if printf '%s' "$RAW_KEY" | grep -q 'BEGIN [A-Z0-9 ]*PRIVATE KEY'; then
 87            if printf '%s' "$RAW_KEY" | grep -q '\\n'; then
 88              printf '%b' "$RAW_KEY" > "$KEY_FILE"
 89            else
 90              printf '%s\n' "$RAW_KEY" > "$KEY_FILE"
 91            fi
 92          else
 93            if ! printf '%s' "$RAW_KEY" | base64 -d > "$KEY_FILE" 2>/dev/null; then
 94              echo "AUR_SSH_PRIVATE_KEY must be a private key (raw, escaped with \\n, or base64-encoded)"
 95              exit 1
 96            fi
 97          fi
 98
 99          tr -d '\r' < "$KEY_FILE" > "${KEY_FILE}.tmp"
100          mv "${KEY_FILE}.tmp" "$KEY_FILE"
101
102          if [ ! -s "$KEY_FILE" ]; then
103            echo "SSH key file is empty after writing secret"
104            exit 1
105          fi
106
107          KEY_HEADER="$(head -n1 "$KEY_FILE" || true)"
108          case "$KEY_HEADER" in
109            "-----BEGIN OPENSSH PRIVATE KEY-----"|"-----BEGIN RSA PRIVATE KEY-----"|"-----BEGIN EC PRIVATE KEY-----"|"-----BEGIN DSA PRIVATE KEY-----"|"-----BEGIN PRIVATE KEY-----")
110              ;;
111            *)
112              echo "AUR_SSH_PRIVATE_KEY has an invalid private key header: $KEY_HEADER"
113              exit 1
114              ;;
115          esac
116
117          if ! ssh-keygen -y -f "$KEY_FILE" > ~/.ssh/curd_aur_deploy.pub 2>/dev/null; then
118            echo "AUR_SSH_PRIVATE_KEY is not a valid, unencrypted SSH private key"
119            exit 1
120          fi
121
122          chmod 600 "$KEY_FILE"
123          chmod 644 ~/.ssh/curd_aur_deploy.pub
124          ssh-keyscan -H aur.archlinux.org >> ~/.ssh/known_hosts
125          chmod 644 ~/.ssh/known_hosts
126          echo "Validated deploy key format. Add the matching public key to the AUR account SSH keys."
127
128      - name: Clone AUR repository
129        env:
130          GIT_SSH_COMMAND: ssh -i ~/.ssh/curd_aur_deploy -o IdentitiesOnly=yes
131        run: |
132          set -euo pipefail
133          git clone --depth 1 ssh://aur@aur.archlinux.org/curd.git aur-curd
134
135      - name: Update AUR package files
136        working-directory: aur-curd
137        run: |
138          set -euo pipefail
139
140          {
141            printf '%s\n' '# Maintainer: Wraient <rushikeshwastaken@gmail.com>'
142            printf '%s\n' "pkgname='curd'"
143            printf '%s\n' "pkgver=${{ steps.release.outputs.version }}"
144            printf '%s\n' 'pkgrel=1'
145            printf '%s\n' 'pkgdesc="Watch anime in CLI with AniList Tracking, Discord RPC, Intro/Outro/Filler/Recap Skipping, etc."'
146            printf '%s\n' "arch=('x86_64')"
147            printf '%s\n' 'url="https://github.com/Wraient/curd"'
148            printf '%s\n' "license=('GPL')"
149            printf '%s\n' "depends=('mpv' 'rofi' 'ueberzugpp')"
150            printf '%s\n' "provides=('curd')"
151            printf '%s\n' "conflicts=('curd')"
152            printf '%s\n' 'source=("https://github.com/Wraient/curd/releases/download/v${pkgver}/curd-linux-x86_64")'
153            printf '%s\n' "sha256sums=('${{ steps.release.outputs.sha256 }}')"
154            printf '\n'
155            printf '%s\n' 'package() {'
156            printf '%s\n' '  install -Dm755 "$srcdir/curd-linux-x86_64" "$pkgdir/usr/bin/curd"'
157            printf '%s\n' '}'
158          } > PKGBUILD
159
160          # Fail fast if checksum/source/install-target drift.
161          grep -q '^source=(' PKGBUILD
162          grep -q '^sha256sums=(' PKGBUILD
163          grep -q 'curd-linux-x86_64' PKGBUILD
164          grep -q 'install -Dm755 "\$srcdir/curd-linux-x86_64" "\$pkgdir/usr/bin/curd"' PKGBUILD
165          bash -n PKGBUILD
166
167          set +u
168          # shellcheck disable=SC1091
169          source PKGBUILD
170          set -u
171
172          pkgbase_value="${pkgbase:-}"
173          if [ -z "${pkgbase_value}" ]; then
174            pkgbase_value="${pkgname[0]:-${pkgname:-}}"
175          fi
176
177          {
178            printf 'pkgbase = %s\n' "$pkgbase_value"
179            [ -n "${pkgdesc:-}" ] && printf '\tpkgdesc = %s\n' "$pkgdesc"
180            [ -n "${pkgver:-}" ] && printf '\tpkgver = %s\n' "$pkgver"
181            [ -n "${pkgrel:-}" ] && printf '\tpkgrel = %s\n' "$pkgrel"
182            [ -n "${epoch:-}" ] && printf '\tepoch = %s\n' "$epoch"
183            [ -n "${url:-}" ] && printf '\turl = %s\n' "$url"
184            for item in "${arch[@]-}"; do [ -n "$item" ] && printf '\tarch = %s\n' "$item"; done
185            for item in "${license[@]-}"; do [ -n "$item" ] && printf '\tlicense = %s\n' "$item"; done
186            for item in "${makedepends[@]-}"; do [ -n "$item" ] && printf '\tmakedepends = %s\n' "$item"; done
187            for item in "${depends[@]-}"; do [ -n "$item" ] && printf '\tdepends = %s\n' "$item"; done
188            for item in "${checkdepends[@]-}"; do [ -n "$item" ] && printf '\tcheckdepends = %s\n' "$item"; done
189            for item in "${optdepends[@]-}"; do [ -n "$item" ] && printf '\toptdepends = %s\n' "$item"; done
190            for item in "${provides[@]-}"; do [ -n "$item" ] && printf '\tprovides = %s\n' "$item"; done
191            for item in "${conflicts[@]-}"; do [ -n "$item" ] && printf '\tconflicts = %s\n' "$item"; done
192            for item in "${replaces[@]-}"; do [ -n "$item" ] && printf '\treplaces = %s\n' "$item"; done
193            for item in "${source[@]-}"; do [ -n "$item" ] && printf '\tsource = %s\n' "$item"; done
194            for item in "${md5sums[@]-}"; do [ -n "$item" ] && printf '\tmd5sums = %s\n' "$item"; done
195            for item in "${sha1sums[@]-}"; do [ -n "$item" ] && printf '\tsha1sums = %s\n' "$item"; done
196            for item in "${sha224sums[@]-}"; do [ -n "$item" ] && printf '\tsha224sums = %s\n' "$item"; done
197            for item in "${sha256sums[@]-}"; do [ -n "$item" ] && printf '\tsha256sums = %s\n' "$item"; done
198            for item in "${sha384sums[@]-}"; do [ -n "$item" ] && printf '\tsha384sums = %s\n' "$item"; done
199            for item in "${sha512sums[@]-}"; do [ -n "$item" ] && printf '\tsha512sums = %s\n' "$item"; done
200            for item in "${b2sums[@]-}"; do [ -n "$item" ] && printf '\tb2sums = %s\n' "$item"; done
201            printf '\n'
202            for pkg in "${pkgname[@]-}"; do [ -n "$pkg" ] && printf 'pkgname = %s\n' "$pkg"; done
203          } > .SRCINFO
204
205          grep -Fq "pkgver = ${{ steps.release.outputs.version }}" .SRCINFO
206          grep -Fq "sha256sums = ${{ steps.release.outputs.sha256 }}" .SRCINFO
207
208      - name: Commit and push to AUR
209        working-directory: aur-curd
210        env:
211          GIT_SSH_COMMAND: ssh -i ~/.ssh/curd_aur_deploy -o IdentitiesOnly=yes
212        run: |
213          set -euo pipefail
214
215          git config user.name "github-actions[bot]"
216          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
217
218          if git diff --quiet; then
219            echo "No changes to commit"
220            exit 0
221          fi
222
223          git add PKGBUILD .SRCINFO
224          git commit -m "Update to version ${{ steps.release.outputs.version }}"
225          git push origin HEAD:master
226
227      - name: Cleanup
228        if: always()
229        run: |
230          rm -f ~/.ssh/curd_aur_deploy
231          rm -f ~/.ssh/curd_aur_deploy.pub