From 904ea6ee536e0a5401ba40c0d177a18e89708531 Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Sat, 2 Nov 2024 00:13:05 +0530 Subject: [PATCH] refact(ci): Preview workflow to create PR (#901) * refact: use tags instead of runner id * refact: pr instead of auto commit --------- Co-authored-by: Chris Titus --- .github/workflows/linutil.yml | 8 ---- .github/workflows/preview.yml | 72 +++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/.github/workflows/linutil.yml b/.github/workflows/linutil.yml index 1184549b..187f1e54 100644 --- a/.github/workflows/linutil.yml +++ b/.github/workflows/linutil.yml @@ -78,11 +78,3 @@ jobs: env: version: ${{ env.version }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Upload build artifacts - uses: actions/upload-artifact@v4 - with: - name: linutil-artifact - path: build/x86_64-unknown-linux-musl/release/linutil - compression-level: 0 - overwrite: true \ No newline at end of file diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index bb63b2d5..e79fbdfe 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -3,8 +3,8 @@ name: LinUtil Preview on: workflow_dispatch: inputs: - run_id: - description: 'Run ID of LinUtil Release' + tag_name: + description: 'Tag name' required: true workflow_run: workflows: ["LinUtil Release"] @@ -14,29 +14,46 @@ on: jobs: generate_preview: runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write steps: - - uses: actions/checkout@v4 + - name: Checkout source + uses: actions/checkout@v4 - - name: Set Run ID - run: | - if [ "${{ github.event_name }}" == "workflow_run" ]; then - echo "run_id=${{ github.event.workflow_run.id }}" >> $GITHUB_ENV - else - echo "run_id=${{ github.event.inputs.run_id }}" >> $GITHUB_ENV - fi - - - name: Download build artifacts - uses: actions/download-artifact@v4 + - name: Get tag name ( Workflow Run ) + id: latest_tag + uses: actions/github-script@v7 + if: ${{ github.event_name }} == 'workflow_run' with: - name: linutil-artifact - github-token: ${{ secrets.GITHUB_TOKEN }} - run-id: ${{ env.run_id }} + script: | + const releases = await github.rest.repos.listReleases({ + owner: context.repo.owner, + repo: context.repo.repo, + per_page: 1 + }); + return releases.data[0].tag_name; + result-encoding: string + + - name: Set tag name ( Workflow Run ) + if: ${{ github.event_name }} == 'workflow_run' + run: echo "tag_name=${{ steps.latest_tag.outputs.result }}" >> $GITHUB_ENV + + - name: Set tag name ( Workflow Dispatch ) + if: ${{ github.event_name }} == 'workflow_dispatch' + run: echo "tag_name=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV + + - name: Download binary + run: | + curl -LO "https://github.com/${{ github.repository }}/releases/download/${{ env.tag_name }}/linutil" - name: Set env run: | chmod +x linutil - echo "${{ github.workspace }}" >> $GITHUB_PATH + mkdir -p build + mv linutil build/linutil + echo "${{ github.workspace }}/build" >> $GITHUB_PATH - name: Generate preview uses: charmbracelet/vhs-action@v2.1.0 @@ -46,16 +63,15 @@ jobs: - name: Move preview run: mv preview.gif docs/assets/preview.gif - - name: Create Pull Request - uses: peter-evans/create-pull-request@v6 + - name: Create PR + uses: peter-evans/create-pull-request@v7.0.5 with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: Preview for ${{ env.run_id }} - title: 'Update preview GIF' - body: 'Automatically generated preview GIF from workflow run ${{ env.run_id }}' - branch: update-preview-gif - base: main + title: "release: Preview for ${{ env.tag_name }}" + commit-message: "release: Preview for ${{ env.tag_name }}" + branch: "preview-${{ env.tag_name }}" delete-branch: true - add-paths: | - docs/assets/preview.gif - if: success() + labels: "documentation" + body: | + ## Preview for ${{ env.tag_name }} + - Tag: [${{ env.tag_name }}](https://github.com/${{ github.repository }}/tree/${{ env.tag_name }}) + - Preview: ![Preview](https://raw.githubusercontent.com/${{ github.repository }}/preview-${{ env.tag_name }}/docs/assets/preview.gif)