Juicy lil' bytes

Automated Ruby Gem Build and Deployment to Rubygems.org

Drop this shell script in your Ruby project and it will build and deploy your updated gem release when you run it. It allows you to either hard code the gem name in the script or pass the name in as an argument to the script.

gemify.sh

Install in Your Project

Here is the full script which is available on my GitHub repository:

gemify.sh - automated gem build and deploymentraw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
# Scriptacular - gemify.sh
# Create a Ruby gem and push it to rubygems.org
# Copyright 2013 Christopher Simpkins
# MIT License

# Enter your gem name below (do not enter version number)
# or pass it as the first argument to the shell script
GEM_NAME=""

# Don't touch these
GEMSPEC_SUFFIX=".gemspec"
GEM_BUILD_CMD="gem build"
GEM_PUSH_CMD="gem push"

# if there is an argument to the script, define the gem name with it
if [ $# -eq 1 ]; then
  GEM_NAME=$1
elif [ $# -gt 1 ]; then
  echo "You entered too many arguments.  Please specify the name of your gem as the argument to the script." >&2
  exit 1
fi

# if the gem name has not been defined, print error message and exit
if [ -z "$GEM_NAME" ]; then
  echo "You did not enter a gem name.  Please include it as an argument to the script or hard code it as a variable in the script." >&2
  exit 1
fi

# run the gem build and parse for the gem release filename
GEM_BUILD_NAME=$(gem build "$GEM_NAME$GEMSPEC_SUFFIX" |  awk '/File/ {print $2}' -)

# if the build failed (i.e. no file name obtained above), print error message and exit
if [ -z "$GEM_BUILD_NAME" ]; then
  echo "The gem build failed.  Please confirm the gem name and try again." >&2
  exit 1
fi

# if above succeeded, then push to rubygems.org using the gem that was compiled
gem push $GEM_BUILD_NAME
exit 0

Place the above file in the directory that contains your project gemspec file. To simplify re-use on the same project, you can hard code the gem name by defining the GEM_NAME variable. Alternatively, you can pass the gem name as the first argument to the script each time that you use it. It is not necessary to include version information or the .gemspec file suffix. The script takes care of these details.

Permissions

Run chmod 744 on the script in order to make it an executable file.

Use It

When you are ready to build and deploy your Ruby gem, you run the script from the command line with or without the gem name as an argument to the script depending upon how you chose to install it above. Here are a couple of examples for a gem named “bogus”.

Run with Hard Coded Gem Name

Change the GEM_NAME shell variable in the file like this:

1
GEM_NAME="bogus"

Save the file in the directory that contains your project gemspec file. Then, make that directory your working directory and run the following command:

1
./gemify.sh

The script will run gem build bogus.gemspec and (assuming that this is version 0.1.0 of your gem), gem push bogus-0.1.0.gem. The new release version number of the gem is automatically captured with each build and populated in this push command.

Run with Gem Name as an Argument

If you would prefer to avoid a hard coded gem name in the file, then you can pass the name of the gem as an argument to the script like this:

1
./gemify.sh bogus

The script will run just as it did with the hard coded name.

Use with Multiple Gem Projects

If you have a lot of Ruby gem projects in active development, you could toss this in an external script directory and create an alias for it in your .bashrc or .bash_profile file. Run a source command on the bash settings file and then use the script alias from any working directory that includes a gemspec file. Include the gem name as the argument to the alias.