Github Actions
约 1131 字大约 4 分钟
2025-01-02
介绍
Github Actions
是 Github
提供的持续集成服务,于2018年10月推出。只要代码推送到 Github
,就会自动运行 Github Actions
。
配置
添加.github/workflows/
目录
需要在项目下新建 .github/workflows/
文件夹,接着在 workflows
文件夹下新增 YAML
文件如: xxx.yml
、xxx.yaml
。 当 Github
提交代码时,它会自动的检测 .github/workflows/
目录中的文件,如果满足触发条件则将执行配置文件中的jobs
。
编写.yml
name
:名称随便取on
:触发条件
根据具体的需求写触发条件。
push
触发
on: [push]
master
分支push
触发
on:
push:
branches:
- master
jobs
:规定要执行的任务,如下我们定义了一个任务build-and-deploy
,每个任务包含如下配置runs-on
:运行所需要的虚拟机环境,Github
为我们提供了如下环境
ubuntu-latest,ubuntu-18.04或ubuntu-16.04
windows-latest,windows-2019或windows-2016
macOS-latest或macOS-10.14
steps
:任务运行的步骤
steps
字段指定每个 Job
的运行步骤,每个job
由多个step
构成,它会从上至下依次执行。steps
可以包含一个或多个步骤,每个 step
步骤可以有:
name
: 步骤名称,步骤的名称。env
: 该步骤所需的环境变量。id
: 每个步骤的唯一标识符uses
: 使用三方的action
,这个表示使用别人预先设置好的Actions,官方acionts市场with
: 指定某个action 可能需要输入的参数。run
: 执行哪些指令,具体运行什么命令行代码。continue-on-error
: 设置为 true 允许此步骤失败job 仍然通过。timeout-minutes
: step 的超时时间。
例子:
name: vueperss deploy
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master
- name: vueperss build and deploy
run: |
yarn install
yarn docs:build
cd docs/.vuepress/dist
git init
git config --global user.email ${{ secrets.MT_EMAIL }}
git config --global user.name ${{ secrets.MT_NAME }}
git add -A
git commit -m 'Auto deploy from Github Actions'
git push -f https://${{ secrets.MT_TOKEN }}@github.com/matiastang/matiastang.github.io master
使用限制
问题
工作流权限
如果没有开启工作流权限将导致工作流运行失败,开启方式如下:
- 转到存储库
Setting
- 选择
Actions
->General
- 在
"工作流权限(Workflow permissions)"
中,选择Read and write permissions
The process '/usr/bin/git' failed with exit code 128
使用如下VuePress
文档中提供的.yml
。VuePress GitHub Pages and Github Actions
name: vueperss deploy
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master
- name: vuepress-deploy
uses: jenkey2011/vuepress-deploy@master
env:
ACCESS_TOKEN: ${{ secrets.MT_TOKEN }}
TARGET_REPO: matiastang/matiastang.github.io
TARGET_BRANCH: master
TARGET_LINK:
BUILD_SCRIPT: yarn && yarn docs:build
BUILD_DIR: docs/.vuepress/dist
COMMIT_MESSAGE: Auto deploy from Github Actions
报错如下:
Done in 12.38s.
Build success
==> Changing directory to 'docs/.vuepress/dist' ...
==> Prepare to deploy
Initialized empty Git repository in /github/workspace/docs/.vuepress/dist/.git/
fatal: not in a git directory
后面更新为如下.yml
,不使用三方的action
,自己写提交。
name: vueperss deploy
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master
- name: vueperss build and deploy
run: |
yarn install
yarn docs:build
cd docs/.vuepress/dist
git init
git add -A
git commit -m 'Auto deploy from Github Actions'
git push -f https://${{ secrets.MT_TOKEN }}@github.com/matiastang/matiastang.github.io master
还是如下报错
Done in 13.09s.
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
Initialized empty Git repository in /home/runner/work/HulkPress/HulkPress/docs/.vuepress/dist/.git/
hint: git branch -m <name>
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: empty ident name (for <runner@fv-az354-901.wyswqxu4gagu3ld0braovpisna.bx.internal.cloudapp.net>) not allowed
Error: Process completed with exit code 128.
看上面的错误信息是要我配置git
的用户信息。所以更新为如下:
name: vueperss deploy
on: [push]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master
- name: vueperss build and deploy
run: |
yarn install
yarn docs:build
cd docs/.vuepress/dist
git init
git config --global user.email ${{ secrets.MT_EMAIL }}
git config --global user.name ${{ secrets.MT_NAME }}
git add -A
git commit -m 'Auto deploy from Github Actions'
git push -f https://${{ secrets.MT_TOKEN }}@github.com/matiastang/matiastang.github.io master
上面这种就运行成功了,应该就是git
现在需要配置用户信息导致的错误。三方的action
还没更新,所以也有问题。
JavaScript heap out of memory
内存超了,一个解决办法是添加环境变量
env:
NODE_OPTIONS: '--max_old_space_size=4096'