Edit on GitHub
Report issue
Page history

Author(s): @{[{ username }]}  Published: {[{ TutorialCtrl.tutorial.date_published | date:'mediumDate' }]}

At the time of this document's publication, Cloud Run is in beta. Google Cloud Platform products in beta are not covered under any SLAs.

Cloud Run is an easy way to deploy your apps to the same infrastructure that powers Google's products. Using Cloud Build, Cloud Run, and GitHub, you can create a CI/CD pipeline for your app written in Elixir with the Phoenix Framework and have it up and running in minutes.

Note: Due to processes being managed and throttled by Cloud Run, some features (such as websockets, presence, and Cloud Pub/Sub) may not work.

This tutorial will help you to get started deploying a Phoenix app (without Ecto) to Cloud Run using Cloud Build. You will create a new Phoenix application and learn how to configure, build, deploy, and update it.

This tutorial requires Elixir 1.5 and Phoenix 1.4 or later. It assumes that you are already familiar with basic Phoenix web development.

Before you begin

Before running this tutorial, take the following steps:

  1. Use the Google Cloud Platform (GCP) Console to create a new GCP project.

  2. Enable billing for your project.

  3. Install the Cloud SDK.

    Make sure that you initialize the SDK and set the default project to the new project you created.

    Version 248.0.0 or later of the SDK is required. If you have an earlier version installed, you may upgrade it by running the following command:

    gcloud components update
    
  4. Install git for use with Github. Follow the steps to set git up on your local machine.

    If you have not yet installed Elixir and Phoenix, do so.

  5. Install Elixir and Node.js. If you are on macOS with Homebrew, you can run the following command:

    brew install elixir node
    

    Otherwise, see the Node download and Elixir install guides for your operating system.

  6. Install the hex, rebar3, and phx_new archives:

    mix local.hex
    mix local.rebar
    mix archive.install hex phx_new 1.4.5
    

Create a new app and run it locally

In this section, you will create a new Phoenix app without a database and make sure that it runs locally in development. If you already have an app to deploy, you may use it instead.

Create a new Phoenix app

  1. Run the phx.new task to create a new Phoenix project called hello:

    mix phx.new hello --no-ecto
    

    Answer Y when the tool asks you if you want to fetch and install dependencies.

  2. Go into the directory with the new application:

    cd hello
    
  3. Run the app with the following command:

    mix phx.server
    

    This compiles your server and runs it on port 4000.

  4. Visit http://localhost:4000 to see the Phoenix welcome screen running locally on your workstation.

Enable source control with Github

GitHub is a web-based hosting service for version control using Git. It is free and connects to GCP through GitHub Apps.

  1. Create or log in to your GitHub account and install the Google Cloud Build app.

    This app gives your GCP account access to your repository.

  2. Create the new repository. We suggest that you create a private repository, with no Readme file, because Phoenix creates one for you.

  3. Follow the prompts to push your local Phoenix app to your new GitHub repository. The steps are listed here for convenience:

    git init
    git add .
    git commit -m "first commit"
    git remote add origin https://github.com/{USERNAME}/{REPO_NAME}.git
    git push -u origin master
    

You should now see your code in the GitHub repository.

Set up a Cloud Build trigger

Now you set up Cloud Build to build on every code change in your GitHub repository.

  1. Go to the Cloud Build Triggers page in the GCP Console.

  2. Create a new trigger for your Phoenix app, following the on-screen prompts. On the Trigger settings page, set the Build configuration to Cloud Build configuration file; everything else can stay at the default values.

Prepare your Phoenix app

You will now configure the build files for Cloud Build and Cloud Run.

  1. Create your own dockerfile or use this template for Phoenix.

    This will be used by Cloud Build to build your container.

  2. Create the Cloud Build configuration file, which is used by the Cloud Build trigger. You can create your own build configuration file, or use this template for Phoenix.

    Note: If you use the template, replace {GITHUB_USERNAME} and {REPO_NAME} on 4 different lines.

  3. Edit your .gitginore file to allow prod.secrets.exs to be included in the repository.

    Note: This step is only included to get your demo up and running. Do not use in production.

    Open .gitignore in the root folder and comment out the /config/*.secret.exs in the last line. For production-ready secrets management, take a look at Cloud Key Management Service.

  4. Push the code changes to GitHub, using the following commands:

    git add .
    git commit -m "Configuration for Cloud Build and Cloud Run"
    git push -u origin master
    

Live application

Your Phoenix application should now be live.

If you visit the Cloud Build History page, you should see your build either building, successfully built, or possibly failed.

If it has successfully built, you can check the Cloud Run Services page to see if your service has successfully launched. When it has, click the service and follow the URL on the page to view your Phoenix app.

Note: If you encountered an error, view the logs to debug the issue. Issues generally arise from misspelling.

Update your application

Let's make a simple change and redeploy.

  1. Open the front page template lib/hello_web/templates/page/index.html.eex in your editor.

  2. Make a change to the HTML template.

  3. Push the changes to GitHub:

    git add .
    git commit -m "Homepage updates"
    git push -u origin master
    

    Cloud Build will take care of rebuilding your app and trigger a deployment of the updated version on successful compilation. Then Cloud Run will migrate traffic to the newly deployed version.

  4. View your changes live by visiting the URL on the Cloud Run services page.

Cleaning up

After you've finished this tutorial, you can clean up the resources you created on GCP so you won't be billed for them in the future. To clean up the resources, you can delete the project or stop the individual services.

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial. To do so using gcloud, run the following command:

gcloud projects delete [YOUR_PROJECT_ID]

where [YOUR_PROJECT_ID] is your GCP project ID.

Warning: Deleting a project has the following consequences: If you used an existing project, you'll also delete any other work you've done in the project. You can't reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.

Deleting individual services

In this tutorial, you created a Cloud Build instance and deployed a Cloud Run service. Here is how to stop these two services.

  • To delete the Cloud Build instance, visit the Triggers page and delete the trigger. This will prevent Cloud Build from running automatically on code pushes.

  • To delete the Cloud Run instance, visit the Cloud Run services page, highlight the services, and select delete. This will stop the current service.

Next steps

The Elixir Samples repository contains a growing set of sample Elixir applications ready to deploy to Google Cloud and examples of communicating with Google APIs from Elixir.

See the Cloud Build documentation for more information on Cloud Build features including dockerfiles, build configurations, and build automation.

See the Cloud Run documentation for more information on Cloud Run features including services, health checks, and traffic migration.

You can also try the tutorials on deploying Phoenix applications to Kubernetes Engine, to Compute Engine, and to App Engine Flex.

See more by @{[{ username }]} and more tagged {[{ tag }]}{[{ $last ? '' : ', ' }]}

Submit a Tutorial

Share step-by-step guides

SUBMIT A TUTORIAL

Request a Tutorial

Ask for community help

SUBMIT A REQUEST

GCP Tutorials

Tutorials published by GCP

VIEW TUTORIALS

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.