使用方法

触发条件

提示💡

repository_dispatch

若想要触发 GitHub 外部发生的活动的工作流,可以使用 GitHub API 触发名为 repository_dispatch 的 Webhook 事件。

请求地址格式为:/repos/{owner}/{repo}/dispatches

const axios = require('axios');
 
const options = {
  method: 'POST',
  url: 'https://api.github.com/repos/songxingguo/songxingguo.github.io/dispatches',
  headers: {
    'content-type': 'application/json',
    Accept: 'application/vnd.github+json',
    Authorization: 'Bearer GITHUB_TOKEN'
  },
  data: {event_type: 'publish', client_payload: {slug}}
};
 
axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});

多条件触发

on:
  push:
    branches:
      - "main"
  repository_dispatch:
    types: [second_brain_sync]

workflow_dispatch : 手动触发

定时执行

on:
  schedule:
    - cron: "0 8 * * *"

执行脚本

static.yml

name: 更新数字花园
on: [push]
jobs:
  garden-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout codes'
        uses: actions/checkout@v4
        with:
          sparse-checkout: |
            garden.sh
          sparse-checkout-cone-mode: false
      - name: "触发garden的GithubAction"
        run: bash ./garden.sh
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}

garden.sh

#!/bin/sh
 
set -eux
 
curl --request POST \
  --url https://api.github.com/repos/songxingguo/garden/dispatches \
  --header "Accept: application/vnd.github+json" \
  --header "Authorization: Bearer $GH_TOKEN" \
  --header 'content-type: application/json' \
  --data '
{
  "event_type":"second_brain_sync"
}'

远程登录

检出代码

指定 node 版本

  - name: Setup Node.js
	uses: actions/setup-node@master
	with:
	  node-version: "18.13.0"

推送代码

推送到其他仓库

 - name: Push to another repository
	uses: cpina/github-action-push-to-another-repository@main
	env:
	  API_TOKEN_GITHUB: ${{ secrets.API_TOKEN_GITHUB }}
	with:
	  source-directory: 'dist'   // 要提交的目标文件
	  destination-github-username:  ${{ secrets.STORE_USER_NAME }}
	  destination-repository-name: ${{ secrets.STORE_ANOTHER_NAME }}
	  user-email: ${{ secrets.USER_EMAIL }}
	  target-branch: master  // 目标分支

部署博客

name: GitHub Actions Build and Deploy
on:
  push:
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v3
        with:
          persist-credentials: false   
          
      - name: Setup Node.js
        uses: actions/setup-node@master
        with:
          node-version: "18.13.0"
 
      - name: Install and Build 🔧
        run: |
          npm i --force
          npm run build
      - name: 推送到代码仓库
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
          GH_REF: "github.com/songxingguo/brain.git"
          CO_TOKEN: ${{ secrets.CO_TOKEN }}
          CO_REF: "e.coding.net/songxingguo/songxingguo.coding.me/brain.git"
        run: |
          cd ./src/.vuepress/dist
          git init
          git config user.name "songxingguo"
          git config user.email "1328989942@qq.com"
          git add .
          git commit -m "Update Blog By GithubAction With Build $TRAVIS_BUILD_NUMBER"
          # Github Pages
          git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages
          # Coding Pages
          git push --force --quiet "https://zrUWsPQJyF:${CO_TOKEN}@${CO_REF}" master:master

部署 Docker

GitHub Actions

IP 地址

docker

常见问题

Action 写入权限

denied: permission_denied: The token provided does not match expected scopes.

和下面的

常用示例

同步代码到Gitee

name: Sync Github Repos To Gitee
on:
    push:
        branches:
            - master
jobs:
    repo-sync:
        runs-on: ubuntu-latest
        steps:
            - name: Sync Github Repos To Gitee
              uses: Yikun/hub-mirror-action@master
              with:
                  src: github/bilibili-ayang
                  dst: gitee/bilibili-ayang
                  dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
                  dst_token: ${{ secrets.GITEE_TOKEN }}
                  static_list: "HTML-CSS-case"
                  force_update: true

参考资料