Can I use environment variables in pubspec.yaml?
Asked Answered
C

2

7

I have private dependency from GitLab in my Flutter mobile app project. It is specified in pubspec.yaml with git repository link and I can successfully run pub get if I use ssh for it (I have generated ssh key and add it to my gitlab account)

  my_private_package:
    git:
      url: ssh://git@************/my_private_package.git

Now I want to build my app with AppCenter and deploy to stores. It is not possible to use ssh keys on AppCenter build phase, so the only working solution I see is to use GitLab Deploy Token as environment variable to access my private package from AppCenter machine. Git link to access the repository becomes this:

  my_private_package:
    git:
      url: https://gitlab+deploy-token-1:${DEPLOY_TOKEN}@************/my_private_package.git

Now, as far as I understand, pubspec can't resolve DEPLOY_TOKEN environment variable this way. pub get completes with error:

Git error. Command: `git clone --mirror https://gitlab+deploy-token-1:******@*******/my_private_package.git /Users/***/Library/flutter/.pub-cache/git/cache/my_private_package***`
stdout:                                                                 
stderr: Cloning into bare repository '/Users/***/Library/flutter/.pub-cache/git/cache/my_private_package***'...
remote: HTTP Basic: Access denied                                       
remote: You must use a personal access token with 'read_repository' or 'write_repository' scope for Git over HTTP.
remote: You can generate one at https://*****/-/profile/personal_access_tokens
fatal: Authentication failed for 'https://gitlab.*****/my_private_package.git/'
exit code: 128   

I also tried to use personal access tokens mentioned in error message above, but getting the same result. Although git clone https://gitlab+deploy-token-1:${DEPLOY_TOKEN}@*******/my_private_package.git from command line works fine if DEPLOY_TOKEN environment variable specified.

My question: Is it possible to use evnironment variables directly from pubspec.yaml? If no, how can I get my private package from another (not gitlab runner) CI machine?

Crowd answered 29/3, 2021 at 7:42 Comment(1)
Some alternatives: generate your pubspec.yaml file, or use path dependencies with Git repositories that you fetch separately.Melli
I
0

Currently it's not possible to set environment variables in pubspec.yaml as mentioned in this thread. As a workaround you can fetch the repository locally and configure it as a path package.

Immolation answered 10/8, 2023 at 14:12 Comment(0)
O
0

Although App Center is scheduled for shutdown at the end of March 2025, iirc, we use a "post-clone" script to add an SSH key for our other private dependencies.

# Add SSH key so that we can access dependencies in other private Bitbucket repos
# See https://github.com/microsoft/appcenter/issues/237#issuecomment-527991036
echo "Adding private SSH key for accessing Bitbucket dependencies in other repos"
mkdir -p ~/.ssh
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts
echo $BITBUCKET_SSH_KEY | base64 -D > ~/.ssh/bitbucket_appcenter
chmod 600 ~/.ssh/bitbucket_appcenter
ssh-add ~/.ssh/bitbucket_appcenter

We did that a while ago and it worked fine, although we no longer use App Center for builds.

Olia answered 7/10 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.