选中内容(绿色)时除了会搜索文章名,还会搜索文章内容
点击结果中的文章名进入文章界面后可以按Ctrl+F在页面内搜索
  • 版权:CC BY-SA 4.0
  • 创建:2021-11-11
  • 更新:2021-11-11
github action 当提交代码后,自动构建生成 pages 文件, 然后提交到本仓库的另一个分支, 重点是怎么提交到另外一个分支,权限怎么获取,这里介绍了使用 GITHUB_TOKEN 的用法


目的

使用 github 的 action 可以非常好地实现一些自动化操作。

这里有一个场景, 当提交代码后,自动构建生成 pages 文件, 然后提交到本仓库的另一个分支

重点是怎么提交到另外一个分支,权限怎么获取

方法

如果只是提交了代码,推送到当前分支,直接push就好了

  1. git add -A
  2. git commit -m ""
  3. git push

如果是新git init的仓库,则:

  • 方法一: ssh-keygen 生成一对秘钥,公钥添加到仓库的deploy keys,私钥添加到Secrets,然后在 action 脚本中使用这个私钥来访问服务器即可。比如在secrets中存了变量ACCESS_KEY,值为生成的私钥
    1. SSHPATH="$HOME/.ssh"
    2. rm -rf "$SSHPATH"
    3. mkdir -p "$SSHPATH"
    4. echo "${{ secrets.ACCESS_KEY }}" > "$SSHPATH/id_rsa"
    5. chmod 600 "$SSHPATH/id_rsa"
    6. sudo sh -c "echo StrictHostKeyChecking no >>/etc/ssh/ssh_config"
    7. remote_addr=`git remote get-url --push origin`
    8. domain=`echo $remote_addr| awk -F'/' '{print $3}'`
    9. user_org=`echo $remote_addr| awk -F'/' '{print $4}'`
    10. repo=`echo $remote_addr| awk -F'/' '{print $5}'`
    11. remote_addr=git@${domain}:${user_org}/${repo}.git
    12. git remote add origin ${remote_addr}
    13. git push origin HEAD:gh-pages --force
  • 方法二:直接使用 github action 内置的 token 变量GITHUB_TOKEN
  1. remote_addr=`git remote get-url --push origin`
  2. remote_addr=`echo $remote_addr| awk -F'://' '{print $2}'`
  3. remote_addr=https://${user_name}:${{ secrets.GITHUB_TOKEN }}@${remote_addr}
  4. git remote add origin ${remote_addr}
  5. git push origin HEAD:gh-pages --force

这两种方法,看起来第二种方法更简单, 如果需求只是推送代码到当前仓库, 使用GITHUB_TOKEN是非常好的选择, 不用再配置任何配置。

但是有一种情况,不能使用第二种:

使用GITHUB_TOKEN推送的代码不会再次触发任何action,可以看官方源文档的描述 , 是为了防止递归构建发生。
所以,如果你希望构建完成后再自动触发其它构建,就只能用自己设置 key 的方法了。

比如:

  • 推送代码到main分支,触发构建生成文件,提交到gh-pages分支
  • 当检测到gh-pages分支有改动时,触发构建将gh-pages分支的内容同步到其它服务器

这个例子里,如果使用GITHUB_TOKEN,就会出现第二个构建脚本无法触发,虽然监测的不是同一个分支,只能使用第一种设置key的方式

下面是这个例子的action脚本,自动根据设置了secrets.ACCESS_KEY与否来决定使用哪种方式

  1. # This is a basic workflow to help you get started with Actions
  2. name: publish pages
  3. # Controls when the action will run.
  4. on:
  5. # Triggers the workflow on push or pull request events but only for the main branch
  6. push:
  7. branches: [ main ]
  8. pull_request:
  9. branches: [ main ]
  10. # Allows you to run this workflow manually from the Actions tab
  11. workflow_dispatch:
  12. # A workflow run is made up of one or more jobs that can run sequentially or in parallel
  13. jobs:
  14. # This workflow contains a single job called "build"
  15. build:
  16. # The type of runner that the job will run on
  17. runs-on: ubuntu-latest
  18. strategy:
  19. matrix:
  20. python-version: [3.8]
  21. # Steps represent a sequence of tasks that will be executed as part of the job
  22. steps:
  23. # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  24. - uses: actions/checkout@v2
  25. # Runs a set of commands using the runners shell
  26. - name: build doc
  27. id: build_doc
  28. run: |
  29. pip3 install setuptools
  30. pip3 install teedoc
  31. ls -al ~/.local/bin/
  32. export PATH=~/.local/bin/:$PATH
  33. commit_info=`git describe --all --always --long`
  34. out_dir=out`python3 -c 'import json; f=open("site_config.json");config=json.load(f);print(config["site_root_url"])'`
  35. teedoc install
  36. teedoc build
  37. echo "out dir: ${out_dir}"
  38. cp -rf .github $out_dir
  39. echo ::set-output name=out_dir::$out_dir
  40. remote_addr=`git remote get-url --push origin`
  41. echo "remote addr: ${remote_addr}"
  42. cd $out_dir
  43. user_name=`git log -1 --pretty=format:'%an'`
  44. user_email=`git log -1 --pretty=format:'%ae'`
  45. git config --global init.defaultBranch gh-pages
  46. git init
  47. git config user.name ${user_name}
  48. git config user.email ${user_email}
  49. git add -A
  50. git commit -m "rebuild website ad $commit_info"
  51. if [[ "${{ secrets.ACCESS_KEY }}x" == "x" ]]; then
  52. remote_addr=`echo $remote_addr| awk -F'://' '{print $2}'`
  53. remote_addr=https://${user_name}:${{ secrets.GITHUB_TOKEN }}@${remote_addr}
  54. else
  55. SSHPATH="$HOME/.ssh"
  56. rm -rf "$SSHPATH"
  57. mkdir -p "$SSHPATH"
  58. echo "${{ secrets.ACCESS_KEY }}" > "$SSHPATH/id_rsa"
  59. chmod 600 "$SSHPATH/id_rsa"
  60. sudo sh -c "echo StrictHostKeyChecking no >>/etc/ssh/ssh_config"
  61. domain=`echo $remote_addr| awk -F'/' '{print $3}'`
  62. user_org=`echo $remote_addr| awk -F'/' '{print $4}'`
  63. repo=`echo $remote_addr| awk -F'/' '{print $5}'`
  64. remote_addr=git@${domain}:${user_org}/${repo}.git
  65. fi
  66. git remote add origin ${remote_addr}
  67. git push origin HEAD:gh-pages --force
文章有误?有想法想讨论?查看或者发起勘误/讨论 主题
(发起评论需要先登录 github)

/wallpaper/wallhaven-5weqr5.jpg