DEV Community

Alexander Martinez
Alexander Martinez

Posted on

Automated Deployments from Scratch

I'm going to share a simple method for automating your deployments from scratch.

While working on a project that uses Go and React, I realized that there were too many steps in the deployment process to do it manually. I needed to generate assets for transfer using three different programs, then I needed to send it over to my production host, and then I needed to move those assets into place in production. I had read about using git hooks to do this kind of thing before, so I looked into them. It turns out that git hooks are easy to use. You just put a script into the .git/hooks/ folder and name it after the event that you want it to be executed during.

I used the pre-commit hook, and wrote a bash script for compiling all of the assets, compressing them, and scp'ing them to my production server.

Now doing git commit -a causes a compile, packaging, and transfer of the compressed assets to the production server. What is left though, is to have the production server automatically install the new assets. As a solution, I wrote a watcher script that polls for changes in a file.

The watcher script runs on the production host and looks for changes in the file that the pre-commit hook pushes to it. When the file changes, it unpacks the archive, stops the app, installs the assets, removes leftover artifacts and starts the app.

That's it. Automated deployments with git and a couple of bash scripts. One script in your development environment that's kicked off by git and another on your production host that's triggered on a file change.

Top comments (0)