Search code examples
gitbashamazon-web-servicesamazon-elastic-beanstalkexit-code

Bash script exit status to account for a git reset


I have a script for deploying AWS beanstalk applications. Early parts of the script modify a handful of files based on the environment I'm deploying:

if [[ $enviro == qa ]] || [[ $enviro == staging ]]; then
  sed -i '' 's/'$app'.config/'$app'-'$enviro'.config/g' .ebextensions/0006_file.config
  git add .ebextensions/0006_file.config && git commit -m " for deploy only - will be (soft) reset "
fi

The last bit of my script reverts that with:

if [[ $enviro == qa ]] || [[ $enviro == staging ]]; then
  git reset --soft HEAD~1
  git reset HEAD .ebextensions/0006_file.config
  git checkout .ebextensions/0006_file.config
fi

However in between those two blocks I have the actual deploy line:

eb deploy $app-$enviro --label $current_date-$current_user --timeout 30 -v

What I'd like to do is write an if/then statement that exits the script if the 'eb deploy' block fails with exit code=1 but also does the git reset. If the script 'eb deploy' block succeeds I:

echo "Congratulations, you've deployed $branch to $enviro"

I can't tell but I may be overthinking this. I'm open to suggestions. My guess is that my second block will have to have another [[ ]] that sets the rc=$?

Thoughts?


Solution

  • 'Trap' put me down the right road but I needed to add a function to complete the process.

    function clean_up {
      if [[ $enviro == qa ]] || [[ $enviro == staging ]] || [[ $enviro == staging2 ]]; then
        git reset --soft HEAD~1
        git reset HEAD .ebextensions/0006_file.config
        git checkout .ebextensions/0006_file.config
      fi
    }
    fi
    trap clean_up EXIT