|
| 1 | +name: Update Latest Release Assets |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, master ] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-and-attach: |
| 12 | + name: Build (${{ matrix.os }}) and attach to latest release |
| 13 | + runs-on: ${{ matrix.os }} |
| 14 | + strategy: |
| 15 | + fail-fast: false |
| 16 | + matrix: |
| 17 | + os: [ macos-latest, windows-latest ] |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Setup Python |
| 24 | + uses: actions/setup-python@v5 |
| 25 | + with: |
| 26 | + python-version: '3.11' |
| 27 | + |
| 28 | + - name: Install dependencies |
| 29 | + run: | |
| 30 | + python -m pip install --upgrade pip |
| 31 | + pip install -r requirements.txt pyinstaller |
| 32 | +
|
| 33 | + - name: Build app (macOS) |
| 34 | + if: startsWith(matrix.os, 'macos') |
| 35 | + run: | |
| 36 | + python build.py --name ChatMock --entry app_qt.py --dmg |
| 37 | + mv dist/ChatMock.dmg dist/ChatMock-macos.dmg |
| 38 | +
|
| 39 | + - name: Build app (Windows) |
| 40 | + if: startsWith(matrix.os, 'windows') |
| 41 | + shell: pwsh |
| 42 | + run: | |
| 43 | + python build.py --name ChatMock --entry app_qt.py |
| 44 | + if (Test-Path dist/ChatMock-windows.zip) { Remove-Item dist/ChatMock-windows.zip -Force } |
| 45 | + Compress-Archive -Path "dist/ChatMock/*" -DestinationPath "dist/ChatMock-windows.zip" |
| 46 | +
|
| 47 | + - name: Set asset variables |
| 48 | + id: asset |
| 49 | + shell: bash |
| 50 | + run: | |
| 51 | + if [[ "${{ matrix.os }}" == macos-* ]]; then |
| 52 | + echo "name=ChatMock-macos.dmg" >> $GITHUB_OUTPUT |
| 53 | + echo "path=dist/ChatMock-macos.dmg" >> $GITHUB_OUTPUT |
| 54 | + else |
| 55 | + echo "name=ChatMock-windows.zip" >> $GITHUB_OUTPUT |
| 56 | + echo "path=dist/ChatMock-windows.zip" >> $GITHUB_OUTPUT |
| 57 | + fi |
| 58 | +
|
| 59 | + - name: Get latest release |
| 60 | + id: latest_release |
| 61 | + uses: actions/github-script@v7 |
| 62 | + with: |
| 63 | + script: | |
| 64 | + const { owner, repo } = context.repo; |
| 65 | + try { |
| 66 | + const latest = await github.rest.repos.getLatestRelease({ owner, repo }); |
| 67 | + core.setOutput('id', latest.data.id.toString()); |
| 68 | + core.setOutput('tag', latest.data.tag_name); |
| 69 | + core.setOutput('upload_url', latest.data.upload_url); |
| 70 | + } catch (e) { |
| 71 | + core.warning(`No releases found or error fetching latest release: ${e.message}`); |
| 72 | + core.setOutput('id', ''); |
| 73 | + core.setOutput('tag', ''); |
| 74 | + core.setOutput('upload_url', ''); |
| 75 | + } |
| 76 | +
|
| 77 | + - name: Delete prior asset for this platform |
| 78 | + if: steps.latest_release.outputs.tag != '' |
| 79 | + uses: actions/github-script@v7 |
| 80 | + env: |
| 81 | + RELEASE_ID: ${{ steps.latest_release.outputs.id }} |
| 82 | + ASSET_NAME: ${{ steps.asset.outputs.name }} |
| 83 | + with: |
| 84 | + script: | |
| 85 | + const { owner, repo } = context.repo; |
| 86 | + const release_id = Number(process.env.RELEASE_ID); |
| 87 | + const assetName = process.env.ASSET_NAME; |
| 88 | + const assets = await github.rest.repos.listReleaseAssets({ owner, repo, release_id, per_page: 100 }); |
| 89 | + const match = assets.data.find(a => a.name === assetName); |
| 90 | + if (match) { |
| 91 | + core.info(`Deleting existing asset: ${match.name} (id=${match.id})`); |
| 92 | + await github.rest.repos.deleteReleaseAsset({ owner, repo, asset_id: match.id }); |
| 93 | + } else { |
| 94 | + core.info(`No existing asset named ${assetName} found.`); |
| 95 | + } |
| 96 | +
|
| 97 | + - name: Upload new asset to latest release |
| 98 | + if: steps.latest_release.outputs.tag != '' |
| 99 | + uses: softprops/action-gh-release@v2 |
| 100 | + with: |
| 101 | + tag_name: ${{ steps.latest_release.outputs.tag }} |
| 102 | + files: ${{ steps.asset.outputs.path }} |
| 103 | + |
0 commit comments