How to use Flutter packages hosted in private git repo into CI/CD

In this article, I’ll show you how to use Flutter packages hosted in a private git repository into a CI/CD pipeline.

Introduction

When working on a Flutter project, you may need to use packages hosted in a private git repository. This can be useful when you want to keep your code private or when you want to reuse code across multiple projects.

Prerequisites

Before you start, you need to have the following:

  • A Flutter project.
  • A private git repository with the package you want to use.
  • A personal access token for your git provider.
  • A CI/CD pipeline set up for your Flutter project.

Steps

Here are the steps to use Flutter packages hosted in a private git repository into a CI/CD pipeline:

1. Create a personal access token

First, you need to create a personal access token in your Git provider. In this example, I’ll use GitHub.

  1. Go to your GitHub account settings.
  2. Click on Developer settings.
  3. Click on Personal access tokens.
  4. Click on Generate new token.
  5. Give your token a name and select the scopes you need.
  6. Click on Generate token.
  7. Copy the token and save it in a secure place.

2. Use the package in your Flutter project

Now you can use the package in your Flutter project by adding it to your pubspec.yaml file.

# pubspec.yaml
dependencies:
  my_private_package:
    git:
      url: https://github.com/<user>/<my_private_package>.git
      ref: main

3. Set up CI/CD pipeline

Finally, you can set up a CI/CD pipeline to build and deploy your Flutter project using the package from the private git repository.

Here is an example of a dockerfile that builds and deploys a Flutter project using a package from a private git repository.


RUN git config --global credential.helper manager

# Inject $GITHUB_PAT PAT only if using environment variable approach
ENV GIT_ACCESS_TOKEN=<github_pat>

RUN git config --global url."https://${GIT_ACCESS_TOKEN}@github.com".insteadOf "https://github.com"

# Run build commands
RUN flutter clean
RUN flutter pub get
RUN flutter gen-l10n
RUN flutter pub run build_runner build --delete-conflicting-outputs
RUN flutter build web

RUN git config --global --remove-section url."https://${GIT_ACCESS_TOKEN}@github.com"

Pass the personal access token as a build argument when you call the docker build command:


docker build --build-arg GIT_ACCESS_TOKEN=<github_pat> -t "${IMAGE}" .