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

r7xajy2e  于 2023-04-11  发布在  Docker
关注(0)|答案(1)|浏览(368)

我试图在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运行器上构建基于ARM64的Docker镜像?我已经尝试在每个可能的地方指定平台架构,但构建仍然失败。
我的Github操作工作流:

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;
}

谢谢。

yyyllmsg

yyyllmsg1#

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

相关问题