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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33038832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:42:03  来源:igfitidea点击:

How to make a script to perform git pull?

linuxgitbashshell

提问by cyber8200

It is a common practice to perform git pullon dev/staging/productionserver(s). I frequently do so myself; I perform git pull almost 100 times a day on my production server running linux.

git pull开发/登台/生产服务器上执行是一种常见的做法。我自己也经常这样做;我每天在运行 linux 的生产服务器上执行 git pull 几乎 100 次。

I figure, it's time to make a scriptto improve that.

我想,是时候制作一个脚本来改进它了。



pull.shwill do these 3 commands

pull.sh将执行这 3 个命令

  • git pull
  • enter my-password (when prompt)
  • service nginx reload
  • 输入我的密码(出现提示时)
  • 服务 nginx 重新加载


I've tried create my pull.shhere

我试过在这里创建我的pull.sh

#!/bin/bash

function pull {
  git pull
  password
  service nginx reload
}


pull ;


Result

结果

After running the script that I have, I still got prompt to input the password.

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

enter image description here

在此处输入图片说明



Any hints / helps / suggestions will be much appreciated !

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

回答by Orest Hera

You can use expectscript to interact with git authentication:

您可以使用expect脚本与 git 身份验证交互:

#!/usr/bin/expect -f
spawn git pull
expect "ass"
send "your_password\r"
interact

It waits for "ass" text (that matches "Password", "password", "passphrase") and then it sends your password.

它等待“ass”文本(匹配“Password”、“password”、“passphrase”),然后发送您的密码。

That script can be called from another bash script that will restart your server:

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

# 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

回答by glenn Hymanman

The way to handle a passphrase is to use an ssh agent: that way, you only need to type in your passphrase once.

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

I have this in my dev user's ~/.bash_profile

我的开发用户中有这个 ~/.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