2014-09-24 131 views
5

我有一個gitub.io回購託管我的網頁 - 該網頁的來源(未編譯的Jade/Sass代碼)是在一個單獨的公共回購。 Travis-CI被設置爲觀察我的源代碼更改並運行編譯套件,生成將推送到github.io回購的HTML/CSS。自動推送到github回購與travis

我可以設置Travis自動執行推送到github回購我自己如果編譯通過,沒有硬編碼我的用戶名和密碼到我的.travis.yml文件(顯然這是一個安全問題)?

我見過this question,但它沒有回答Travis的想法 - 我不認爲我可以使用密鑰對認證,因爲我需要將私鑰放入回購站或Travis腳本中,這與輸入我的密碼一樣大。


對於其他人誰在這裏捲起,我發現下面的信息使用roidrage的回答爲跳板:

  1. 特拉維斯使用公鑰/私鑰加密,讓你嵌入機密信息.travis.yml文件。你可以安裝他們的名爲「travis」的gem並用它來加密東西,並且他們會在它們的最後安全地解密它。文檔:http://docs.travis-ci.com/user/encryption-keys/

  2. 在github上,您可以在您的applications settings中生成「個人訪問令牌」。這可以像應用程序使用密碼一樣使用。使用上述技術對其進行加密並將其放入yaml中。

+0

特拉維斯要求一個變量('SOMEVAR')。我應該使用哪個變量? – koppor 2016-02-03 20:20:35

+1

@ koppor我不知道。如果有幫助,這裏是一個回購,我使用travis將降價文件編譯爲pdf,然後將編譯後的pdf回推到回購:https://github.com/ahemmeter/ahemmeter.github.io – 2016-02-04 20:06:29

+1

在我的情況下,我使用travis gem加密了一個ssh私鑰(部署密鑰)。我將加密的私鑰存儲在回購站中。特拉維斯下載回購協議,解密私鑰,然後使用解密的密鑰推回到回購協議。 – 2016-02-04 20:09:58

回答

4

這可以通過在.travis.yml文件中以加密方式存儲令牌來訪問GitHub來實現。有關如何加密數據的示例,請參閱我們的docs

至於推到GitHub頁面,有一個blog post總結步驟相當不錯,它甚至指向a script,您可以在您的構建中使用。

腳本的鏡像是在這裏:

#!/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 "[email protected]" 
    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 
+0

腳本是404。 – koppor 2016-02-03 20:20:35

0

的Mac OS埃爾卡皮坦需要Ruby^2.2

brew unlink ruby; brew install Ruby 
gem install travis 

使用特拉維斯寶石來加密你的祕密PAT和更新您的.travis.yml

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