原文地址:
本人翻译水平有限,译文中难免存在错误。欢迎指出并及时纠正。
Auto-deploying built products to gh-pages with Travis(使用 Travis 自动发布构建好的项目到 gh-pages)
This is a set up for projects which want to check in only their source files,but have their gh-pages branch automatically updated with some compiled output every time they push.
本设置用于检查项目源文件,每次推送时自动编译内容并更新到 gh-pages 分支
Create a compile script(创建一个 compile 脚本)
You want a script that does a local compile to e.g. an out/ directory. Let's call this compile.sh for our purposes,but for your project it might be npm build or gulp make-docs or anything similar.
你需要一个本地编译脚本,比如它编译内容到 out 目录。我们称之为 compile.sh ,不过对于你们的项目它可能是 npm build 或 gulp make-docs 或其它类似的命令。
The out/ directory should contain everything you want deployed to gh-pages . That almost always includes an index.html .
这个 out 目录应该包含你想发布到 gh-pages 的所有内容。这些内容中几乎总是包含一个 index.html 文件。
Check this script in to your project.
在你们的项目中检入这个脚本。
Create a deploy script(创建一个 deploy 脚本)
Create a deploy script,call it deploy.sh ,with the following contents:
用以下内容创建一个名为 deploy.sh 的 deploy 脚本:
SOURCE_BRANCH="master"
TARGET_BRANCH="gh-pages"
function doCompile {
./compile.sh
}
Pull requests and commits to other branches shouldn't try to deploy,just build to verify
PR 和 commits 到其他分支不应该尝试发布,只建立验证
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "$SOURCE_BRANCH" ]; then
echo "Skipping deploy; just doing a build."
doCompile
exit 0
fi
Save some useful information
保存一些有用的信息
REPO=git config remote.origin.url
SSH_REPO=${REPO/https://github.com//git@github.com:}
SHA=git rev-parse --verify HEAD
Clone the existing gh-pages for this repo into out/
克隆现有的 gh-pages 到这个仓库的 out/ 目录
Create a new empty branch if gh-pages doesn't exist yet (should only happen on first deploy)
如果不存在 gh-pages 则创建一个新的空分支(应该只发生在第一次 deploy)
git clone $REPO out
cd out
git checkout $TARGET_BRANCH || git checkout --orphan $TARGET_BRANCH
cd ..
Clean out existing contents
清理现有内容
rm -rf out/*/ || exit 0
Run our compile script
运行我们的 compile 脚本
doCompile
Now let's go have some fun with the cloned repo
现在让我们来给已克隆的仓库做一些有趣的事吧
cd out
git config user.name "Travis CI"
git config user.email "$COMMIT_AUTHOR_EMAIL"
If there are no changes to the compiled out (e.g. this is a README update) then just bail.
如果编译出来的内容没有改变(比如更新了 README)则取保候审
if [ -z git diff --exit-code ]; then
echo "No changes to the output on this push; exiting."
exit 0
fi
Commit the "changes",i.e. the new version.
提交 "更改",比如新版本
The delta will show diffs between new and old versions.
delta 将显示新版和旧版之间的差异
git add .
git commit -m "Deploy to GitHub Pages: ${SHA}"
Get the deploy key by using Travis's stored variables to decrypt deploy_key.enc
通过使用 Travis 存储的变量获取 deploy key 来解密 deploy_key.enc
ENCRYPTED_KEYVAR="encrypted${ENCRYPTION_LABEL}_key"
ENCRYPTED_IVVAR="encrypted${ENCRYPTION_LABEL}_iv"
ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR}
ENCRYPTED_IV=${!ENCRYPTED_IV_VAR}
openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in deploy_key.enc -out deploy_key -d
chmod 600 deploy_key
eval ssh-agent -s
ssh-add deploy_key
Now that we're all set up,we can push.
现在我们一切准备就绪,可以推送了
git push $SSH_REPO $TARGET_BRANCH
This deploy script depends on an encrypted deploy key file,deploy_key.enc ,which we will discuss shortly. The basic idea is that Travis stores a couple environment variables for you,not checked in to your repo,which allow the script to decrypt deploy_key.enc ,which is checked in to your repo.
这个 deploy 脚本依赖一个加密的 deploy key 文件称作 deploy_key.enc ,我们稍后讨论。基本想法是你不检入必要的解密文件到你的仓库,让 Travis 存储一对环境变量,这样就可以让脚本来解密你仓库中的 deploy_key.enc 。
Sign up for Travis and add your project(注册 Travis 并添加你的项目)
Get an account at Turn on Travis for your repository in question,using the Travis control panel.
在 获取一个账户。使用 Travis 控制面板打开你的项目仓库的 Travis 服务。
Get encrypted credentials(获取加密证书)
The trickiest part of all this is that you want to give Travis the ability to run your deploy script and push changes to gh-pages,without checking in the necessary credentials to your repo. The solution for this is to use Travis's .
其中最棘手的部分是,在不检入必要的证书到你的仓库的情况下,需要给予 Travis 能够运行 deploy 脚本以及推送更新的文件到 gh-pages 的能力。解决方案是使用 Travis 的
NOTE: an earlier version of this guide recommended generating a GitHub personal access token and encrypting that. Although this is simpler,it is not a good idea in general,since it means any of your repository's collaborators would be able to edit the Travis build script to email them your access token,thus giving them access to all your repositories. The repository-specific deploy key approach is safer.
注意:这个教程的早期版本推荐生成一个 Github personal access token 并加密它。尽管这很简单,但是总体而言不是一个好的主意,因为这表示你的仓库的任何协作人员都能编辑这个 Travis 构建脚本来给他们发送你的访问 token,这样他们得到了访问你所有仓库的权限。所以指定仓库 depoly key 的方式更安全
First,. You should not reuse existing SSH keys,and you should not add the SSH key to your GitHub account.
首先,。你不应该重用现有的 SSH key,也不应该添加这个 SSH key 到你的 GitHub 账户
Next,add that deploy key to your repository at https://github.com///settings/keys .
其次,在 https://github.com///settings/keys 添加那个 deploy key 到你的仓库
Now use the Travis client to encrypt the generated deploy key. The result should look something like this:
现在使用 Travis 客户端加密这个已生成的 deploy key。结果看起来如下所示:
$ travis encrypt-file deploy_key
encrypting deploy_key for domenic/travis-encrypt-file-example
storing result as deploy_key.enc
storing secure env variables for decryption
Please add the following to your build script (before_install stage in your .travis.yml,for instance):
openssl aes-256-cbc -K $encrypted_0a6446eb3ae3_key -iv $encrypted_0a6446eb3ae3_key -in super_secret.txt.enc -out super_secret.txt -d
Pro Tip: You can add it automatically by running with --add.
Make sure to add deploy_key.enc to the git repository.
Make sure not to add deploy_key to the git repository.
Commit all changes to your .travis.yml.
Make note of that encryption label,here "0a6446eb3ae3" . This can be public information; it just says which environment variables to use on the Travis server when decrypting this file.
记下那个加密 label,这里是 "0a6446eb3ae3" 。这可以是公共信息;它只是说当在 Travis 服务器上解密这个文件时使用哪个环境变量。
You should follow the instructions and commit deploy_key.enc to the repository. You should also add deploy_key to your .gitignore ,or delete it. Ignore the bits about .travis.yml ,however; we're going to do that part all custom-like.
你应该按照指示并提交 deploy_key.enc 到仓库。你也应该添加 deploy_key 到你的 .gitignore ,或删除它。不过,我们忽略了 .travis.yml 文件;我们去做所有自定义的部分吧。
Create your .travis.yml file(创建你的 .travis.yml 文件)
With all this in hand,you can create a .travis.yml file. It should look like this:
万事俱备,你可以创建一个 .travis.yml 文件。它看起来是这样的:
script: bash ./deploy.sh
env:
global:
- ENCRYPTION_LABEL: "<.... encryption label from previous step ....>"
- COMMIT_AUTHOR_EMAIL: "you@example.com"
The $COMMIT_AUTHOR_EMAIL variable will be used in the commits to gh-pages that Travis makes on your behalf.
变量 $COMMIT_AUTHOR_EMAIL 用于提交信息并提交到 gh-pages,它告诉 Travis 使用你的名义来做这件事。
If your compile script depends on certain environment features,you might want to set up the environment using Travis's built-in abilities,e.g. by changing the language lines like so:
如果你的 compile 脚本依赖某些特定的环境,你可能需要使用 Travis 的内置功能设置这个环境,比如像这样更改语言行:
stable
(In this case, Travis will install the latest stable Node.js,then run npm install .)
(在这种情况下, Travis 将安装最新的稳定版的 Node.js,然后运行 npm install 。)
Finishing up(结尾)
At this point you should have 3-4 files checked in to your repo: compile.sh ,deploy.sh ,and .travis.yml . If you've also told Travis about your repo,then the first time you push to GitHub with these changes,it will automatically compile and deploy your source!
到这里,你应该有 3-4 个文件检入了你的仓库:compile.sh ,deploy.sh ,deploy_key.enc ,和 .travis.yml 。如果你也已经告诉 Travis 关于你的仓库,而且你第一次推送这些更改到 GitHub,它将自动编译并发布你的源文件!
See it in action(参考内容)
I use basically this exact setup for The relevant files are:
我基本上在 使用了这个确切的设置。相关文件是:
(I have inlined the compile script into deploy.sh ,so there is no separate compile.sh .)
(我已经在 deploy.sh 内置了 compile 脚本,所以没有单独的 compile.sh 文件。)
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|