与用于Armhf-v7的GitlabCI交叉编译golang应用程序时出现“执行格式错误”

时间:2019-06-11 09:40:14

标签: docker go cross-compiling gitlab-ci

我正在尝试使用GitlabCI来为ArmV7l构建Go二进制文件。

一堆失败的意图后,我可以使用此.gitlab-ci.yml

成功地对其进行编译
image: golang
variables:
  PACKAGE_PATH: /go/src/gitlab.com/company/edge_to_bc
  PACKAGE_API_NAME: registry.gitlab.com/company/edge_to_bc
  REGISTRY_URL: https://registry.gitlab.com
  DOCKER_DRIVER: overlay
stages:
  - build
  - publish

# A hack to make Golang-in-Gitlab happy
.anchors:
  - &inject-gopath
      mkdir -p $(dirname ${PACKAGE_PATH})
      && ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH}
      && cd ${PACKAGE_PATH}


# ==================== TEST ====================
build_binary:
  stage: build
  before_script:
    - *inject-gopath
    - dpkg --add-architecture armhf
    - apt update && apt-get install -y gcc-arm-linux-gnueabihf libltdl-dev:armhf
  script:
    - CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 go build -v -o ./release/edge_to_bc
  artifacts:
    name: "binary-$CI_PIPELINE_ID"
    paths:
      - release/edge_to_bc
    expire_in: 1 hour

docker-push:
  stage: publish
  before_script:
    - *inject-gopath
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY_URL
  image: docker:stable
  services:
    - docker:dind
  script:
    - docker build -t $PACKAGE_API_NAME:$CI_BUILD_ID  .
    - docker build -t $PACKAGE_API_NAME:latest  .
    - docker push $PACKAGE_API_NAME:$CI_BUILD_ID
    - docker push $PACKAGE_API_NAME:latest
  dependencies:
    - build_binary

这是我的Dockerfile。

FROM alpine:3.7

RUN apk --no-cache add ca-certificates libtool

WORKDIR /sunclient/

COPY ./release/edge_to_bc /sunclient/
EXPOSE 5555

CMD [ "./edge_to_bc" ]

在ARM板上运行它时,我得到:

[root@artik system]# docker run -it  registry.gitlab.com/company/edge_to_bc:latest bash
standard_init_linux.go:207: exec user process caused "exec format error"

当我想用bash调试它时:

[root@artik system]# docker run registry.gitlab.com/company/edge_to_bc:latest
standard_init_linux.go:207: exec user process caused "no such file or directory"

我该怎么办?

0 个答案:

没有答案