github常用workflows

lijunyi2024-05-31githubworkflows

自动发布到 docker

name: Build and Push Docker Image

on:
  push:

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Check out code
        uses: actions/checkout@v2
      
      - 
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      -
        name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          tags: |
            ${{ secrets.DOCKERHUB_USERNAME }}/project-name:${{ github.ref_name }}
            ${{ secrets.DOCKERHUB_USERNAME }}/project-name:latest

自动同步上游代码

name: Upstream Sync

permissions:
  contents: write

on:
  schedule:
    - cron: "0 0 * * *" # every day
  workflow_dispatch:

jobs:
  sync_latest_from_upstream:
    name: Sync latest commits from upstream repo
    runs-on: ubuntu-latest
    if: ${{ github.event.repository.fork }}

    steps:
      # Step 1: run a standard checkout action
      - name: Checkout target repo
        uses: actions/checkout@v3

      # Step 2: run the sync action
      - name: Sync upstream changes
        id: sync
        uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
        with:
          upstream_sync_repo: 上游仓库(xxx/project)
          upstream_sync_branch: main
          target_sync_branch: main
          target_repo_token: ${{ secrets.GITHUB_TOKEN }} # automatically generated, no need to set

          # Set test_mode true to run tests instead of the true action!!
          test_mode: false

      - name: Sync check
        if: failure()
        run: |
          echo "[Error] 由于上游仓库的 workflow 文件变更,导致 GitHub 自动暂停了本次自动更新,你需要手动 Sync Fork 一次"
          exit 1

博客自动build并推送

  • 本站博客自动发布脚本,两个 Private 仓库,一个源码一个静态页面。实现提交源码后,自动执行 npm run build并推送到blog 仓库
  • 需要建一个名为PATtoken,要有workflows权限
name: Build and Deploy to Blog Repository

on:
  push:
    branches:
      - main  # 可以根据需要更改为您使用的默认分支名

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '20'

    - name: Install dependencies
      run: npm install

    - name: Build
      run: npm run build

    - name: Deploy to Blog Repository
      run: |
        git config --global user.name 'username'
        git config --global user.email 'xxxx@qq.com'
        git clone https://x-access-token:${{ secrets.PAT }}@github.com/username/blog.git
        cp -r .vuepress/dist/* blog/
        cd blog
        git add .
        git commit -m "Deploy updates"
        git push
      env:
        PAT: ${{ secrets.PAT }}
Last Updated 6/14/2024, 3:05:31 AM