1

我想通过 ci/cd gitlab 部署我的 laravel 项目,我在部署我的项目时遇到了问题。我正在尝试通过 gitlab CI 将我的 laravel 项目部署到 cpanel。我在这个项目中使用部署程序包,但是当我将提交推送到 gitlab 时,管道失败了。

当我将提交推送到 gitlab 并在管道 错误图像中出现此错误时出现此错误

这是我的 deploy.php 代码

<?php
declare(strict_types=1);
return [
    'default' => 'basic',
    'strategies' => [
    ],
    'hooks' => [
        'start' => [
        ],
        'build' => [
        ],
        'ready' => [
            'artisan:storage:link',
            'artisan:view:clear',
            'artisan:config:cache',
            'artisan:migrate',
            'artisan:horizon:terminate',
        ],
        'done' => [
            'fpm:reload',
        ],
        'success' => [
        ],
        'fail' => [
        ],
        'rollback' => [
            'fpm:reload',
        ],
    ],
    'options' => [
        'application' => env('APP_NAME', 'Laravel'),
        'repository' => 'git@gitlab.com/mygroup/project.git',
        'php_fpm_service' => 'php7.4-fpm',
    ],

    'hosts' => [
        'mywebsite.com' => [
            'deploy_path' => '/home/sfd/public_html/nas',
            'user' => 'deployer',
        ],
    ],

    'localhost' => [

    ],


    'include' => [

    ],

    'custom_deployer_file' => false,

];

这是我的 gitlab-ci.yml 代码

image: edbizarro/gitlab-ci-pipeline-php:7.4

stages:
  - preparation
  - building
  - deploy


composer:
  stage: preparation
  script:
    - php -v
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts --no-suggest
    - cp .env.example .env
    - php artisan key:generate
  artifacts:
    paths:
      - vendor/
      - .env
    expire_in: 1 days
    when: always
  cache:
    paths:
      - vendor/

yarn:
  stage: preparation
  script:
    - yarn --version
    - yarn install --pure-lockfile
  artifacts:
    paths:
      - node_modules/
    expire_in: 1 days
    when: always



.build-production-assets:
  stage: building
  dependencies:
    - composer
    - yarn
  script:
    - cp .env.example .env
    - echo "PUSHER_APP_ID=$PUSHER_LIVE_APP_ID" >> .env
    - echo "PUSHER_APP_KEY=$PUSHER_LIVE_APP_KEY" >> .env
    - echo "PUSHER_APP_SECRET=$PUSHER_LIVE_APP_SECRET" >> .env
    - yarn --version
    - yarn run production --progress false
  artifacts:
    paths:
      - public/css/
      - public/js/
      - public/fonts/
      - public/mix-manifest.json
    expire_in: 1 days
    when: always
  only:
    - dev

.init_ssh_live: &init_ssh_live |
  mkdir -p ~/.ssh
  echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
  chmod 600 ~/.ssh/id_rsa
  [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config



deploy:
  stage: deploy
  script:
    - *init_ssh_live
    - php artisan deploy my-website.com -s upload
  environment:
    name: live
    url: https://my-website.com
  only:
    - dev
4

1 回答 1

1

本期所述

您收到的错误是因为 Deployer 无法通过 SSH 连接到您的服务器。

你得到命令“ rm -f /PATH/.dep/deploy.lock”失败。因为这是部署过程中在您的主机上执行的第一个命令。

因此,请仔细检查“将 SSH 密钥与 GitLab CI/CD 一起使用”文档,看看是否缺少以下步骤:

  • 受密码保护的私钥(需要 ssh 代理)
  • 缺少ssh-keyscan以确保验证远程主机

在切换回您自己的项目之前,请先尝试使此示例项目正常工作。

于 2021-10-25T06:50:55.293 回答