Boris Kayi
Boris Kayi

Reputation: 583

Github action fails to fetch pub package from private repo

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: [email protected]: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 [email protected]: 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/[email protected]

      - 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

Upvotes: 6

Views: 3780

Answers (2)

Moaz El-sawaf
Moaz El-sawaf

Reputation: 3059

Use Deploy keys feature from GitHub 🔥

It is very easy, powerful, and designed for use cases like that.

Steps:

  1. Create an SSH key on your local machine with the following command:
ssh-keygen -t ed25519 -C "[email protected]:username/repo.git"

Notes:

  • Replace username with you GitHub username that owns the repository and replace repo with the repository name.
  • Don't set a passphrase for the key or it will not work, just hit enter when you prompted for that.

You should see the output location of the generated key pair, it will be in form of two files, a public key file ends with .pub and a private key file without an extension.

  1. In the private repository you want to use, go to Settings -> Deploy keys -> Add Deploy key, enter a title for the key (the title choice is up to you, it is used to easily identify the key later) and copy the content of the public key file and paste them in the key field.

  2. For your GitHub Action, add a new Secret with the value of the contents of the Private Key file.

  3. Use webfactory/ssh-agent action like the following:

- uses: webfactory/[email protected]
  with:
    ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

Note: Replace SSH_PRIVATE_KEY with the name of the secret that holds the value of the private key.

Tada 🎉, You have just used your private repository in your action in a secured way.

Upvotes: 1

Moe Arvand
Moe Arvand

Reputation: 128

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:[email protected]/user/repo
      ref: main

  • ref is the branch name or commit id

  • url example : https://Mdkhaki:[email protected]/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

Upvotes: 11

Related Questions