Laravel |使用gitlab ci进行部署和自动部署

时间:2019-03-18 22:10:07

标签: php laravel continuous-integration gitlab

我想设置自动部署到我的生产服务器。

这是我来自gitlab ci的日志->

Running with gitlab-runner 11.9.0-rc2 (227934c0)
  on docker-auto-scale fa6cab46
Using Docker executor with image lorisleiva/laravel-docker:latest ...
Pulling docker image lorisleiva/laravel-docker:latest ...
Using docker image sha256:4bd5ecacba7b0f46950944f376647090071a70a7b1ffa0eacb492719bd476c6b for lorisleiva/laravel-docker:latest ...
Running on runner-fa6cab46-project-11286864-concurrent-0 via runner-fa6cab46-srm-1552945620-f480ce3e...
Initialized empty Git repository in /builds/Woblex/web/.git/
Fetching changes...
Created fresh repository.
From https://gitlab.com/Woblex/web
 * [new branch]      master     -> origin/master
Checking out ce51a64e as master...

Skipping Git submodules setup
Downloading artifacts for composer (179849020)...
Downloading artifacts from coordinator... ok        id=179849020 responseStatus=200 OK token=zy8-CGce
Downloading artifacts for npm (179849021)...
Downloading artifacts from coordinator... ok        id=179849021 responseStatus=200 OK token=NvUWyzkg
$ eval $(ssh-agent -s) # collapsed multi-line command
Agent pid 11
Identity added: (stdin) (git@gitlab.com)
$ find . -type f -not -path "./vendor/*" -exec chmod 664 {} \;     # collapsed multi-line command
$ whoami
root
$ php artisan deploy woblex.cz -s upload
✈︎ Deploying HEAD on woblex.cz with upload strategy
➤ Executing task deploy:prepare
✔ Ok
➤ Executing task deploy:lock
✔ Ok
➤ Executing task deploy:release
✔ Ok
➤ Executing task deploy:update_code
➤ Executing task deploy:failed
✔ Ok
➤ Executing task deploy:unlock
✔ Ok

In Client.php line 99:

  The command "cd /var/www/dev.woblex.cz && (/usr/bin/git clone   --recursive  
    git@gitlab.com:Woblex/web.git /var/www/dev.woblex.cz/releases/1 2>&1)" fa  
  iled.                                                                        

  Exit Code: 128 (Invalid exit argument)                                       

  Host Name: woblex.cz                                                         

  ================                                                             
  Warning: Identity file /home/gitlab/.ssh/id_rsa not accessible: No such fil  
  e or directory.                                                              


deploy [-p|--parallel] [-l|--limit LIMIT] [--no-hooks] [--log LOG] [--roles ROLES] [--hosts HOSTS] [-o|--option OPTION] [--] [<stage>]


In Process.php line 239:

  The command "vendor/bin/dep --file=vendor/lorisleiva/laravel-deployer/.buil  
  d/deploy.php deploy 'woblex.cz'" failed.                                     

  Exit Code: 128(Invalid exit argument)                                        

  Working directory: /builds/Woblex/web                                        

  Output:                                                                      
  ================                                                             
  ✈︎ Deploying HEAD on woblex.cz with upload strategy                          
  ➤ Executing task deploy:prepare                                              
  ✔ Ok                                                                         
  ➤ Executing task deploy:lock                                                 
  ✔ Ok                                                                         
  ➤ Executing task deploy:release                                              
  ✔ Ok                                                                         
  ➤ Executing task deploy:update_code                                          
  ➤ Executing task deploy:failed                                               
  ✔ Ok                                                                         
  ➤ Executing task deploy:unlock                                               
  ✔ Ok                                                                         


  Error Output:                                                                
  ================                                                             

  In Client.php line 99:                                                       


    The command "cd /var/www/dev.woblex.cz && (/usr/bin/git clone   --recursi  
  ve                                                                           
      git@gitlab.com:Woblex/web.git /var/www/dev.woblex.cz/releases/1 2>&1)"   
  fa                                                                           
    iled.                                                                      



    Exit Code: 128 (Invalid exit argument)                                     



    Host Name: woblex.cz                                                       



    ================                                                           

    Warning: Identity file /home/gitlab/.ssh/id_rsa not accessible: No such f  
  il                                                                           
    e or directory.                                                            




  deploy [-p|--parallel] [-l|--limit LIMIT] [--no-hooks] [--log LOG] [--roles  
   ROLES] [--hosts HOSTS] [-o|--option OPTION] [--] [<stage>]                  


ERROR: Job failed: exit code 1

我有我的deploy.php配置->

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default deployment strategy
    |--------------------------------------------------------------------------
    |
    | This option defines which deployment strategy to use by default on all
    | of your hosts. Laravel Deployer provides some strategies out-of-box
    | for you to choose from explained in detail in the documentation.
    |
    | Supported: 'basic', 'firstdeploy', 'local', 'pull'.
    |
    */

    'default' => 'basic',

    /*
    |--------------------------------------------------------------------------
    | Custom deployment strategies
    |--------------------------------------------------------------------------
    |
    | Here, you can easily set up new custom strategies as a list of tasks.
    | Any key of this array are supported in the `default` option above.
    | Any key matching Laravel Deployer's strategies overrides them.
    |
    */

    'strategies' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Hooks
    |--------------------------------------------------------------------------
    |
    | Hooks let you customize your deployments conveniently by pushing tasks 
    | into strategic places of your deployment flow. Each of the official
    | strategies invoke hooks in different ways to implement their logic.
    |
    */

    'hooks' => [
        // Right before we start deploying.
        'start' => [
            //
        ],

        // Code and composer vendors are ready but nothing is built.
        'build' => [
            //
        ],

        // Deployment is done but not live yet (before symlink)
        'ready' => [
            'artisan:storage:link',
            'artisan:view:clear',
            'artisan:cache:clear',
            'artisan:config:cache',
            'artisan:migrate',
            'artisan:horizon:terminate',
        ],

        // Deployment is done and live
        'done' => [
            //
        ],

        // Deployment succeeded.
        'success' => [
            //
        ],

        // Deployment failed.
        'fail' => [
            //
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Deployment options
    |--------------------------------------------------------------------------
    |
    | Options follow a simple key/value structure and are used within tasks
    | to make them more configurable and reusable. You can use options to
    | configure existing tasks or to use within your own custom tasks.
    |
    */

    'options' => [
        'application' => env('APP_NAME', 'Laravel'),
        'repository' => 'git@gitlab.com:Woblex/web.git',
        'git_tty' => false,
    ],

    /*
    |--------------------------------------------------------------------------
    | Hosts
    |--------------------------------------------------------------------------
    |
    | Here, you can define any domain or subdomain you want to deploy to.
    | You can provide them with roles and stages to filter them during
    | deployment. Read more about how to configure them in the docs.
    |
    */

    'hosts' => [
        'woblex.cz' => [
            'deploy_path' => '/var/www/dev.woblex.cz',
            'user' => 'gitlab',
            'identityFile' => '/home/gitlab/.ssh/id_rsa',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Localhost
    |--------------------------------------------------------------------------
    |
    | This localhost option give you the ability to deploy directly on your
    | local machine, without needing any SSH connection. You can use the
    | same configurations used by hosts to configure your localhost.
    |
    */

    'localhost' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Include additional Deployer recipes
    |--------------------------------------------------------------------------
    |
    | Here, you can add any third party recipes to provide additional tasks, 
    | options and strategies. Therefore, it also allows you to create and
    | include your own recipes to define more complex deployment flows.
    |
    */

    'include' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Use a custom Deployer file
    |--------------------------------------------------------------------------
    |
    | If you know what you are doing and want to take complete control over
    | Deployer's file, you can provide its path here. Note that, without
    | this configuration file, the root's deployer file will be used.
    |
    */

    'custom_deployer_file' => false,

];

我认为从laravel部署程序连接到我的ubuntu服务器的用户有问题,因为我在config中将用户名设置为“ gitlab”,但是如果部署程序使用此用户连接到ubuntu服务器,则可以看到文件/ home /gitlab/.ssh/id_rsa。有人可以帮我吗?非常感谢!

编辑:这是我的gitlab-ci.yml

image: lorisleiva/laravel-docker:latest

.init_ssh: &init_ssh |
  eval $(ssh-agent -s)
  echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  mkdir -p ~/.ssh
  chmod 700 ~/.ssh
  [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

# Replace the last line with the following lines if you'd rather
# leave StrictHostKeyChecking enabled (replace yourdomain.com):
#
#  ssh-keyscan yourdomain.com >> ~/.ssh/known_hosts
#  chmod 644 ~/.ssh/known_hosts

.change_file_permissions: &change_file_permissions |
  find . -type f -not -path "./vendor/*" -exec chmod 664 {} \;    
  find . -type d -not -path "./vendor/*" -exec chmod 775 {} \;

composer:
  stage: build
  cache:
    key: ${CI_COMMIT_REF_SLUG}-composer
    paths:
      - vendor/
  script:
      - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
      - cp .env.example .env
      - php artisan key:generate
  artifacts:
    expire_in: 1 month
    paths:
      - vendor/
      - .env

npm:
  stage: build
  cache:
    key: ${CI_COMMIT_REF_SLUG}-npm
    paths:
      - node_modules/
  script:
      - npm install
      - npm run production
  artifacts:
    expire_in: 1 month
    paths:
      - node_modules/
      - public/css/
      - public/js/

#codestyle:
#  stage: test
#  dependencies: []
#  script:
#    - phpcs --standard=PSR2 --extensions=php --ignore=app/Support/helpers.php app

phpunit:
  stage: test
  dependencies:
    - composer
  script:
    - phpunit --coverage-text --colors=never

staging:
  stage: deploy
  script:
    - *init_ssh
    - *change_file_permissions
    - whoami
    - php artisan deploy woblex.cz -s upload
  environment:
    name: staging
    url: http://www.dev.woblex.cz
  only:
    - master

production:
  stage: deploy
  script:
    - *init_ssh
    - *change_file_permissions
    - php artisan deploy new.woblex.cz -s upload
  environment:
    name: production
    url: https://www.new.woblex.cz
  when: manual
  only:
   - master

0 个答案:

没有答案