45

Deploying to Vapor Cloud 2 Tutorial [FREE]

 5 years ago
source link: https://www.tuicool.com/articles/hit/eyuMjyq
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

You’re ready to deploy that Vapor app you’ve worked so hard on. But how can you deploy it to a cloud solution, with minimum hassle? Vapor Cloud to the rescue!

Vapor Cloud 2 is a Platform as a Service (PaaS) built by the Vapor team specifically for hosting Vapor applications. In this tutorial, you’ll learn how to deploy your application to Vapor Cloud 2.

Vapor Cloud 2 is designed to simplify configuring servers and managing deployments so you can concentrate on writing code. Vapor Cloud 2 is still in pre-release, but it’s available to use for testing the deployment of your apps. And there’s some great news in this latest iteration: Vapor Cloud 2 provides a free database tier for persisting data! You can find out more about the release schedule of Vapor Cloud 2 here .

You’ll use a pre-built Vapor app named TIL (Today I Learned) that hosts user-supplied acronyms.

The high-level steps you’ll perform in this tutorial are:

  • Setting up a Vapor Cloud account.
  • Setting up Git access to the Vapor Cloud Git server.
  • Forking a pre-built Vapor app on GitHub.
  • Creating and setting up a new Vapor Cloud application for your app.
  • Deploying your app to Vapor Cloud and bringing it online.

Note : This tutorial assumes you have some experience with using Vapor to build web apps. See Getting Started with Server-side Swift with Vapor if you’re new to Vapor. This tutorial also assumes you have some experience working with the command line, SSH, Docker, Git, and GitHub.

Getting Started

Click the Download Materials button at the top or bottom of this tutorial to download a text file in which you’ll keep track of all the credentials you’ll create in this tutorial.

You’ll also need a Vapor Cloud account in order to deploy your app. If you don’t have one, sign up for one here .

signup0-592x500.png

Once you’ve confirmed your email account and logged in to Vapor Cloud, you’ll see the Dashboard , which is still under construction at the time of writing this tutorial.

dashboard-1-592x500.png

Vapor 2 Dashboard

Setting Up Git Access

To host your code, you’ll need to access Vapor Cloud’s private Git server. The best way to do this is by adding your public SSH key to your new account.

On macOS, in Terminal , enter this command to find the file that contains your public SSH key:

ls ~/.ssh

Your file probably has one of the default public key filenames: id_rsa.pub , id_dsa.pub , id_ecdsa.pub or id_ed25519.pub .

Note : If you don’t have an SSH key, follow GitHub’s instructions to create one.

Copy the name of the id_something.pub file, and paste it into the following command:

pbcopy < ~/.ssh/<your .pub file>

This copies your public SSH key to the clipboard.

Now go to Settings in the Vapor Cloud console, and click the + button in the upper right corner to add your SSH key:

ssh_key-1-593x500.png

Add SSH Key

Give the key a name, paste the key in, then click Create . To verify your access, enter the following command in Terminal :

ssh [email protected]

Enter yes if it asks “Are you sure you want to continue connecting (yes/no)?”.

If your SSH key is working, you’ll see output similar to this:

PTY allocation request failed on channel 0
Hi [email protected], You've successfully authenticated to Vapor Cloud Git
Connection to git.code.vapor.cloud closed.

Forking the TIL Project

Rather than start from scratch, you’ll fork an existing Vapor app, and work on your fork for the rest of this tutorial.

The Vapor TIL app is available in a public repo on GitHub .

til-github-1-650x298.png

Vapor TIL GitHub repo

Running the app locally looks like this:

til-app-1-650x291.png

TIL App

To work with your own version of this app, fork the repo to your own GitHub account:

fork-1-650x126.png

Fork the repo

Next, create a local clone of the repo on your development machine by cloning the repo fork from your account. In Terminal , cd to the directory where you want your local repo to live.

Then, if you’ve set up your GitHub profile with SSH , enter this command:

git clone [email protected]:<YOUR_GITHUB_USERNAME>/vapor-til.git

If you haven’t set up SSH access, you should clone the repository with HTTPS :

git clone https://github.com/<YOUR_GITHUB_USERNAME>/vapor-til.git

At this point, you want to ensure you have the version of the code that matches this tutorial. To make sure of this, execute the following commands against your local repo:

git reset --hard 3b7ead6
git push --force origin master

Doing this resets your fork to the particular commit you need for this tutorial.

Editing web.Dockerfile

Vapor Cloud uses Docker to build and run your app, and to handle HTTP requests on port 80. For this to happen, you’ll need to include a web.Dockerfile in the root folder of your project. web.Dockerfile sets up everything your app needs to run in a Docker container, including setting the Swift version and OS for the container. A number of prefabricated files are available at https://github.com/vapor-cloud/docker .

For the Vapor TIL app, you need the Swift version of web.Dockerfile . The root folder of the Vapor TIL app already contains this file. However, you need to make two changes to the template version of the file to work with your app.

Edit your local copy of web.Dockerfile and uncomment the following two lines:

COPY --from=builder /app/Public ./Public
COPY --from=builder /app/Resources ./Resources

This tells the app to load resources from the Public directory, and also sets up your Vapor app to use Leaf templating when deploying to the cloud.

Once you’ve made these changes, push your local version of web.Dockerfile to your GitHub fork. Open web.Dockerfile in your Github repo, to verify that your changes made it there.

With your fork of the app in place, you now need to create an Application on Vapor Cloud for your app.

Creating a New Vapor Cloud Application

Select Applications in the Vapor Cloud menu and click the + button in the upper right:

new_cloud_application-1-648x500.png

New Vapor Cloud Application

Choose a Generated name, to let Vapor Cloud generate an app name and app-slug for your application. Choose Dev Free for the Plan , and choose the only available Region for your server.

Click Create , and the Applications screen will display your new cloud application:

cloud_app-1-650x363.png

Vapor Cloud app with app-slug

In this case, the app-slug is rain-dark-71923 . Store this value in creds.txt , and use this value whenever this tutorial specifies YOUR_APP_SLUG .

Setting Up Your Database

Version 2 of Vapor Cloud provides a free database tier for your cloud apps. The Vapor TIL app uses PostgreSQL for persistence, so you’ll add support for it to your cloud app now.

Click the + button in the upper right to open the New Service menu, and choose Database .

new_service-1-650x294.png

New Service: Database

Then set up your database as follows:

database_setup-1-650x425.png

Set up database

Set Plan to Dev free : This opens the Engine field. Choose PostgreSQL 10.5 for the database engine, set the Region , then click Create .

cloud_app_with_database-1-650x393.png

New database added

You’ll see the new database instance running as part of your cloud app.

Note : This tutorial uses the default production environment for your new cloud application, but the Environment item under the + button menu lets you add more environments that you might need for other purposes, such as staging .

Setting Up Vapor’s Git Server

Next, you need to set up Vapor’s private Git server as a remote for your project. Click the highlighted More button on the Git card, then click Setup Instructions :

rUvuEzm.png!web

Git card menu

You’ll see two variations of the instructions:

qaymAf7.png!web

Use the Existing Project instructions to set up Vapor Cloud as a new remote for your project:

git remote add cloud [email protected]:YOUR_APP_SLUG.git
git push cloud master

The Vapor TIL app uses Google and GitHub for authentication, and also uses SendGrid for sending registration emails. Third-party services like these typically provide security keys to use for your app, and it’s a best practice to not include such keys in your app’s source code, but instead provide them as environment variables on your server instance. So, you need to configure the environment variables in Vapor Cloud for use with your app.

Configuring OAuth Credentials

First, you’ll get a SendGrid API key . In the rest of this section, you’ll see how to determine the environment variables for Google and GitHub authentication. You’ll then configure all these environment variables for your cloud app.

Getting Your SendGrid API Key

In your browser, open SendGrid , sign up for a Free account, then click the link in the verification email.

Click Start for the Integrate using our Web API or SMTP relay option:

sendmail-1-650x247.png

Then Choose Web API :

sendmail-2-650x340.png

On the next page, it doesn’t matter which language you choose. I chose cURL , because, well, cURL.

sendmail-3-650x362.png

Finally, enter a name for your API key, and click Create Key . Store this key in creds.txt .

sendmail-4-650x268.png

Setting Up Google OAuth

To use Google OAuth in your application, you must first register the application with Google. In your browser, open Google APIs Credentials .

If this is the first time you’ve used Google’s credentials, the site will prompt you to create a project:

google1-credentials-create-project-650x400.png

Google Credentials: Create Project

Note : You might have to widen your browser window to see the Create button, as the button hovers on the right edge of the screen.

Click Create to create a project for the TIL application. Fill in the form with an appropriate name like Vapor TIL :

google2-create-project-650x400.png

Click Create , and you’ll return to the Credentials page. Click Create Credentials , and choose OAuth client ID :

google3-credentials-oauth-client-650x400.png

Note : If you’d already created other projects in the past, click Select , and choose the project you just created.

Next, click Configure consent screen to set up the page that Google will present to users so they can authorize access to their details for your application.

Add your product or application name, Vapor TIL , then scroll down to Authorized domains . Enter v2.vapor.cloud , then click Save :

google4-consent-screen-plus-1-650x461.png

Google OAuth Consent Screen

This takes you to the next step: Create OAuth client ID . Choose Web application , and add an Authorized redirect URI for testing your application — http://localhost:8080/oauth/google .

This is the URL that Google redirects back to once users have allowed your application access to their data.

But you also you want to deploy your application to the internet, just as you are doing here with Vapor Cloud 2. Therefore, you’ll need to add another redirect for the URL for that site, using your app-slug: https://YOUR_APP_SLUG.v2.vapor.cloud/oauth/google .

google_oauth-1-597x500.png

Create Google OAuth client ID

Store this URL in creds.txt as GOOGLE_CALLBACK_URL .

Click Create , and the site will provide you with you your client ID and client secret:

google6-oauth-client-id-secret-650x422.png

Store these credentials in creds.txt .

Note : You must keep these credentials safe and secure. Your secret provides access to Google’s APIs, and you absolutely must not share those credentials or keep your secret under source control. You should treat it, in effect, just as you would treat any of your passwords.

Setting Up GitHub OAuth

To use GitHub OAuth in your application, you must first register the application with GitHub. Fortunately, this process will go a little faster than it did when setting things up under Google.

In your browser, go to GitHub Developer settings . Click Register a new application :

github1-register-new-application-650x449.png

Fill in the form with an appropriate name like Vapor TIL . Set the Homepage URL to https://YOUR_APP_SLUG.v2.vapor.cloud for this application, and provide a sensible description. Set the Authorization callback URL to https://YOUR_APP_SLUG.v2.vapor.cloud/oauth/github . This is the URL that GitHub redirects your users to, once they have authorized access to their data.

Store this URL in creds.txt as GITHUB_CALLBACK_URL .

new_github_oath-1-650x480.png

Click Register application . After GitHub creates the application, you’ll be taken back to the application’s information page, where you’ll see the client ID and client secret that you need:

github3-oauth-client-id-secret-650x449.png

Store these credentials in creds.txt

Note : Again, you must keep these keys safe and secure. Your secret allows you access to GitHub’s APIs, and you should not share or check the secret into source control. Treat any API key or secret like a password.

Configuring Vapor Cloud Environment Variables

Having obtained your Google, GitHub, and SendGrid keys, you now need to configure them for your Vapor Cloud application.

Click the highlighted More button on the Application card:

application_more-1-1-650x403.png

Then click Config Vars :

configure_vars-1-650x398.png

Now copy and paste the keys and values from creds.txt , except for the “Vapor Cloud app-slug”, to set up the environment variables for your app. Be careful not to click anywhere outside the Config Vars window until you’ve clicked the Update button, or you’ll have to start over.

configure_vars_values-1-645x500.png

You’ve pushed your application code to the Vapor Cloud private Git server, and you’ve fully configured your app for deployment. It’s time to deploy your app to Vapor Cloud!

Deploying the Vapor Cloud App

To start the deployment, click the Deploy button on the Git card:

git_deploy-1-650x389.png

Use the Default Branch and click Deploy :

deploy_app-1-650x390.png

Be sure to note the comment on the deploy card that “Deployments usually take around 10 minutes to complete. They cannot be canceled.” Time for a celebratory drink or a brisk walk around the block!

While you’re celebrating, your app will build and be deployed to Vapor Cloud:

app_deploying-1-648x500.png

Upon your return, at the bottom of the window, you’ll see:

-----> Released v4
Pushed to Vapor Cloud on YOUR_APP_SLUG.v2.vapor.cloud
Job done: closing logs.
Activity closed, goodbye.
-----> Connection to logs closed

Bringing the App Online

With the app deployed for the first time, you must use one or more replicas to scale the app. You will not need to scale on subsequent deployments.

Click Scale on the web Replica card, then change 0 to 1 , and click Scale :

scale_the_app-1-650x416.png

Once the scaling is done, point your browser at YOUR_APP_SLUG.v2.vapor.cloud .

app_in_cloud-1-631x500.png

You’ve just deployed your first Vapor app to Vapor Cloud 2!

Where to Go From Here?

You’ve seen how easy it is to host your Vapor app on Vapor Cloud 2. You learned how to set up the app using the Vapor Cloud 2 console, configure your Git repository, add the necessary configuration values to your application, and deploy your app.

You can learn much more about deploying to Vapor Cloud 2 in the official docs .

Remember that, at the time of writing this tutorial, Vapor Cloud 2 is still in pre-release, so be sure to follow its development schedule before you start hosting production apps on the service.

You’ll also want to learn about other deployment options for Vapor apps, such as Heroku. You can read about these other options in our book, Server Side Swift with Vapor , that covers everything you need to know to get started developing and deploying Vapor APIs and web apps.

Feel free to add comments or questions in the forum below!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK