git_push.sh raw

   1  #!/bin/sh
   2  # ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
   3  #
   4  # Usage example: /bin/sh ./git_push.sh wing328 openapi-petstore-perl "minor update" "gitlab.com"
   5  
   6  git_user_id=$1
   7  git_repo_id=$2
   8  release_note=$3
   9  git_host=$4
  10  
  11  if [ "$git_host" = "" ]; then
  12      git_host="github.com"
  13      echo "[INFO] No command line input provided. Set \$git_host to $git_host"
  14  fi
  15  
  16  if [ "$git_user_id" = "" ]; then
  17      git_user_id="GIT_USER_ID"
  18      echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id"
  19  fi
  20  
  21  if [ "$git_repo_id" = "" ]; then
  22      git_repo_id="GIT_REPO_ID"
  23      echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id"
  24  fi
  25  
  26  if [ "$release_note" = "" ]; then
  27      release_note="Minor update"
  28      echo "[INFO] No command line input provided. Set \$release_note to $release_note"
  29  fi
  30  
  31  # Initialize the local directory as a Git repository
  32  git init
  33  
  34  # Adds the files in the local repository and stages them for commit.
  35  git add .
  36  
  37  # Commits the tracked changes and prepares them to be pushed to a remote repository.
  38  git commit -m "$release_note"
  39  
  40  # Sets the new remote
  41  git_remote=$(git remote)
  42  if [ "$git_remote" = "" ]; then # git remote not defined
  43  
  44      if [ "$GIT_TOKEN" = "" ]; then
  45          echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
  46          git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
  47      else
  48          git remote add origin https://${git_user_id}:"${GIT_TOKEN}"@${git_host}/${git_user_id}/${git_repo_id}.git
  49      fi
  50  
  51  fi
  52  
  53  git pull origin master
  54  
  55  # Pushes (Forces) the changes in the local repository up to the remote repository
  56  echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
  57  git push origin master 2>&1 | grep -v 'To https'
  58