51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Github构建Hugo并同步COS

前言 {#前言}

最近重新搭建了 hugo 博客 为了去掉繁琐的上传操作,这里使用 github action 来将打包好的文件上传到 cos 并用 python 刷新 CDN, 这里主要是解决有些小伙伴使用腾讯COS部署网站导致每次需要自己上传的麻烦过程

准备工作 {#准备工作}

  • 需要获取到腾讯的访问密钥 腾讯访问密钥获取 。创建密钥后先保存好
  • 获取 cos 存储桶的昵称 获取昵称 如下图示 左边昵称 右边地域
  • 获取 cos 存储桶的地域 获取地域 如下图示 左边昵称 右边地域

5ec5bdb5-9a51-4d98-9d57-c2b304b2b870

设置 Action 需要的数据 {#设置-Action-需要的数据}

在项目下点击 Settings 然后点击 Secrets and variables 下面的 Actions 再点击右边的 New repository secret

6632168d-5321-4e37-a210-846ea71721ce
添加数据 我们这里只需要设置 SecretIdSecretKeyBucketRegion 四个密钥 在 name 里面依次填入上面的密钥昵称 Secret 填入对应的内容

28455981-0c61-4e17-9d5a-ec3987f5f85a

比如填 Bucket 如下示例 其他 3 个一样的填写

3dafb592-1751-42ca-b56f-def6a91ed6a6

最后弄好后的效果

1139e566-683c-48c1-a932-e314c2366f29

编写刷新 CDN 的 python 文件 {#编写刷新-CDN-的-python-文件}

这里以腾讯云CDN为例:需要创建一个 python 文件叫 flush-cdn.py 放在项目的 根目录下 把下面代码中的 https://www.test.com 改成自己的网站域名地址

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | import json from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.cdn.v20180606 import cdn_client, models import os try : secret_id = os.environ.get( 'SECRET_ID' ) secret_key = os.environ.get( 'SECRET_KEY' ) print (secret_key, secret_id) cred = credential.Credential(secret_id, secret_key) httpProfile = HttpProfile() httpProfile.endpoint = "cdn.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cdn_client.CdnClient(cred, "" , clientProfile) req = models.PurgePathCacheRequest() params = { "Paths" : [ "https://www.test.com" ], "FlushType" : "delete" } req.from_json_string(json.dumps(params)) resp = client.PurgePathCache(req) print (resp.to_json_string()) except TencentCloudSDKException as err: print (err) |

编写 Action 文件 {#编写-Action-文件}

项目下点击 Actions 然后点击 set up a workflow yourself

25a14cbc-b5c4-4eb0-9ac5-9a43eca25110

这里需要注意:新建仓库的push分支不叫 main 而是叫 master .需要根据自己仓库去修改成对应的分支

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | name: Deploy to Server on: push: branches: - main jobs: deploy: name: Deploy runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v2 - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: 'latest' extended: true - name: hugo build run: hugo --minify - name: Setup Python uses: actions/setup-python@v4 with: python-version: 3.9 - name: Setup coscmd and sdk run: sudo pip install coscmd - name: Configure coscmd env: SECRET_ID: ${{ secrets.SecretId }} SECRET_KEY: ${{ secrets.SecretKey }} BUCKET: ${{ secrets.Bucket }} REGION: ${{ secrets.Region }} run: coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION - name: Upload to COS run: coscmd upload -rfs --delete public/ / - name: Flush CDN env: SECRET_ID: ${{ secrets.SecretId }} SECRET_KEY: ${{ secrets.SecretKey }} run: | pip install --upgrade tencentcloud-sdk-python python flush-cdn.py |

把上面的代码复制粘贴进去 然后点击 commit changes 后面会谈一个窗口 继续点击 commit changes

8ad716c0-524c-453a-82a6-10f722bedb00

提交成功后会自动执行当前的 Action

26210624-06c9-4136-b4c2-e6c97b93fd21

更新一下本地项目 把刚刚创建的 main.yml 拉下来 这里以使用 webstorm 为例

6a62af93-7d3c-49ed-9367-d5b8797c985d

最后 {#最后}

最后在本地编辑好文章就可以直接上传到 GitHub 就会自动上传到 COS 和刷新 CDN

赞(0)
未经允许不得转载:工具盒子 » Github构建Hugo并同步COS