Skip to content

GitHub Actions Integration

Roman Khadka edited this page Apr 16, 2024 · 8 revisions

You can also run Pronto as a GitHub action.

Here's an example .github/workflows/pronto.yml workflow file using the github_status and github_pr formatters and running on each GitHub PR, with pronto-rubocop as the runner:

name: Pronto
on: [pull_request_target]

jobs:
  pronto:

    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - run: |
          git fetch --no-tags --prune --depth=10 origin +refs/heads/*:refs/remotes/origin/*
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
      - name: Setup pronto
        run: gem install pronto pronto-rubocop
      - name: Run Pronto
        run: PRONTO_PULL_REQUEST_ID="${{ github.event.pull_request.number }}" PRONTO_GITHUB_ACCESS_TOKEN="${{ github.token }}" pronto run -f github_status github_pr -c origin/${{ github.base_ref }}
  • ${{ github.token }} is scoped to the current repository, so if you want to checkout a different repository that is private you will need to provide your own PAT. e.g ${{ secrets.GitHub_PAT }} # GitHub_PAT is a secret that contains your PAT.

If you intend to use bundler/Gemfile to manage all the dependencies (e.g. all the rubocop extension gems):

name: Pronto
on: [pull_request_target]

jobs:
  pronto:

    runs-on: ubuntu-latest

    env:
      # Requires the relevant gems to have a separate group in `Gemfile`
      # At the time of writing there is no way to only install gems in specific group(s) by specifying the group name(s)
      BUNDLE_WITHOUT: "default development test"
      # `MAKE="make --jobs $(nproc)"` is from
      # https://build.betterup.com/one-weird-trick-that-will-speed-up-your-bundle-install/
      # Only works for MRI
      #
      # Using 4 since https://github.com/ruby/setup-ruby use 4
      MAKE: "make --jobs 4"

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - run: |
          git fetch --no-tags --prune --depth=10 origin +refs/heads/*:refs/remotes/origin/*

      # If your project only specifies its Ruby version in its
      # Gemfile, you'll need to specify a version for the
      # action to use. See documentation for the
      # ruby/setup-ruby action for details.
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version:  "2.7"
          # Run `bundle install` with cache when `true`
          bundler-cache: true

      - name: Setup pronto
        run: gem install pronto pronto-rubocop
      - name: Run Pronto
        run: bundle exec pronto run -f github_status github_pr -c origin/${{ github.base_ref }}
        env:
          PRONTO_PULL_REQUEST_ID: ${{ github.event.pull_request.number }}
          PRONTO_GITHUB_ACCESS_TOKEN: "${{ github.token }}"
          BUNDLE_PATH: "vendor/bundle"
# Gemfile
# This might not be the right way to specify the group(s) for these gems
# Try it yourself
group :development do
  # Ruby static code analyzer
  gem "rubocop", ">= 1.15.0", require: false, group: :pronto
  gem 'rubocop-performance', ">= 1.11.0", require: false, group: :pronto
  gem 'rubocop-rails', '>= 2.11.0', require: false, group: :pronto
  gem 'rubocop-rake', '>= 0.5.1', require: false, group: :pronto

  # Mainly for being run in GH Action
  gem "pronto", require: false, group: :pronto
  gem "pronto-rubocop", require: false, group: :pronto
end

Notes:-

* Known Issues

"Resource not accessible by integration"

  • pull_request_target lets you execute actions triggered by pull requests, but have access to secrets (the file available are from the main branch, not the PR)

  • workflow_run lets you run actions after other actions have completed, with access to secrets

this blog post provides details of GitHub Actions improvements for fork and pull request workflows

* Checkout code

please use GitHub doc checkout to update git checkout according to need.

  • In above pronto.yml, setting linting pipelines up with a --depth=N (maybe 10 usually?) to reduce the size of the transfer and make the clone faster.

* Run Pronto

If your workflow is using bundler or Gemfile in that case, you might need to use bundle exec before pronto command.

- name: Run Pronto
        run: |
          PRONTO_PULL_REQUEST_ID="${{ github.event.pull_request.number }}" PRONTO_GITHUB_ACCESS_TOKEN="${{ github.token }}" bundle exec pronto run -f github_status github_pr -c origin/${{ github.base_ref }}

Rails Application Example:-

add pronto and pronto runners gems in Gemfile

on: [pull_request]

name: Pronto

jobs:
  linters:
    name: Linters
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - run: |
          git fetch --no-tags --prune --depth=10 origin +refs/heads/*:refs/remotes/origin/*

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
      - name: Ruby gem cache
        uses: actions/cache@v1
        with:
          path: vendor/bundle
          key: |
            ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-
      - name: Install gems
        run: |
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3

      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: 10.13.0
      - name: Find yarn cache location
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: JS package cache
        uses: actions/cache@v1
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: |
            ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install packages
        run: |
          yarn install --pure-lockfile
      - name: Run Pronto
        run: |
          PRONTO_PULL_REQUEST_ID="${{ github.event.pull_request.number }}" PRONTO_GITHUB_ACCESS_TOKEN="${{ github.token }}" bundle exec pronto run -f github_status github_pr -c origin/${{ github.base_ref }}