Github action fails to fetch pub package from private repo

Viewed 1185

I have a flutter project which is fetching a package from a private repo on Github such as:

dependencies:
  flutter:
    sdk: flutter

  my_package:
    git:
      url: git@github.com:username/my_package.git
      ref: main

When I run flutter pub get on my local computer, everything is fine since I have my computer connected to GitHub through ssh, but once I push on GitHub, the GitHub actions I have are failing to fetch the package with the message git@github.com: Permission denied (publickey). I understand the error message, I wonder if there is a better way to do this and pass the action.

Here is the GitHub action script:

name: Flutter

on: [pull_request, push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - uses: subosito/flutter-action@v1.5.0

      - name: Install Dependencies
        run: flutter packages get

      - name: Format
        run: flutter format --set-exit-if-changed lib test

      - name: Analyze
        run: flutter analyze lib test

      - name: Run tests
        run: flutter test --no-pub --coverage --test-randomize-ordering-seed random

2 Answers

A "Permission denied" error means that the server rejected your connection. GitHub Actions only have access to the repository they run for.

There are two ways to fetch this private package:

1- Personal access tokens

The easy way is to create to new personal access token and fetch the library using that access token

dependencies:
  flutter:
    sdk: flutter
  private_package:
    git:
      url: https://username:token@github.com/user/repo
      ref: main

  • ref is the branch name or commit id

  • url example : https://Mdkhaki:ghp_LMWzHKnNctxxX9dmP4kxmnwjshRMmJ2MyjF8@github.com/Mdkhaki/private-package.git

2- SSH-agent

So, in order to access additional private repositories, you need to create an SSH key with sufficient access privileges. To learn more visit

If you're working with a personal repo that is not necessarily private, you can use it in your GitHub actions like this:

dependencies:
  flutter:
    sdk: flutter
  your_package:
    git:
      url: https://github.com/your_username/repo.git
      ref: master
Related