Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add pkg.pr.new GitHub action workflow #931

Closed
wants to merge 5 commits into from

Conversation

sadeghbarati
Copy link
Collaborator

@sadeghbarati sadeghbarati commented May 18, 2024

You need to configure pkg.pr.new GitHub app first

https://github.com/stackblitz-labs/pkg.pr.new

I need some help here cause I have zero experience in advanced GitHub actions

based on workflow file I want to run pkg.pr.new action on

  • push (if APPROVED then run action on every push)
  • pull_request [opened, synchronize, reopened] (if APPROVED then run action on every opened, synchronize, reopened)
  • pull_request_review [submitted]

One more thing about packages/plugins,
how to check this path if it's changed then run build all packages

in another word, if the user push 100 commits workflow doesn't run

until someone approved the PR
after that any other (push, synchronize, reopened) allowed to trigger the workflow job

@sadeghbarati
Copy link
Collaborator Author

@zernonia why not update to latest pnpm?

GitHub workflow files are outdated one uses node 16 one uses 18 😕

@sadeghbarati
Copy link
Collaborator Author

Chat GPT4o response xd

To configure GitHub Actions to run on push, pull_request, and pull_request_review events with the condition that it only runs if the pull request is approved, you need to use the GitHub Actions workflow_run event to create a workflow that is triggered after the approval. Here is how you can set it up:

  1. Create the main workflow: This workflow will run on push, pull_request, and pull_request_review events but will be conditional on the pull request being approved.

  2. Create a second workflow: This workflow will be triggered by the workflow_run event when the main workflow is completed.

Step 1: Main Workflow

Create a workflow file .github/workflows/main.yml:

name: Main Workflow

on:
  push:
  pull_request:
  pull_request_review:
    types: [submitted]

jobs:
  check-approval:
    runs-on: ubuntu-latest
    steps:
      - name: Check if PR is approved
        id: check_pr_approved
        uses: actions/github-script@v6
        with:
          script: |
            const { data: reviews } = await github.pulls.listReviews({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.payload.pull_request.number,
            });

            const approved = reviews.some(review => review.state === 'approved');

            core.setOutput('approved', approved);

  trigger-secondary-workflow:
    needs: check-approval
    if: steps.check_pr_approved.outputs.approved == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Trigger secondary workflow
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: 'Secondary Workflow'
          token: ${{ secrets.GITHUB_TOKEN }}
          ref: ${{ github.ref }}

Step 2: Secondary Workflow

Create another workflow file .github/workflows/secondary.yml:

name: Secondary Workflow

on:
  workflow_run:
    workflows: ["Main Workflow"]
    types:
      - completed

jobs:
  run-actions:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Run your actions
        run: |
          echo "Running actions on approved PR"
          # Add your actual actions/commands here

Explanation

  • Main Workflow:

    • Listens to push, pull_request, and pull_request_review events.
    • The check-approval job checks if the pull request is approved by listing all reviews and looking for at least one with the state approved.
    • If the pull request is approved, the trigger-secondary-workflow job triggers the secondary workflow using workflow-dispatch.
  • Secondary Workflow:

    • Listens to the workflow_run event for the completion of the "Main Workflow".
    • Runs the necessary actions if the PR is approved.

By structuring the workflows this way, you ensure that actions are only run if the pull request is approved, meeting your specified conditions.

@sadeghbarati
Copy link
Collaborator Author

sadeghbarati commented May 18, 2024

cc @Aslemammad 🙏

Thanks for this tool this will help maintainers ❤️

@Aslemammad
Copy link

Thank you team for using https://pkg.pr.new! It means a lot to us.

@zernonia
Copy link
Member

@zernonia why not update to latest pnpm?

GitHub workflow files are outdated one uses node 16 one uses 18 😕

Hahahaha no reason. Just didnt have the need to update because the pipeline we have is working now. If this new addition requires higher version of node, sure let's upgrade it!

@zernonia
Copy link
Member

@sadeghbarati It's being triggered here

image

Seems like the publishing work. However it would be nice to have the App to comment the npm command. cc @Aslemammad

npm i https://pkg.pr.new/radix-vue/radix-vue@2de584a    

Comment on lines +3 to +4
pull_request_review:
types: [submitted]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think publishing this also can be trigger only when there's change in packages, similar to build.yaml workflow

pull_request_review:
    types: [submitted]
    paths:
      - 'packages/**'

@sadeghbarati
Copy link
Collaborator Author

pkg.pr.new GitHub app configured after PR opened will close this and open same changes in new PR

@sadeghbarati sadeghbarati deleted the ci/pkg.pr.new branch May 18, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants