588 字
3 分钟
记一次Github Actions的使用

使用Github Actions自动部署Fuwari博客到Azure static web apps#

之前使用默认的配置都可以完成自动化部署,单身从昨天开始,Github Actions就一直报错,无法部署,查看日志,发现是有一个库的引用有问题。日志如下

5:15:55 AM [vite] Error when evaluating SSR module /github/workspace/astro.config.mjs: failed to import "astro-icon"
|- file:///github/workspace/node_modules/@iconify/tools/lib/svg/index.mjs:1
import cheerio from 'cheerio';
       ^^^^^^^
SyntaxError: The requested module 'cheerio' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async nodeImport (file:///github/workspace/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52928:15)
    at async ssrImport (file:///github/workspace/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52786:16)
    at async eval (/github/workspace/astro.config.mjs:8:31)
    at async instantiateModule (file:///github/workspace/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)

[astro] Unable to load your Astro config

The requested module 'cheerio' does not provide an export named 'default'
  Stack trace:
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async ssrImport (file:///github/workspace/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52786:16)
    at async instantiateModule (file:///github/workspace/node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52844:5)


---End of Oryx build logs---
Oryx has failed to build the solution.

For further information, please visit the Azure Static Web Apps documentation at https://docs.microsoft.com/en-us/azure/static-web-apps/
If you believe this behavior is unexpected, please raise a GitHub issue at https://github.com/azure/static-web-apps/issues/
Exiting

搜了好久都没有解决,区fuwari的issue提问了之后,作者回复说只支持npnm,但是actions默认使用的时npm,所有需要修改actions的workflow文件,在Azure/static-web-apps-deploy@v1工作之前,添加构建步骤,将Azure/static-web-apps-deploy@v1的skip_app_build设置为true,让官方的工作流跳过构建。

    #自定义构建步骤
      - name: Install pnpm
        run: npm install -g pnpm

      - name: Install Dependencies
        run: pnpm install

      - name: Run Build
        run: pnpm build

这里又遇到了新的问题,默认的app_location是/,output_location是dist,但是自定义构建输出的是工作目录的dist文件,构建完成后,官方的工作流就会显示找不到dist的文件,这里需要把app_location更改为自定义构建输出的目录,在日志中可以看到是/home/runner/work/blog/blog/dist

#完整的workflow文件是
name: Azure Static Web Apps CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - main

jobs:
  build_and_deploy_job:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
    runs-on: ubuntu-latest
    name: Build and Deploy Job
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true
          lfs: false
          
      - name: Install pnpm
        run: npm install -g pnpm

      - name: Install Dependencies
        run: pnpm install

      - name: Run Build
        run: pnpm build

      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_SKY_0884F7B00 }}
          repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
          action: "upload"
          ###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
          # For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
          app_location: "/home/runner/work/blog/blog/dist" # App source code path
          skip_app_build: true
          ###### End of Repository/Build Configurations ######

  close_pull_request_job:
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    name: Close Pull Request Job
    steps:
      - name: Close Pull Request
        id: closepullrequest
        uses: Azure/static-web-apps-deploy@v1
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_THANKFUL_SKY_0884F7B00 }}
          action: "close"
记一次Github Actions的使用
https://blog.zzboy.tk/posts/记一次github/
作者
小朱朱
发布于
2024-08-13
许可协议
CC BY-NC-SA 4.0