Reputation: 583
I have a flutter project which is fetching a package from a private repo on Github such as:
dependencies:
flutter:
sdk: flutter
my_package:
git:
url: [email protected]:username/my_package.git
ref: main
When I run flutter pub get
on my local computer, everything is fine since I have my computer connected to GitHub through ssh, but once I push on GitHub, the GitHub actions I have are failing to fetch the package with the message [email protected]: Permission denied (publickey)
. I understand the error message, I wonder if there is a better way to do this and pass the action.
Here is the GitHub action script:
name: Flutter
on: [pull_request, push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: subosito/[email protected]
- name: Install Dependencies
run: flutter packages get
- name: Format
run: flutter format --set-exit-if-changed lib test
- name: Analyze
run: flutter analyze lib test
- name: Run tests
run: flutter test --no-pub --coverage --test-randomize-ordering-seed random
Upvotes: 6
Views: 3780
Reputation: 3059
Use Deploy keys feature from GitHub 🔥
It is very easy, powerful, and designed for use cases like that.
Steps:
ssh-keygen -t ed25519 -C "[email protected]:username/repo.git"
Notes:
username
with you GitHub username that owns the repository and replace repo
with the repository name.You should see the output location of the generated key pair, it will be in form of two files, a public key file ends with .pub
and a private key file without an extension.
In the private repository you want to use, go to Settings
-> Deploy keys
-> Add Deploy key
, enter a title for the key (the title choice is up to you, it is used to easily identify the key later) and copy the content of the public key file and paste them in the key field.
For your GitHub Action, add a new Secret with the value of the contents of the Private Key file.
Use webfactory/ssh-agent action like the following:
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
Note: Replace SSH_PRIVATE_KEY
with the name of the secret that holds the value of the private key.
Tada 🎉, You have just used your private repository in your action in a secured way.
Upvotes: 1
Reputation: 128
A "Permission denied" error means that the server rejected your connection. GitHub Actions only have access to the repository they run for.
There are two ways to fetch this private package:
The easy way is to create to new personal access token and fetch the library using that access token
dependencies:
flutter:
sdk: flutter
private_package:
git:
url: https://username:[email protected]/user/repo
ref: main
ref is the branch name or commit id
url example : https://Mdkhaki:[email protected]/Mdkhaki/private-package.git
So, in order to access additional private repositories, you need to create an SSH key with sufficient access privileges. To learn more visit
Upvotes: 11