首页 > 解决方案 > 将 Vue + Go 作为单个二进制文件部署到 Heroku

问题描述

我已将单独的前端和后端存储库合并为一个。我的 gitlab-ci 管道非常适合单独的项目,但我无法让它作为一个单独的项目工作。

单个后端项目的管道:

...

staging:
  image: ruby
  stage: deployment
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY
  environment:
    name: Staging
  only:
    - staging

单个前端项目的管道:

...

build site:
  image: node
  stage: build
  script:
    - yarn install --progress=false
    - yarn build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

staging:
  image: ruby
  stage: deployment
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=$$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY
  environment:
    name: Staging
  only:
    - staging

然后我决定将这两个项目合并在一起。项目结构如下所示:

Project
 |
 +-- backend
 |    
 +-- frontend
 |  |  
 |  +-- dist
 |  |  
 |  +-- package.json
 |    
 +-- Procfile
 |    
 +-- .gitlab-ci.yml

Go 后端在静态路由上为 Vue 前端提供服务。所以我需要在后端之前构建前端,然后在我的后端构建中引用生成的 dist 目录。

这是Procfile:

web: backend

这是我的 gitlab-ci.yml 的当前状态:

...

build frontend:
  image: node
  stage: frontend
  script:
    - cd frontend
    - yarn install --progress=false
    - yarn build
  artifacts:
    name: "frontend"
    expire_in: 1 week
    paths:
      - frontend/dist

build backend:
  image: golang
  stage: backend
  script:
    - cd backend
    - go build
    - ls -la
  artifacts:
    name: "backend"
    expire_in: 1 week
    paths:
      - backend/backend

staging:
  image: ruby
  stage: deployment
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl  --skip_cleanup --provider=heroku --app=$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY
  environment:
    name: Staging
  only:
    - staging

此版本的管道在部署时失败,并出现以下错误:

-----> App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/go.tgz
       More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
 !     Push failed
deploy failed

没有进一步的信息可以检查究竟上传了什么。据我了解,它应该只是不应该失败的二进制文件。

我做错了什么或我的误解是什么?

编辑:更新的分期部分:

staging:
  image: ruby
  stage: deployment
  script:
    - ./detect backend
    - cd backend
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - ls -la ../frontend/dist
    - ls -la
    - dpl --skip_cleanup --provider=heroku --app=$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY
    - ls -la

回报:

$ ./detect backend
Go

$ ls -la
total 30896
drwxrwxrwx 5 root root     4096 Sep 21 11:39 .
drwxrwxrwx 5 root root     4096 Sep 21 11:03 ..
drwxrwxrwx 2 root root     4096 Sep 18 15:03 controllers
-rw-rw-rw- 1 root root     1306 Sep 21 10:11 go.mod
-rw-rw-rw- 1 root root    56556 Sep 21 10:11 go.sum
-rw-rw-rw- 1 root root      251 Sep 18 13:14 main.go
-rwxr-xr-x 1 root root 31527936 Sep 21 11:38 backend
drwxrwxrwx 2 root root     4096 Sep 18 13:14 middleware
drwxrwxrwx 2 root root     4096 Sep 18 13:14 model

...
       Detected the following main packages to install:
            backend
       
-----> Running: go install -v -tags heroku backend 
...

github.com/jackc/pgx/v4
github.com/jackc/pgx/v4/stdlib
 !     Push rejected, failed to compile Go app.
 !     Push failed
deploy failed

标签: herokugitlabgitlab-ci

解决方案


推荐阅读