Files
openit/.github/说明文档.md
GitHub Actions cd995d7629 Hello World
2022-06-30 02:18:52 +00:00

155 lines
5.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Workflow的具体实现
## CDN具体实现
首先常规步骤然后用Linux系统的curl命令访问jsdelivr的更新CDN地址脚本里有
然后用cron命令定时(这里有个方便的在线小工具测试定时是否符合期望https://tooltt.com/crontab/) 我定时每天从每小时的第5分钟开始每20分钟执行一次
然后添加一个触发按钮`workflow_dispatch`用于调试 [如图](#调试时点击绿框直接运行)
#### 具体实现
```
name: CDN
on:
schedule:
- cron: "5/20 * * * *"
workflow_dispatch:
jobs:
my-job:
name: jsdelivr
runs-on: ubuntu-latest
steps:
- name: long
run: curl https://purge.jsdelivr.net/gh/git-yusteven/openit@main/long
- name: Clash.yaml
run: curl https://purge.jsdelivr.net/gh/git-yusteven/openit@main/Clash.yaml
```
## Base64 Encode 具体实现
通过对某一个或几个项目的更改自动化把文件base64编码并输出
不用一个个复制,最后给出全部代码
### 首先设定环境
1. 设定触发条件特定几个文件写在paths下方xxx处或者某些文件不要定时运行(后文给出原因),最好加上手动触发`workflow_dispatch:`放在xxx的下一行与push对齐以备调试所用
[如图](#调试时点击绿框直接运行)
```
name: xxx
on:
push:
paths:
- 'xxx'
workflow_dispatch:
```
2. ubuntu系统即可代码给出的是持续最新版ubuntu
```
jobs:
my-job:
name: xxx
runs-on: ubuntu-latest
```
3. 使用官方`actions/checkout@v3`命令检出代码(v1的改良版v1用到了指针的概念不易懂)
```
- uses: actions/checkout@v3
```
4. 配置git注意把邮箱和名字改成自己GitHub的邮箱和名字引号内添加此处默认GitHub action
```
# 配置 git
- name: config git
run: git config --global user.email "actions@github.com"
git config --global user.name "GitHub Action"
```
### 其次修改仓库文件
**只需使用Linux上的命令修改文件即可不用去考虑cd到仓库文件夹**
```
- name: xxxx
run: 具体的命令,也可以是脚本
```
### 最后上传文件
这里有两种方式分为手动推送或者运行一个bash脚本下面给出详细解释
#### 其一,手动推送
<br>①检查change了什么文件
<br>②将文件提交到缓存区
<br>③给要上传的文件一个备注(不是名字)
<br>④push上传
```
- name: check for changes
run: git status
- name: stage changed files
run: git add .
- name: commit changed files
run: git commit -m "By Github actions"
- name: push code to main
run: git push
````
#### 其二 运行一个bash脚本
理想情况是修改、提交、推送,但是或许存在一些情况,导致其实文件没有发生变化。如果这时执行`git commit`,会提示`nothing to commit, working tree clean`
<br>注意了,这是一个报错,意味着 action 执行失败,会出现一个红叉❌,注意避免分支运行此Action,会报错`error: src refspec main does not match any`
<br>这里提供了一个bash脚本需要放在仓库中建议命名为`update-repo.sh`
```
#!/bin/bash
status_log=$(git status -sb)
# 这里使用的是 main 分支,根据需求自行修改
if [ "$status_log" == "## main...origin/main" ];then
echo "nothing to commit, working tree clean"
else
git add .&&git commit -m "By GitHub Action"&&git push origin main
fi
```
然后action脚本里这样写
```
- name: <!--上方的四件套或者下方命令运行bash脚本两者选其一,update-repo.sh为上述bash文件名-->run script
run: chmod +x ./update-repo.sh&&./update-repo.sh
```
#### 具体实现
```
name: xxx
on:
push:
paths:
- 'xxx'
workflow_dispatch:
jobs:
my-job:
name: xxx
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: config git
run: git config --global user.email "actions@github.com"
git config --global user.name "GitHub Action"
- name: xxxx
run: 具体的命令
- name: <!--下方是四件套-->check for changes
run: git status
- name: stage changed files
run: git add .
- name: commit changed files
run: git commit -m "By GitHub Action"
- name: push code
run: git push
- name: <!--上方的四件套或者下方命令运行bash脚本两者选其一,update-repo.sh为上述bash文件名-->run script
run: chmod +x ./update-repo.sh&&./update-repo.sh
```
官方actions/checkout说明文档https://github.com/actions/checkout
参考https://juejin.cn/post/6875857705282568200
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
## 如何创建密钥
新建一个Token(workflows权限,其他权限自行斟酌)添加到仓库密钥区
在这里新建一个Token指路链接https://github.com/settings/tokens
在这里添加到仓库密钥区指路链接https://github.com/您的名字/您的仓库名/settings/secrets/actions/new
##### 调试时,点击绿框直接运行
##### [返回到CDN](#cdn具体实现) /// [返回到Base64](#base64-encode-具体实现)
![workflows](https://github.com/git-yusteven/openit/raw/main/images/workflows.jpg)