在使用GitHub Actions时遇到了AWS CDK无法运行的问题,让我陷入困境

写着的内容 (xiě zhe de

尝试使用GitHub Actions和AWS CDK获取部署差异时遇到了错误,花了几天时间解决。
即使在网上搜索,也没有找到解决方案,经过多次尝试和犯错,最终才解决了。
如果有人遇到类似的问题感到困惑,请参考一下。

出现错误的CI文件 de CI

name: CI

on:
  pull_request:
    branches: [main]

jobs:
  diff:
    name: Diff
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18.x'

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-1

      - name: diff
        working-directory: ./
        run : |
          npm ci
          npm run cdk-diff

实际出现的错误。

运行上述CI时会出现此错误。yarn安装失败了吗?会有这种情况吗?
即使通过错误消息搜索也找不到好的解决方案。

#5 [ 2/10] RUN npm install --global yarn@1.22.5
#5 0.214 exec /bin/sh: exec format error
#5 ERROR: process "/bin/sh -c npm install --global yarn@1.22.5" did not complete successfully: exit code: 1
------
 > [ 2/10] RUN npm install --global yarn@1.22.5:
0.214 exec /bin/sh: exec format error
------
Dockerfile:7
--------------------
   5 |     
   6 |     # Install yarn
   7 | >>> RUN npm install --global yarn@1.22.5
   8 |     
   9 |     # Install pnpm
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install --global yarn@1.22.5" did not complete successfully: exit code: 1
/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/core/lib/private/asset-staging.js:2
`).map((line,idx)=>`${idx===0?firstLine:padding}${line}`)};const reason=proc.signal!=null?`signal ${proc.signal}`:`status ${proc.status}`,command=[prog,...args.map(arg=>/[^a-z0-9_-]/i.test(arg)?JSON.stringify(arg):arg)].join(" ");throw new Error([`${prog} exited with ${reason}`,...prependLines("--> STDOUT:  ",proc.stdout)??[],...prependLines("--> STDERR:  ",proc.stderr)??[],`--> Command: ${command}`].join(`
                                                                                                                                                                                                                                            ^
Error: docker exited with status 1
--> Command: docker build -t cdk-e4d4c769de205f6558dd8adc010a0a517a8ae893f27262ab6dbe3fed349afb5e --platform "linux/arm64" --build-arg "IMAGE=public.ecr.aws/sam/build-nodejs18.x" --build-arg "ESBUILD_VERSION=0" "/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib"
    at dockerExec (/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/core/lib/private/asset-staging.js:2:237)
    at Function.fromBuild (/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/core/lib/bundling.js:1:4364)
    at new Bundling (/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:3393)
    at Function.bundle (/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.js:1:605)
    at new NodejsFunction (/home/runner/work/hoge/hoge/node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:1273)
    at new HogeCdkStack (/home/runner/work/hoge/hoge/lib/hoge-cdk-stack.ts:114:28)
    at Object.<anonymous> (/home/runner/work/hoge/hoge/bin/hoge-cdk.ts:23:23)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module.m._compile (/home/runner/work/hoge/hoge/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
[06:13:43] Notices refreshed
[06:13:43] Failed to store notices in the cache: Error: ENOENT: no such file or directory, open '/home/runner/.cdk/cache/notices.json'

Subprocess exited with error 1
[06:13:43] Error: Subprocess exited with error 1
    at ChildProcess.<anonymous> (/opt/hostedtoolcache/node/18.17.1/x64/lib/node_modules/aws-cdk/lib/index.js:445:50448)
    at ChildProcess.emit (node:events:514:28)
    at ChildProcess.emit (node:domain:489:12)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Error: Process completed with exit code 1.

解决方案 cè àn)

将此添加到 ci.yml 文件中。

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: linux/arm64
name: CI

on:
  pull_request:
    branches: [main]

jobs:
  diff:
    name: Diff
    runs-on: ubuntu-latest

    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: linux/arm64
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18.x'

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ap-northeast-1

      - name: diff
        working-directory: ./
        run : |
          npm ci
          npm run cdk-diff

终于通过了CI,我为此纠结了三四天,但问题已经解决了。

bannerAds