Github Action是 Github 官方的 CICD 工具,你可以在Github Action中做,单元测试、代码编译、发布、部署等动作,是 Github 的利器。
简介
With GitHub Actions you can automate your workflow from idea to production.本人开源的 Actions
代码托管在x-actions,说明:
- x-actions/hugo:生产 hugo 静态页面
- x-actions/git-push:归档 git 代码
- x-actions/wechat-work:发送微信企业号 robot 通知
- x-actions/cdn-sync:同步代码到 CDN
- x-actions/dingtalk:发送钉钉 robot 通知
Actions 开发文档
推荐的 Actions
- licensed
- github-profile-views-counter 记录 github-profile 查看次数
本地模拟
- nektos/act 项目
- 安装
brew install act- 常用命令
# 本地运行
act
# 列出actions
act -laction 时区
sudo timedatectl set-timezone "Asia/Shanghai"demo
- 取消之前的任务
- name: Cancel previous runs on the same branch
if: ${{ github.ref != 'refs/heads/master' }}
uses: styfle/cancel-workflow-action@0.7.0
with:
access_token: ${{ github.token }}- 触发其他的任务
- name: Hook Update workflow
run: |
curl -XPOST -H "Authorization: token ${{ secrets.WORKFLOW_TRIGGERS }}" -H "Accept: application/vnd.github.v3+json" -H "Content-Type: application/json" https://api.github.com/repos/xiexianbin/xiexianbin.github.io/actions/workflows/deploy.yml/dispatches添加手动触发工作流
手动工作流,通过 on.workflow_dispatch 字段指定,需要将代码何如 main or master 分支才生效。
on:
...
workflow_dispatch:
inputs:
reason:
description: 'run action reason'
required: false
type: string
default: 'manually test'推荐的 actions
CodeQL Action
- CodeQL Action 针对版本库源代码运行 GitHub 行业领先的语义代码分析引擎 CodeQL,查找安全漏洞
.github/workflows/codeql.yaml- 配置参考 Configuring default setup for a repository
- Gemini Code Assist
Dependabot
- Dependabot
.github/dependabot.yml示例参考
# Basic set up for three package managers
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
# Workflow files stored in the default location of `.github/workflows`. (You don't need to specify `/.github/workflows` for `directory`. You can use `directory: "/"`.)
directory: "/"
schedule:
interval: "weekly"
# Maintain dependencies for npm
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
# Maintain dependencies for Composer
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"Renovate
-
Renovate 通过自动
拉取请求自动更新依赖关系,实现有两种方式: -
- GITHUB_TOKEN 密钥不能用于对 Renovate 进行身份验证,因为它的权限限制过多。特别是,使用 GITHUB_TOKEN 从更多类型的 Github 工作流创建新的
拉取请求会导致拉取请求不会触发拉取请求和推送 CI 事件(do not trigger your Pull Request and Push CI events)- 如果你想在 Renovate 中使用 github-actions manager,请确保你提供的令牌包 workflow scope。否则,GitHub 不允许 Renovate 更新工作流程文件,因此它将无法为受影响的软件包(如
actions/checkout或renovatebot/github-action本身)创建更新 PR
- 如果你想在 Renovate 中使用 github-actions manager,请确保你提供的令牌包 workflow scope。否则,GitHub 不允许 Renovate 更新工作流程文件,因此它将无法为受影响的软件包(如
- 推荐使用 GitHub APP 程序,可以得到更好的权限控制,参考 Running as a GitHub App
- 创建新应用,并按照 Renovate 文档中的说明配置应用权限和 config.js
- actions/create-github-app-token 创建 GitHub 应用程序安装访问令牌的 GitHub 操作
- GITHUB_TOKEN 密钥不能用于对 Renovate 进行身份验证,因为它的权限限制过多。特别是,使用 GITHUB_TOKEN 从更多类型的 Github 工作流创建新的
#!/bin/bash
# renovate: datasource=github-tags depName=mikefarah/yq
export YQ_VERSION=v4.44.2
curl -fsSL -o /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64
chmod a+x /usr/local/bin/yq
runuser -u ubuntu renovate- .github/renovate-postupgrade.sh
- 使用示例
# renovate: datasource=github-tags depName=helm/helm
version: v3.15.2
# renovate: datasource=custom.python_actions depName=python
python-version: 3.12.4
# renovate: datasource=github-releases depName=norwoodj/helm-docs
HELM_DOCS_VERSION=1.13.1