如何通过 Github Actions runner 在 CDK 上构建 ARM64 Docker 镜像

6

我正在尝试在Github Actions工作流中部署我的Fargate服务(使用ARM64 Graviton处理器)。然而,当我的Docker尝试构建镜像时,RUN语句失败:

Warning: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
 ---> Running in 025f2d97f759
exec /bin/sh: exec format error
Removing intermediate container f2c3858b0496
The command '/bin/sh -c apt-get update' returned a non-zero code: 1
 ---> 02ceb19d6208
[66%] fail: docker build --tag cdkasset-7e1b96b9aea331ad2efd7e6fbf6f075033eca830469ea4ab8d57bf4a8eeb86cd --file Dockerfile --platform linux/arm64 . exited with error code 1: The command '/bin/sh -c apt-get update' returned a non-zero code: 1

在 AMD64 Github Actions runner 上构建基于 ARM64 的 Docker 镜像是否可行?我已经尝试在所有可能的地方指定平台架构,但构建仍然失败。

我的 Github Actions 工作流程:

name: Deploy

on:
  push:
    branches:
      - main

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "npm"

      - run: npm ci

      - uses: docker/setup-buildx-action@v2

      - uses: aws-actions/configure-aws-credentials@v2
        with:
          role-to-assume: arn:aws:iam::123456789098:role/cicd-role
          aws-region: us-west-2

      - run: npx cdk deploy --all --require-approval never
        env:
          DOCKER_BUILDKIT: 1

我的 Dockerfile:

# We create our own image rather than using cloudflare/cloudfared because that is an ultra 
# slimmed-down image that does not have a shell. We need the shell to be present to call
# the image with a command like `["sh", "-c", "cloudflared tunnel run --token $TOKEN"] wherin
# we can use available environment variables which represent passed-in values from AWS Secrets
# Manager.
FROM --platform=linux/arm64 debian:stretch-slim

RUN apt-get update
RUN apt-get dist-upgrade --yes
RUN apt-get install curl --yes

# Install cloudflared
RUN curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb
RUN dpkg -i cloudflared.deb
RUN rm cloudflared.deb

我的CDK构建中构建图像的部分:
import {
  aws_ecr_assets,
  aws_ecs,
} from "aws-cdk-lib";
import { Construct } from "constructs";

export class Tunnel extends Construct {
  constructor(scope: Construct, id: string, props: TunnelProps) {
    super(scope, id);

    const taskDefinition = new aws_ecs.FargateTaskDefinition(
      this,
      "TunnelDefinition",
      {
        runtimePlatform: {
          cpuArchitecture: aws_ecs.CpuArchitecture.ARM64,
        },
      }
    );

    taskDefinition.addContainer("container", {
      image: aws_ecs.AssetImage.fromAsset("./", {
        platform: aws_ecr_assets.Platform.LINUX_ARM64,
      }),
    });

    new aws_ecs.FargateService(this, "Tunnel", {
      serviceName: "cloudflare-tunnel",
      cluster: props.cluster,
      taskDefinition,
    });
  }
}

export interface TunnelProps {
  cluster: aws_ecs.ICluster;
  loadBalancerDnsName: string;
}

谢谢。


1
注意:GitHub Actions将在2024年第一季度引入基于Arm的托管运行器。目前已经提供了私有测试版(2023年第四季度)。 - undefined
1个回答

4
docker/setup-qemu-action添加到GitHub Actions工作流程中即可解决问题:
name: Deploy

on:
  push:
    branches:
      - main

permissions:
  id-token: write
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "npm"

      - run: npm ci

      - uses: docker/setup-qemu-action@v2
        with:
          platforms: 'arm64,arm'

      - uses: docker/setup-buildx-action@v2

      - uses: aws-actions/configure-aws-credentials@v2
        with:
          role-to-assume: arn:aws:iam::123456789098:role/cicd-role
          aws-region: us-west-2

      - run: npx cdk deploy --all --require-approval never
        env:
          DOCKER_BUILDKIT: 1

值得注意的是,在运行 CDK 时需要在环境中设置 DOCKER_BUILDKIT: 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接