Using SSH keys - Connect to Ionic Pro

Posted on October 03, 2017 in Develop
Updated: June 15, 2021

HowTo connect to Ionic Pro, when ionic link fails?
Ionic Pro is a new site taking over the features from Ionic Cloud - a hosted mobile build, test and deploy service

Intro

The problem

I wanted to connect to Ionic Pro from Windows.
But the attempt failed.

It kept coming with one error after the other. The latest I had difficulty to pass was:

>ionic link
× Looking up your apps - failed!
Request: GET https://api.ionicjs.com/apps?page=1&page_size=25
Response: 401
Body:
{ type: 'Unauthorized', link: null, message: 'Invalid Token' }

This blog has a workaround for ionic link.

ionic link

ionic link CLI command connects your local git repo with a remote repo at Ionic Pro. That remote repo was created when you manually created an app at Ionic Pro site.

It does that by doing probably all these tasks

  1. creating a set of SSH keys,
  2. uploading the public key to Ionic Pro and
  3. adding the app id to project-root\ionic.config.json
  4. adding remote ionic repo to your local project

So when it won't we can do it ourselves.

The workaround

Prerequisites

  • Create a ionic app in Ionic Pro
    After creation the id of the app will be the last part of the url - e.g. as in https://apps.ionic.io/app/09faf85a
  • Create a ionic starter app on your PC

1. Create a set of SSH keys

You need ssh-keygen.exe to generate a set of public/private SSH keys. ssh-keygen.exe is installed as part of

  • Git (default installed into C:\Program Files\Git\usr\bin) or
  • OpenSSH (default installed into C:\Program Files\OpenSSH-Win64)

On my PC I also had HerokuToolkit installed with a path in ssh-keygen.bat to ssh-keygen.exe.
But this path was wrong. It seemed to be part of some problems. Rename that file, so it is not called instead of one of the real .exe files.

From Git bash:

# https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

# Generate key-pair into C:\Users\your_userid\.ssh\
c:
cd C:/Users/your_userid/.ssh
# http://docs.ionic.io/services/ssh-keys/
ssh-keygen.exe -t rsa -b 4096 -C "your_email@example.com"

# Check if ssh agent is running
eval $(ssh-agent -s)
# Agent pid 12345 - yes

# Add private key to agent - 
ssh-add C:/Users/your_userid/.ssh/id_rsa

2. Upload the public key to Ionic Pro

Paste the content of file id_rsa.pub to a new key in Ionic Pro's SSH store

3. Add the app id to your Ionic config in your local repo

Edit project-root\ionic.config.json

{
  "name": "Your Magic App",
  "app_id": "09faf85a", # replace with your id

4. Add remote ionic repo to your local project

# https://dashboard.ionicjs.com/app/09faf85a/settings/git
# Add Ionic Pro as remote repo
git remote add ionic ssh://git@git.ionicjs.com:your_ionic_userid/your_ionic_appname.git

After the workaround

5. Upload your app to Ionic Pro

# Upload to Ionic Pro
git push ionic master

6. Make Github your default remote repo

# Add github as remote repo
git remote add origin https://github.com/your_git_userid/your_git_appname.git
# Upload to github and set it default (for pull)
git push --set-upstream origin master
# Check your remotes
git remote -v

7. Continue to use Ionic Pro

On the url for your app - e.g. https://dashboard.ionicjs.com/app/09faf85a/code/builds/list you can continue to deploy, preview and more.

You might also need to modify src/app/app.module.ts. Define a CloudSettings object.


Tips

Download an app as a starter app

If you have access to a Ionic Pro app, you can git clone it with

ionic start --pro-id 09faf85a

... if I am asuming correct.

If you want OpenSSH on Windows

.. then here is how you install OpenSSH

# do this from an admin prompt
choco install openssh # takes a while
# reload environment variables
refresh env

If you want Git Bash to add your Github SSH key to an agent

How to Setup SSH Authentication for Git Bash on Windows

If you want Powershell to add your SSH key to an agent

Setting up the SSH Agent

# PS1
# Make sure you're running as an Administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
# Add SSH private key to agent
ssh-add c:\users\<yourid>\.ssh\id_rsa
# Verity key added
ssh-add -l

Links

The End