繁体   English   中英

用travis自动推送到github仓库

[英]automated push to a github repo with travis

我有一个托管我的网页的gitub.io存储库-该网页的源代码(未编译的Jade / Sass代码)位于单独的公共存储库中。 Travis-CI的设置是为了监视我的源存储库中的更改并运行编译套件,生成将被推送到github.io存储库中的HTML / CSS。

如果编译通过,是否可以将Travis设置为自动推送到我拥有的github存储库,而无需将我的用户名和密码硬编码到我的.travis.yml文件中(显然,这是.travis.yml安全考虑)?

我已经看到了这个问题 ,但是考虑到Travis并没有得到回答-我认为我不能使用密钥对身份验证,因为我需要将私钥放在存储库或travis脚本中,这是就像输入密码一样有很大的安全漏洞。


对于到这里来的其他人,我使用roidrage的答案作为跳板发现了以下信息:

  1. Travis使用公用/专用密钥加密来允许您将敏感信息嵌入.travis.yml文件中。 您可以安装他们的名为“ travis”的gem并将其用于加密内容,然后他们会在其末端安全地对其解密。 文档: http : //docs.travis-ci.com/user/encryption-keys/

  2. 在github上,您可以在应用程序设置中生成“个人访问令牌”。 应用程序可以像密码一样使用它。 使用以上技术对其进行加密,然后将其放入Yaml中。

这可以通过在.travis.yml文件中存储以加密方式访问GitHub的令牌来实现。 有关如何加密数据的示例,请参见我们的文档

至于推送到GitHub Pages的问题,有一篇博客文章对这些步骤进行了很好的总结,甚至指向可以在构建中使用的脚本

脚本的镜像在这里:

#!/usr/bin/env bash

# This script was written to facilitate the deployment process of Pelican
# websites using Travis CI. See this blog post for more information:
# http://kevinyap.ca/2014/06/deploying-pelican-sites-using-travis-ci/

usage="Usage: $(basename "$0") (deploy | diff | serve)

Commands:
  deploy     Upload site to Github Pages
  diff       Compare locally generated site to live site
  serve      Generate and serve site (auto-reloads on changes)"

TARGET_REPO="iKevinY/iKevinY.github.io"
GH_PAGES_BRANCH="master"

DEVELOP_CONF="pelicanconf.py"
PUBLISH_CONF="publishconf.py"

OUTPUT_DIR="output"
REMOTE_DIR="remote"

PY_CMD="python3"
SERVER="http.server"
PORT="8000"

rootPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

generate_site() {
  # Based on http://zonca.github.io/2013/09/automatically-build-pelican-and-publish-to-github-pages.html
  if [ "$TRAVIS" == "true" ]; then
    # Ensure that builds triggered by pull requests are not deployed
    if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
      echo "Successfully built pull request #$TRAVIS_PULL_REQUEST."
      exit 0
    fi

    echo "Deploying site to $GH_PAGES_BRANCH branch of $TARGET_REPO."
    git config --global user.email "travis@travis-ci.org"
    git config --global user.name "Travis CI"
  else
    cd "$rootPath" || exit 1
    pelican -s $PUBLISH_CONF
  fi

  # Pull hash and commit message of the most recent commit
  commitHash=$(git rev-parse HEAD)
  commitMessage=$(git log -1 --pretty=%B)

  # Clone the GitHub Pages branch and rsync it with the newly generated files
  GITHUB_REPO=https://${GH_TOKEN:-git}@github.com/${TARGET_REPO}.git
  git clone --branch $GH_PAGES_BRANCH --depth 1 "$GITHUB_REPO" $REMOTE_DIR &> /dev/null
  rsync -r --exclude=.git --delete $OUTPUT_DIR/ $REMOTE_DIR/
  pushd $REMOTE_DIR > /dev/null

  git add -A
  git status -s

  $1  # execute the function that was passed as an argument
}

push_changes() {
  if [ "$TRAVIS" == "true" ]; then
    longMessage="Generated by $commitHash; pushed by build #$TRAVIS_BUILD_NUMBER."
    git commit -m "$commitMessage" -m "$longMessage"
    git push origin $GH_PAGES_BRANCH &> /dev/null || echo "Push failed."
  else
    read -rp "Push changes to GitHub Pages? [y/N] " response
    if [[ "$response" =~ ^[Yy]$ ]]; then
      git commit -m "$commitMessage" -m "Generated by $commitHash."
      git push origin $GH_PAGES_BRANCH
    fi

    popd > /dev/null
    rm -rf -- $REMOTE_DIR $OUTPUT_DIR && echo "Removed $REMOTE_DIR and $OUTPUT_DIR."
  fi
}

case "$1" in
  'deploy')
    generate_site push_changes
    ;;

  'diff')
    generate_site 'git --no-pager diff --cached --color-words'
    ;;

  'serve')
    developPath=${rootPath}/develop
    local_ip=$(ifconfig | grep 'inet ' | awk 'NR==2 {print $2}')

    # Seed directory with site content
    cd "$rootPath" && pelican -s $DEVELOP_CONF > /dev/null
    echo "Serving HTTP at $(tput bold)${local_ip}:${PORT}$(tput sgr0)."

    cleanup() {
      pkill -f $SERVER
      cd "$rootPath" && rm -r "$developPath" && echo && exit 0
    }

    trap cleanup SIGINT

    (pelican -rs $DEVELOP_CONF 2> /dev/null) &
    (cd "$developPath" || exit 1; $PY_CMD -m $SERVER $PORT 1> /dev/null) &
    wait
    ;;

  *)
    echo "$usage"
    exit 2
    ;;

esac

Mac OS El Capitan需要Ruby ^ 2.2

brew unlink ruby; brew install Ruby
gem install travis  

使用travis gem加密您的秘密PAT并更新您的.travis.yml

travis encrypt  GH_TOKEN=<secret github personal access token> --add

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM