Skip to content

Deploy Ruby App to Azure Web Apps

Hello friends.

I was trying to deploy a Ruby application to Azure Web Apps now that it supports running Ruby (2.3) application stack natively on Linux with the Azure Web App on Linux (In Preview at the time of writing this post).  And I thought I’d share the steps I took and the issues I stumbled on during.

Although you were previously able to run Ruby on Rails Web applications on an Azure VM, running a Ruby Application on Web Apps brings a lot of PaaS benefits .

I have a local machine operated by Windows 10 (creators update) so to get started I needed to run the Ruby on rails application locally before deploying it to an Azure Web App. As a prerequisite, make sure Ruby 2.3.3 or higher is installed on your development machine along with Git as I will use it to deploy to Azure. RubyInstaller is a great project to help you install Ruby and it gives you everything you need to set up a full Ruby development environment on Windows.

To start, I created a new basic ruby on rails application locally with the following commands

//create a directory for the new app to work with and navigate to that directory. As you can see in the below screenshot first I navigated to another directory where I wanted to create my application


mkdir test
cd test

Next, we need to Initialize Ruby and check the version using the following command

ruby -v

and then install rails using the gem install rails command.

Here is where I faced the first issue as you can see in the screenshot below, I had an error “Error installing rails, native gem requires build tools”, so I went and downloaded the Ruby Development Kit to resolve this.

snip_20170718101321

Next, I created a rails application called hello-world using the following command:

rails new hello-world --skip-yarn

hello-world create rails

Afterward, navigate to the newly created to the hello-world directory, and start the server with the following command:

cd hello-world
rails server

Now you can browse the newly created rails web application locally on your browser by navigating to http://localhost:3000.

But here is where I faced the 2nd issue as when I browsed the application I got the error:

AbstractController::Helpers::MissingHelperError (Missing helper file helpers/d:/ code/test/hello_world/app/helpers/application_helper.rb_helper.rb):

The commands I’ve been passing were on Git Bash, so I tried starting the rails server on a Windows command prompt and it worked.

But when I wanted to start the rails server from a Windows command prompt it mentioned that “A server is already running. Check d:/Code/test/hello-world/tmp/pids/server.pid.

So I went to that server.pid file and deleted it and then ran the command rails server again and this time it launched successfully.

rails server error

Now that the app is running locally, I needed to make an extra step before proceeding to deploy it to Azure. We need to configure a default route before we deploy to Azure otherwise we will receive a 404 error when browsing the site. Since this is a basic “Hello World” rails app, there’s no Route in routes.rb. For now, we’ll to add the default route in the routes.rb file found in the config folder. Just add the following line there

root 'application#hello'

The above line routes the app to the application controller and the action hello. This is because when controllers are generated in Rails they are empty by default unless you tell it your desired actions during the generation process.

To manually define an action inside a controller, all you need to do is to define a new method inside the controller. So navigate and open the application controller file in app/controllers/application_controller.rb and inside the ApplicationController class, define the hello method so that your controller now looks like this:

class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
           def hello
               render html: “Hello again world, from Azure Web Apps!”
           end
end

So when you refresh the local URL <http://localhost:3000>, the rails application will display a plain HTML page that has the above line “Hello again world, from Azure Web Apps”

Now we can begin the deployment stage. First, we need to create a Ruby web app on Azure which is straightforward and easy.

On your Azure portal navigate to Add a Web App on Linux and complete the actions in the Create Blades as the following screenshots show:Create Web App on Linux

The Create blade

  • Give the web app a unique name.
  • Choose an existing resource group or create a new one
  • Choose an existing Azure App Service plan or create a new one
  • Choose the Ruby 2.3 built-in runtime stack for your container configuration (only one available at the time of writing
  • Hit OK

In no time, the web app is created, and you can see the Overview blade displayed. Feel free to browse the URL of the web it should take you to the default Hosting Start page

Next, the deployment.
I used Git to deploy the local Rails application to Azure. Go to Deployment options in the Azure Web App menu under Deployment, choose source and select Local Git Repository. This will set up deployment source for your web app and populate the Git clone url value in the overview blade as the below screenshots show

Untitled

git clone url

Now that we have the Git deployment URL, mine was <https://mygituser@rubytest-2.scm.azurewebsites.net:443/rubytest-2.git>, we will need to get the deployment credentials (basically an ftp user) which will be used to authenticate against when the repo is pushed to Azure, so if you don’t have these yet create one. On the portal, when on the web app blade, just navigate to Deployment Credentials under Deployment from the left menu and set the username and password.

Finally, we can run the followings git commands to publish the local application to the Azure web app (Assuming you are in the root directory of your local app, hello-world in our case):

//if git is not initialized yet, run the git init command first
git remote add azureweb https://mygituser@rubytest-2.scm.azurewebsites.net:443/rubytest-2.git
git add -A
git commit -m "initial upload ruby on rails"
git push azureweb master

In the push command, I used “azureweb” as I created a reference named ‘azureweb’ for the web app’s repository in the first command. this should prompt the

After running these commands, the Git Credential Manager for Windows should prompt you for the username and password that are in your ftp settings within the deployment credentials.

git deploy authenticate

And that’s it. At the end, you should get from remote that the operation “Finished successfully” and that the “Deployment successful” To https://rubytest-2.scm.azurewebsites.net:443/rubytest-2.git.

Browse to the URL of your web app and you should see that Ruby on Rails application is now running on Azure.

ruby running on azure

Happy Coding!

PS: I am not a Ruby expert, I was trying it out to check the new Web Apps on Linux feature so please feel free to share your feedback if I might have had any inaccurate info above.

 

Published inMicrosoft Azure

Be First to Comment

Leave a Reply

Skip to toolbar