React Gin Blog (11/19): Deploy and stop scripts

Now that we moved our configuration to ENV, we need to source .env every time we restart our machine or shell session, which can become a bit tedious. To make our lives easier, we will create bash scripts for starting and stopping server. While at it, we will also separate deploy for dev and prod environments. Let’s start by creating new directory internal/cli/ and file cli.go inside of it with following content:

package cli

import (

func usage() {
  fmt.Print(`This program runs RGB backend server.


rgb [arguments]

Supported arguments:


func Parse() {
  flag.Usage = usage
  env := flag.String("env", "dev", `Sets run environment. Possible values are "dev" and "prod"`)

This code is pretty straightforward. We set usage text that will be printed if app is started with some invalid options or arguments, after that we set default value and description for -env option. Finally, parse all CLI flags with flag.Parse(). For the moment we don’t do anything smart with that value, we are only printing it. But that will change latter when we add logging. For now let’s use this by updating cmd/rgb/main.go file:

package main

import (

func main() {

Everything is now ready to write bash scripts that will be used for deploying and stopping our app. I will not go into details about bash commands and syntax but i will explain logic behind it. Lets first create another directory inside of our project root and name it simply scripts/. Inside of it we will add our first script,

#! /bin/bash

# default ENV is dev

while test $# -gt 0; do
  case "$1" in
      if test $# -gt 0; then
      # shift

cd ../../rgb
source .env
go build -o cmd/rgb/rgb cmd/rgb/main.go
cmd/rgb/rgb -env $env &

In the script above we first set env=dev which means that dev environment will be selected by default. After that we are reading arguments passed to this script, and if we found -env argument, we will set env variable to passed value of that argument. This is done inside of while loop. When env variable is set, change directory to project root, source ENV variables and then run go build -o cmd/rgb/rgb cmd/rgb/main.go. This will create executable file named cmd/rgb/rgb which we can run to start our server. When app is built, we are starting server with cmd/rgb/rgb -env $env & which passes value of env variable as -env flag to our server and then starts it in background because of $ flag.

Let’s now create very simple script, also inside of scripts/ directory:

#! /bin/bash

kill $(pidof rgb)

This script will find process ID of our rgb app and then kills that process.

Before running both scripts, you will need to make them executable:

chmod +x
chmod +x

Now we can start and stop our server by simply positioning inside of scripts/ directory and running:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: