github图床 私库 gitLab备份

教程

Token


Token


cloudflare 新建workers

cloudflare

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url)
  const path = url.pathname
  
  // 定义环境变量 GITHUB_USERNAME 和 GITHUB_PAT
  const GITHUB_USERNAME = 'zbb07' // 替换为你的 GitHub 用户名
  const GITHUB_PAT = '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' // 替换为你的 GitHub PAT,只开放 repo 权限即可
  
  // 构建 GitHub raw 内容的 URL
  const githubUrl = `https://raw.githubusercontent.com/${GITHUB_USERNAME}${path}`
  
  // 创建新的请求,添加必要的头部
  const modifiedRequest = new Request(githubUrl, {
    method: request.method,
    headers: {
      ...request.headers,
      'Authorization': `token ${GITHUB_PAT}`,
      'Accept': 'application/vnd.github.v3.raw'
    }
  })
  
  // 发送请求并返回响应
  try {
    const response = await fetch(modifiedRequest)
    
    // 如果响应不成功,抛出错误
    if (!response.ok) {
      throw new Error(`GitHub API responded with ${response.status}: ${response.statusText}`)
    }
    
    // 创建新的响应,保留原始内容但移除敏感头部
    const newResponse = new Response(response.body, response)
    newResponse.headers.delete('Authorization')
    
    return newResponse
  } catch (error) {
    return new Response(`Error: ${error.message}`, { status: 500 })
  }
}

测试 显示 # img1

https://img1.kev071225.workers.dev/img1/main/README.md


设置自定义域名 tt.n06.dpdns.org


设置PicGo

插件设置 安装compress-next 1.5.2


tinypng 申请 Token

https://tinypng.com/




6RLd8v72LsrYHn3TpsZQ0Hjx00TFBbyk




PicGo设置


图床设置 GitHub



图床配置名 sikuimg1
设定仓库名 zcr07/img1
设定分支名 main
设定Token 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
设定存储路径 siku/
设定自定义域名 https://tt.zb9.dpdns.org/img1/main

上传一张图片测试



github中已经上传成功

https://github.com/zbb07/img1/tree/main/img11


备份到gitLab

https://gitlab.com/


只需要命名 img11 和github上一样就行了





glpat-bRVj5zf6DyKpc4gJXRU_

回到Github


https://github.com/zbb07/img1/settings/secrets/actions

GITLAB_USERNAME Gitlab 用户名 zcr071225
GITLAB_REPO Gitlab 上创建的项目名 img11
GITLAB_PAT 上面生成的 Gitlab 项目的 PAT glpat-bRVj5zf6DyKpc4gJXRU_




新增文件

.github/workflows/sync-to-gitlab.yml



name: Sync to GitLab

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  sync:
    runs-on: ubuntu-latest

    env:
      GITLAB_USERNAME: ${{ secrets.GITLAB_USERNAME }}
      GITLAB_REPO: ${{ secrets.GITLAB_REPO }}
      GITLAB_PAT: ${{ secrets.GITLAB_PAT }}

    steps:
    - name: Checkout repository
      uses: actions/[email protected]
      with:
        fetch-depth: 0

    - name: Set up Git
      run: |
        git config --global user.name 'github-actions'
        git config --global user.email '[email protected]'

    - name: Add GitLab remote
      run: git remote add gitlab https://${{ env.GITLAB_USERNAME }}:${{ env.GITLAB_PAT }}@gitlab.com/${{ env.GITLAB_USERNAME }}/${{ env.GITLAB_REPO }}.git

    - name: Force push to GitLab
      run: git push gitlab main --force



同步已成功 查看gitLab



已成功备份到了gitLab上了。

转载请注明出处