How much can we reduce the time from writing a line of code to deploying it?
As a developer, I like spending my time researching and implementing new functionalities.
Manual tests and deployments consume too much time and energy slowing down the delivery process.
Let’s try to pay the price of automating the deployment up front. We’ll tackle automated tests in another post.
We’ll implement an echo app and host it in the cloud. The goal is to deploy every change automatically.
Tech used
These are my go-to choices for new projects:
Language: Javascript (Node)
Version Control System: Github
Cloud vendor: Heroku
Implementation
Let’s skip the coding. Open a console and run:
$ git clone https://github.com/leancycle/cd.git
The actual code is pretty simple. It requires express for the rest endpoint:
var express = require('express') var app = express() app.get('/echo/:text', function (req, res) { res.send(req.params.text); }) app.listen(process.env.PORT || 3000, function () { console.log('Example app listening on port 3000!') })
Executing the code
Open a console and run
$ npm install
$ node index.js
Example app listening on port 3000!
Open the following URL in your browser:
You’ll see the output: hello
That’s the app so far. Pretty neat :-)
Deployment
Add the app to Github: https://github.com
Got to Heroku and create a new app: https://dashboard.heroku.com
Link the app to the Github repo and enable automatic deploys.
Add a file to the repo named Procfile with the following content:
web: node index.js
It tells Heroku what to run when it starts the application.
Push the changes and Heroku will deploy the application to https://[app name].herokuapp.com/
My app name is leancycle-cd and it’s running at https://leancycle-cd.herokuapp.com/Hello%20World
We now have continuous deployment in place!
Updates
We now have an app running. Let’s take advantage of continuous deployment to push a change.
Instead of just an echo let’s reply with all caps. Adding toUpperCase() to the response string will do the trick.
The code now looks as follows:
app.get('/echo/:text', function (req, res) {
res.send(req.params.text.toUpperCase());
})
We push the change to Github and a few seconds later my app is returning
HELLO WORLD
Conclusions
By implementing continuous deployment, we reduced the time to deploy changes to seconds. We can focus our time and energy into making a better product and improving the deployment process.
You can find the final code in https://github.com/leancycle/cd
The app is running at https://leancycle-cd.herokuapp.com/Hello%20World
Comentarios