Give credentials/keys/tokens to a snapcraft build

Hey there,

I feel like this should be a very common thing to do, but I cannot find any pages or posts about it.

How do I give arguments to my snapcraft build? Like you would in docker do docker build --build-arg=SOME_TOKEN=lksdfjow?

The reason I am asking is that inside my snapcraft I build a flutter app that pulls dependencies from private GitLab repositories.

In docker I have achieved this by giving my GitLab credentials to my docker build, and then inside the the build I create a .git-credentials file by doing echo "https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@gitlab.com" > /.git-credentials

I did already get it to work like this:

parts:
  git-credentials:
    plugin: dump
    source: .
    stage:
      - .git-credentials # to stage files leading with a . you have to stage them explicitly

  my-flutter-app:
    after: [flutter-git, git-credentials]
    source: .
    plugin: nil
    override-build: |
      # Set git credentials so we can pull private repositories in "pub get"
      git config --global credential.helper "store --file $CRAFT_PART_BUILD/.git-credentials"

Is that the correct way to do it?

Just asking to see if there is other ways + to create some documentation for any others that are curious.

Unlike Docker, Snapcraft’s design leans away from passing arbitrary environment variables and build arguments.

For private repo access, there is a --bind-ssh that you can use:

I see. Do you disagree with the credentials-file solution?

No, I think your file-based solution is good too!

1 Like