Github Action 使用介绍

发布时间: 更新时间: 总字数:1243 阅读时间:3m 作者:IP:上海 网址

Github Action 是 Github 官方的 CICD 工具,你可以在 Github Action 中做,单元测试、代码编译、发布、部署等动作,是 Github 的利器。

简介

With GitHub Actions you can automate your workflow from idea to production.

官网

本人开源的 Actions

代码托管在x-actions,说明:

Actions 开发文档

语法 icon 默认变量

推荐的 Actions

本地模拟

  • nektos/act 项目
  • 安装
brew install act
  • 常用命令
# 本地运行
act
# 列出actions
act -l

action 时区

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

Dependabot

# 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

expand-id ...
.github/workflows/renovate.yaml ...
#!/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-config.json5 ...
  • .github/renovate-postupgrade.sh
.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
本文总阅读量 次 本站总访问量 次 本站总访客数