A secret Flutter lovestory
Flutter Pubspec and private Gitlab Repositories
Integrate a private GitLab repository with Deploy Tokens in your pubspec.yaml
Imagine you created your own package and want to reference it in another project. It’s easy to do so if you reference the package by path. Then your pubspec.yaml
would look like this:
As soon as you get a bit more professional you probably want to automate the build chain or your testing and therefore access the package from a CI server. Also not a problem. You simply host your code in a git repository and reference it. This would look like:
All quite easy and straight forward until here. More about referencing unpublished packages at “Dependencies on unpublished packages” in the offical documentation.
It gets more complicated though if you want to reference a private repository from this CI server. In my company, we chose Gitlab CI
for automating our build process and I want to highlight how to solve the problem with this special setup.
How to access a private repository in Gitlab from CI
Gitlab offers a simple way to access private repositories from a CI server. The Deploy Tokens!
Deploy tokens allow you to download (
git clone
) or push and pull packages and container registry images of a project without having a user and a password.
You can create a Deploy Token
for your repository by entering your repository and by navigating to Settings
→ Repository
. For a complete guide on how to do this, simply follow this official guide by GitLab
Make sure to write down the Deploy Token
as it won’t be accessible once you leave the page. After the creation of such a token you will see something like this
Once you set this up you are able to access your repository without username and password.
How to integrate a private Gitlab repository in your pubspec.yaml
We created a Deploy Token
and can now use it to reference the repository in our pubspec.yaml
as follows:
As you can see you have to use a combination of token name and token password to reference the repository and you can
- either reference a
tag
- or a
branch
More about referencing the git repository in your pubspec.yaml
here:
That’s it. You successfully included your private Gitlab repository in your Flutter
project. Now automate the hell out of your project. 3,2,1…
I hope you liked it. I think it’s pubspectacular.🤙
Follow Flutter Community on Twitter: https://www.twitter.com/FlutterComm