GitHub Workflows

注意
本文最后更新于 2024-05-19,文中内容可能已过时。

原文链接:https://ovea-y.cn/github_workflows/

本文介绍GitHub工作流的创建和作用。

工作流创建方式

在git项目下,创建.github/workflows文件夹,里面编写的任何yml文件都是工作流的一部分。 Pasted image 20240519154126

secrets的创建方式

secrets.GITHUB_TOKEN是GitHub自动创建的,不需要自己创建。

secrets.SERVER_HOST这个的来源,需要在GitHub对应的项目的设置中设置。 Pasted image 20240519154915

readme的构建标签

Pasted image 20240519155308 配置好Branch、Event类型后,把下面这段内容复制到markdown文档里,就会自动显示自动化流程状态了。其实就是一张图片,由GitHub自动生成提供。 Screenshot 2024-05-19 at 15.53.30

一. 自动构建网站并推送到远程服务器

自动构建hugo,并更新远程服务器的静态网站内容。

yml

name: Deploy Hugo Site

# 当推送到main分支时触发部署流程
on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    # 检出仓库的代码
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: 'recursive' # 递归拉取子模块,包括子模块中的子模块

    # 安装Hugo
    - name: Setup Hugo
      uses: peaceiris/actions-hugo@v2
      with:
        hugo-version: 'latest'
        extended: true

    # 构建Hugo站点
    - name: Build Hugo site
      run: hugo --minify

    # 删除远程服务器上的旧网站内容
    - name: Remove old site content
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        password: ${{ secrets.SERVER_PASSWORD }}
        port: ${{ secrets.SERVER_PORT }}
        script: |
          rm -rf ${{ secrets.ROOT_PATH }}/html/*          
      continue-on-error: true

    # 部署到远程服务器
    - name: Deploy to Server
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        password: ${{ secrets.SERVER_PASSWORD }}
        port: ${{ secrets.SERVER_PORT }}
        # 根据实际部署路径进行替换
        target: "${{ secrets.ROOT_PATH }}/"
        # 注意,这里假设你的Hugo站点是构建在public目录下
        source: "public/*"
        # 如果使用SSH密钥而不是密码,请替换成以下内容:
        # key: ${{ secrets.SERVER_SSH_KEY }}
        # 使用SSH密钥时,不要使用password字段

    # Delete temp files
    - name: Remove old site content
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        password: ${{ secrets.SERVER_PASSWORD }}
        port: ${{ secrets.SERVER_PORT }}
        script: |
          cp -r ${{ secrets.ROOT_PATH }}/public/* ${{ secrets.ROOT_PATH }}/html/
          rm -rf ${{ secrets.ROOT_PATH }}/public          

二、设置CI

yml

name: CI

# 当向main分支推送时触发
on:
  push:
    branches:
      - '*'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    # 设置Rust环境
    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        override: true

    # 添加构建步骤
    - name: Build project
      run: |
        cargo build --release
        echo "Build project here"        

三、发布新版本时自动构建产物

bash

git push -u origin main
git tag "0.9.0"
git push -u origin 0.9.0

yml

name: Build and Release

# 当向main分支推送时触发
on:
  push:
    # branches:
    #   - main
    tags:
        - '*'

permissions:
    contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    permissions: write-all
    steps:
    - uses: actions/checkout@v2

    # 设置Rust环境
    - name: Set up Rust
      uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        toolchain: stable
        override: true

    # 添加构建步骤
    - name: Build project
      run: |
        cargo build --release
        echo "Build project here"        

    # 编译产物打包
    - name: Archive production artifacts
      run: |
        tar czvf processtrace-${{ github.ref_name }}.tar.gz -C target/release process_trace        

    - name: Create release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref_name }}
        release_name: Release ${{ github.ref_name }}
        draft: false
        prerelease: false

    # 添加构建产物上传步骤
    - name: Upload Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: processtrace-${{ github.ref_name }}.tar.gz
        asset_name: processtrace-${{ github.ref_name }}-linux.zip
        asset_content_type: application/zip

原文链接:https://ovea-y.cn/github_workflows/

相关内容