如何 git 後直接傳到遠端目標?

发布于 2022-09-06 20:31:05 字数 124 浏览 13 评论 0

我在linode建立了php, apache等等,就是網頁需要用到的環境都建立好了,我個人也有用git版控,傳到bitbucket。現在有個問題,我想讓他同時upload到我linode的apache var/www/html 目錄!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单调的奢华 2022-09-13 20:31:05

方案1, 用post-update勾子

使用下面的post-update文件:
将您的.git目录复制到 web 服务器上
在本地副本中, 修改.git/config 文件并将 web 服务器添加为远程:

[remote "production"]
url = username@webserver:/var/www/html/.git

在服务器上, 用下面的文件替换.git/hooks/post-update

添加对文件的执行访问权限 (再次, 在服务器上):

chmod +x .git/hooks/post-update

现在, 只需在本地推送到您的 web 服务器, 它应该自动更新工作副本:

git push production
#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update". 
git-update-server-info 
is_bare=$(git-config --get --bool core.bare) 
if [ -z "$is_bare" ]
then
      # for compatibility's sake, guess
      git_dir_full=$(cd $GIT_DIR; pwd)
      case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi 
update_wc() {
      ref=$1
      echo "Push to checked out branch $ref" >&2
      if [ ! -f $GIT_DIR/logs/HEAD ]
      then
             echo "E:push to non-bare repository requires a HEAD reflog" >&2
             exit 1
      fi
      if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
      then
             wc_dirty=0
      else
             echo "W:unstaged changes found in working copy" >&2
             wc_dirty=1
             desc="working copy"
      fi
      if git diff-index --cached HEAD@{1} >/dev/null
      then
             index_dirty=0
      else
             echo "W:uncommitted, staged changes found" >&2
             index_dirty=1
             if [ -n "$desc" ]
             then
                   desc="$desc and index"
             else
                   desc="index"
             fi
      fi
      if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
      then
             new=$(git rev-parse HEAD)
             echo "W:stashing dirty $desc - see git-stash(1)" >&2
             ( trap 'echo trapped $; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
             git-update-ref --no-deref HEAD HEAD@{1}
             cd $GIT_WORK_TREE
             git stash save "dirty $desc before update to $new";
             git-symbolic-ref HEAD "$ref"
             )
      fi 
      # eye candy - show the WC updates :)
      echo "Updating working copy" >&2
      (cd $GIT_WORK_TREE
      git-diff-index -R --name-status HEAD >&2
      git-reset --hard HEAD)
} 
if [ "$is_bare" = "false" ]
then
      active_branch=`git-symbolic-ref HEAD`
      export GIT_DIR=$(cd $GIT_DIR; pwd)
      GIT_WORK_TREE=${GIT_WORK_TREE-..}
      for ref
      do
             if [ "$ref" = "$active_branch" ]
             then
                   update_wc $ref
             fi
      done
fi

方案2,用rsync

git ls-files -z | rsync --files-from - --copy-links -av0 . user@server.com:/var/www/project

原文参考:
https://stackoverflow.com/a/3...

染墨丶若流云 2022-09-13 20:31:05

是想搞devops?

可以用 JenKins 来搞,添加一个监控git的任务,再写个upload的脚本就行。
JenKins 任务流程:

  1. push 到 git
  2. JenKins 监测到 git 更新
  3. 服务器 pull git
  4. 触发脚本(复制到 var/www/html 目录)

具体可参考:https://yq.aliyun.com/article...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文