Using Dart & Flutter Packages from a Private Git Repo

Using Dart & Flutter Packages from a Private Git Repo

Problem

We want to use Dart/Flutter packages from a private git repo on Github.

The following article will show you how.

Set up SSH client

To authenticate ourselves into the private repo, we will need to use the SSH protocol.

Mac and Linux

Mac and Linux usually has an ssh client configured. If not present, you can install one with your system's a package manager (apt, dnf, pacman, brew etc..) .

Windows 10

UPDATE: It seems the latest versions of Windows have ssh ready for use.

Windows 10 has a built-in OpenSSH client. But it must be activated first. Check here: howtogeek.com/336775/how-to-enable-and-use-..

Powershell also has a built-in ssh client.

Set up SSH Keys on Github

We need to associate an SSH key to provide authentication to the private repo.

  • Sign in to your github account that has access to the private repo
  • Go to github.com/settings/keys and add your public SSH key into your account

Generating SSH Keys

If you don't have an SSH key, you can generate one. For simplicity's sake, we will create one that doesn't have a passphrase, as doing so would require extra work to make it automatic (setting up ssh-agent on Linux/Mac).

Mac and Linux and Windows using Powershell

  • Run ssh-keygen on your terminal
  • Specify a name for this keypair, in this example we use the name my_key
  • For now skip the process where it asks for a passphrase
  • This will create a two keys, a public key (my_key.pub) and private key (my_key).

Windows 10

Set up SSH config on your local machine

In the .ssh folder, where your public and private keys are stored by default, lives a config file.

This file will help our git client manage our connection to remote git hosts (like Github, Bitbucket, Gitlab etc) when we clone using SSH.

Here we can set it up to associate an Identity File (a private/pub key pair) to a specific host.

Open the ssh/.config file. It can be found below:

Mac and Linux

$HOME/.ssh/config

Windows

C:\Users\your_username\.ssh\config

Add the following snippet to your .ssh/config file:

# Using your company key
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    User git
    IdentityFile ~/.ssh/my_key
  • Host can mean anything. It's just used for us to separate between different accounts we use on, say, GitHub.
  • HostName is the location of the remote git host in our case it's github.com
  • PreferredAuthentications tells your client to use public key authentication first, before password based authentication
  • User has to be git when authenticating with Github
  • IdentityFile is the location of your private key.

Check if you can clone the private repo

If you can clone the private repo, it means you are authenticated.

Use the package as a dependency

In your Flutter project, open the pubspec.yaml

Under dependencies, you can add your private flutter package located in your repository.

dependencies:
  my_test_package:
   git:
     url: git@github.com:my_company_repo/my_test_package.git

Run flutter pub get and the package should be available for use in the project.

Specifying a version

There's no direct versioning system when using a git package dependency. However, we can specify which branch, or tag, or commit hash to pull from.

dependencies:
  my_test_package:
   git:
     url: git@github.com:my_company_repo/my_test_package.git
     ref: master

The ref property can have a branch, tag or a specific commit hash.

You can read more about this spec here: flutter.dev/docs/development/packages-and-p..

Did you find this article valuable?

Support Jethro Lorenzo Garcia Lising by becoming a sponsor. Any amount is appreciated!