www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

auto-push.sh (4403B)


      1 #!/bin/sh
      2 set -e
      3 set +x # do not display any command, as they could contain the Travis openssl key and IV.
      4 
      5 usage() {
      6   echo "Usage: $0 official_repo deploy_repo deploy_branch deploy_base_commit key_iv_id deploy_directory"
      7   echo "official_repo:       https://github.com/user/repo.git"
      8   echo "deploy_repo:         git@github.com:user/repo.git"
      9   echo "deploy_branch:       gh-pages"
     10   echo "deploy_base_commit:  branch name or tag"
     11   echo "key_iv_id:           123456789abc, part of encrypted_123456789abc_key and encrypted_123456789abc_iv"
     12   echo "deploy_directory:    directory to copy on top of deploy_base_commit"
     13   echo "from_branch:         master # allow push only when building the given branch"
     14 }
     15 
     16 if test "$#" -eq 1 && test "$1" = "-h" -o "$1" = "--help"; then
     17   usage
     18   exit 0
     19 elif test "$#" -ne 7; then
     20   usage
     21   exit 1
     22 fi
     23 
     24 official_repo="$1"      # https://github.com/user/repo.git
     25 deploy_repo="$2"        # git@github.com:user/repo.git
     26 deploy_branch="$3"      # gh-pages
     27 deploy_base_commit="$4" # branch name or tag
     28 key_iv_id="$5"          # 123456789abc, part of encrypted_123456789abc_key and encrypted_123456789abc_iv
     29 deploy_directory="$6"   # directory to copy on top of deploy_base_commit
     30 from_branch="$7"        # master # allow push only when building the given branch
     31 key_env_var_name="encrypted_${key_iv_id}_key"
     32 iv_env_var_name="encrypted_${key_iv_id}_iv"
     33 key="$(sh -c 'echo "${'"$key_env_var_name"'}"')"
     34 iv="$(sh -c 'echo "${'"$iv_env_var_name"'}"')"
     35 
     36 if test "$(git config remote.origin.url)" != "$official_repo"; then
     37   echo "Not on official repo, will not deploy to ${deploy_repo}:${deploy_branch}."
     38 elif test "$TRAVIS_PULL_REQUEST" != "false"; then
     39   echo "This is a Pull Request, will not deploy to ${deploy_repo}:${deploy_branch}."
     40 elif test "$TRAVIS_BRANCH" != "$from_branch"; then
     41   echo "Not on $from_branch branch (TRAVIS_BRANCH = $TRAVIS_BRANCH), will not deploy to ${deploy_repo}:${deploy_branch}."
     42 elif test -z "${key:-}" -o -z "${iv:-}"; then
     43   echo "Travis CI secure environment variables are unavailable, will not deploy to ${deploy_repo}:${deploy_branch}."
     44 elif test ! -e travis-deploy-key-id_rsa.enc; then
     45   echo "travis-deploy-key-id_rsa.enc not present, will not deploy to ${deploy_repo}:${deploy_branch}."
     46 else
     47   echo "Automatic push to ${deploy_repo}:${deploy_branch}"
     48 
     49   # Git configuration:
     50   git config --global user.name "$(git log --format="%aN" HEAD -1) (Travis CI automatic commit)"
     51   git config --global user.email "$(git log --format="%aE" HEAD -1)"
     52 
     53   # SSH configuration
     54   mkdir -p ~/.ssh
     55   chmod 700 ~/.ssh
     56   if openssl aes-256-cbc -K "$key" -iv "$iv" -in travis-deploy-key-id_rsa.enc -out travis-deploy-key-id_rsa -d >/dev/null 2>&1; then
     57     echo "Decrypted key successfully."
     58   else
     59     echo "Error while decrypting key."
     60     exit 1
     61   fi
     62   # TODO: all the config should be in a separate folder, instead of using ~/.ssh for the id_rsa.
     63   mv travis-deploy-key-id_rsa ~/.ssh/travis-deploy-key-id_rsa
     64   chmod 600 ~/.ssh/travis-deploy-key-id_rsa
     65   eval `ssh-agent -s`
     66   ssh-add ~/.ssh/travis-deploy-key-id_rsa
     67 
     68   travis_known_hosts="$("$(dirname "$0")/absolute-path.sh" "$(dirname "$0")/travis_known_hosts")"
     69   chmod 600 "$travis_known_hosts"
     70   echo "$travis_known_hosts"
     71   cat "$travis_known_hosts"
     72   ssh -o UserKnownHostsFile=$travis_known_hosts git@github.com || true
     73 
     74   TRAVIS_AUTO_PUSH_REPO_DIR="$HOME/travis-temp-auto-push-$(date +%s)"
     75   if test -e "$TRAVIS_AUTO_PUSH_REPO_DIR"; then rm -rf "$TRAVIS_AUTO_PUSH_REPO_DIR"; fi
     76   GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=$travis_known_hosts" git clone -b "$deploy_base_commit" --depth 1 --shallow-submodules "$deploy_repo" "$TRAVIS_AUTO_PUSH_REPO_DIR"
     77   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git checkout -b "$deploy_branch")
     78   rsync -a "${deploy_directory}/" "${TRAVIS_AUTO_PUSH_REPO_DIR}/"
     79   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git add -A . && git commit --allow-empty -m "Auto-publish to $deploy_branch") > commit.log || (cat commit.log && exit 1)
     80   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && git log --oneline --decorate --graph -10)
     81   echo '(cd '"$TRAVIS_AUTO_PUSH_REPO_DIR"' && git push --force --quiet "'"$deploy_repo"'" "'"$deploy_branch"'")'
     82   (cd "$TRAVIS_AUTO_PUSH_REPO_DIR" && GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=$travis_known_hosts" git push --force --quiet "$deploy_repo" "$deploy_branch" >/dev/null 2>&1) >/dev/null 2>&1 # redirect to /dev/null to avoid showing credentials.
     83 fi