如何制作一个脚本来执行git pull?

时间:2015-10-09 12:53:44

标签: linux git bash shell

dev / 暂存 / 生产服务器上执行git pull是一种常见做法。我经常这样做;我在运行linux的生产服务器上每天执行git pull几乎100次。

我想,现在是时候制作一个脚本来改善它。

pull.sh 会执行这3个命令

  • git pull
  • 输入我的密码(提示时)
  • service nginx reload

我已尝试在此处创建 pull.sh

#!/bin/bash

function pull {
  git pull
  password
  service nginx reload
}


pull ;

结果

运行我的脚本后,我仍然提示输入密码。

enter image description here

任何提示/帮助/建议都将不胜感激!

2 个答案:

答案 0 :(得分:6)

您可以使用#!/usr/bin/expect -f spawn git pull expect "ass" send "your_password\r" interact 脚本与git身份验证进行交互:

# Call script directly since the shell knows that it should run it with
# expect tool because of the first script line "#!/usr/bin/expect -f"
./git-pull-helper-script.sh
# Without the first line "#!/usr/bin/expect -f" the file with commands
# may be sent explicitly to 'expect':
expect file-with-commands

# Restart server
service nginx reload

等待" ass"文本(匹配"密码","密码","密码")然后它会发送您的密码。

可以从另一个重启服务器的bash脚本调用该脚本:

{{1}}

答案 1 :(得分:2)

处理密码短语的方法是使用ssh代理:这样,您只需输入一次密码短语。

我在我的开发用户~/.bash_profile

中有这个
# launch an ssh agent at login, so git commands don't need to prompt for password
# ref: http://stackoverflow.com/a/18915067/7552

SSH_ENV=$HOME/.ssh/env

if [[ -f ~/.ssh/id_rsa ]]; then
    function start_agent {
        # Initialising new SSH agent...
        ssh-agent | sed 's/^echo/#&/' > "${SSH_ENV}"
        chmod 600 "${SSH_ENV}"
        source "${SSH_ENV}" > /dev/null
        ssh-add
    }

    # Source SSH settings, if applicable

    if [ -f "${SSH_ENV}" ]; then
        source "${SSH_ENV}" > /dev/null
        agent_pid=$(pgrep ssh-agent)
        (( ${agent_pid:-0} == $SSH_AGENT_PID )) || start_agent
        unset agent_pid
    else
        start_agent
    fi
fi