2

I typically run the following commands to deploy a particular app:

compass compile -e production --force
git add .
git commit -m "Some message"
git push
git push production master

How can I wrap that up into a single command?

I'd need to be able to customize the commit message. So the command might look something like:

deploy -m "Some message"

7 Answers 7

5

There are two possibilities:

  • a script, as others answered

  • a function, defined in your .bash_profile:

    deploy() {
        compass compile -e production --force &&
        git add . &&
        git commit -m "$@" &&
        git push &&
        git push production master
    }
    

Without arguments, you'd have a third option, namely an alias:

alias deploy="compass compile -e production --force &&
              git add . &&
              git commit -m 'Dumb message' &&
              git push &&
              git push production master"
Sign up to request clarification or add additional context in comments.

5 Comments

If any of the previous commands fail, you really need to abort. Replace all ';' with '&&', and put '&&' at the end of each line but the last in the function definition.
The line continuation character is not required after the '&&'.
How do I pass the argument to this? I'm trying deploy "Some Example" and it's throwing back error: pathspec 'Example' did not match any file(s) known to git.
@Shpigford, that's because the arguments are not properly escaped in the function definition. See my answer for how they should be passed to git commit (in short: use "$@" in the function and use it as `deploy -m "Some Example").
@spatz: I made the $@ change, and reloaded the .bash_profile file, but I'm still getting that same pathspec error.
2

You could create a function that does what you want, and pass the commit message as argument:

function deploy() {
    compass compile -e production --force
    git add .
    git commit "$@"
    git push
    git push production master
}

Put that in your .bashrc and you're good to go.

Comments

1

You can make a shell script. Something that looks like this (note no input validation etc):

#!/bin/sh
compass compile -e production --force
git add .
git commit -m $1
git push
git push production master

Save that to myscript.sh, chmod +x it, then do something like ./myscript.sh "Some message".

Comments

0

You can write a shell script for this

#!/bin/bash
compass compile -e production --force 
git add . 
git commit -m $1 
git push 
git push production master

Save this to 'deploy' and do a chmod 7xx on it. Now you can use it as ./deploy "Some message"

Comments

0

you could write these commands into a file named deploy.sh .

Then make it executable and run as sh deploy.sh

You could even add it to your path by exporting the path where you save the script.

Comments

0

everyone mentions about writing a script and this is probably the best way of doing it.

However you might someday want to use another way - merge commands with &&, for example:

cd ../ && touch abc

will create a file "abc" in a parent directory :)

It is just to let you know about such thing, for this particular scenario (and 99% of the others) please take a look at other answers :)

Comments

0

I would go through the effort of making the command work for more than just the current directory. One of the most versitle ways of doing this is to use getopt in a BASH script. Make sure you have getopt installed, create deploy.sh then chmod 755 deploy.sh and then do something like this:

#!/bin/bash

declare -r GETOPT=/usr/bin/getopt
declare -r ECHO='builtin echo'
declare -r COMPASS=/path/to/compass
declare -r GIT=/path/to/git


sanity() {
    # Sanity check our runtime environment to make sure all needed apps are there.
    for bin in $GETOPT $ECHO $COMPASS $GIT
    do
        if [ ! -x $bin ]
        then
            log error "Cannot find binary $bin"
            return 1
        fi
    done

    return 0
}

usage() {
$CAT <<! 

${SCRIPTNAME}:  Compile, add and commit directories

Usage: ${SCRIPTNAME} -e <env> [-v]
    -p|--path=<path to add>
    -c|--comment="Comment to add"
    -e|--environment=<production|staging|dev>

Example:
    $SCRIPTNAME -p /opt/test/env -c "This is the comment" -e production

!
}



checkopt() {

# Since getopt is used within this function, it must be called as

# checkopt "$@"
    local SHORTOPT="-hp::c::e::"
    local LONGOPT="help,path::,comment::,environment::"

eval set -- "`$GETOPT -u -o $SHORTOPT --long $LONGOPT -n $SCRIPTNAME -- $@`"
    while true
    do
        case "$1" in
        -h|--help)
            return 1
            ;;
        -|--path)
            PATH="$2"
            shift 2
            ;;
        -c|--comment)
            COMMENT=$2
            shift 2
            ;;
        -e|--environment)
            ENV="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            $ECHO "what is $1?"
            ;;
        esac
    done
}


if ! sanity
then
    die "Sanity check failed - Cant find proper system binaries"
fi


if checkopt $@
then
    $ECHO "Running Compass Compile & Git commit sequence..."
    $COMPASS compile -e $ENV --force
    $GIT add $PATH
    $GIT commit -m $COMMENT
    $GIT push
    $GIT push ENV master

else
    usage
    exit 1
fi

exit 0

2 Comments

You should be aware that some versions of getopt have issues.
Its something to keep in mind. That's a good article explaining which versions to stay away from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.