開発

CI/CD引継ぎ日誌。GitLab Laravel環境のCI/CD オートスケール 対応

CI/CDを引き継ぐことになったのでまとめる🐱

 

前提準備

  • GitLabRunnerを立ててGitLabのCI/CDにたてておく(t2.large)
  • dev, stg, prodのAutoScalingGroup, 起動設定作っておく
  • GitLabに環境変数を設定する

GitLabのCI/CDのVariableで定義するもの

SAMPLEAPP_SSH_KEY

-----BEGIN EC PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END EC PRIVATE KEY-----

 

AWS_CONFIG

[default]\nregion = ap-northeast-1\noutput = text

AWS_CREDENTIALS

[default]\naws_access_key_id = AKIAJxxxxxx\naws_secret_access_key = aaaabbbbccccxxxxxxxxxx

GITLAB_ACCESS_TOKEN

xxxxxxxxx

FIREBASE_API_KEY

xxxxxxxxx

DEV_CI_SKIP

true

STG_CI_SKIP

true

PROD_CI_SKIP

true

 

重要なファイル

.gitlab-ci.yml

  • CI/CDで中心となるファイル
  • CDの中の一部の処理でEnboy.bleade.phpを利用する

 

Envoy.blade.php

  • artisan系の処理などlaravelのセットアップを行う

https://readouble.com/laravel/7.x/ja/envoy.html

 

 

parallelオプションで並列化が可能で処理が早くなる。// GitLabのCI/CDのtimeout時間に間に合わせる

@task('checkout', ['parallel' => true])

@taskの時は[‘parallel’ => true]をつけておきたい

 

 

 

@story('deploy_dev', ['on' => ['web']])
    checkout
    setup_composer
    artisan_cache_clear
    setup_npm
    upload_s3
    migrate
    refresh_data
@endstory

こうEnvoy.blade.phpで書いたら、gitlab-ci.ymlにて

envoy run deploy_dev --releases_dir=/var/www/develop-project --release_type=develop

envoy run {story名}という形で実行される。

–でつけたものは変数としてEnvoy.blade.phpに渡される。

実際にその変数を利用している箇所

@task('refresh_data', ['parallel' => true])
    // Seederの実行
    /usr/bin/php {{ $releases_dir }}/artisan db:seed --class=xxxSeeder
    /usr/bin/php {{ $releases_dir }}/artisan db:seed --class=yyySeeder
    // CloudFrontのキャッシュ削除
    @if ($release_type === 'develop')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front E1CKNxxxxxxxxx
    @elseif ($release_type === 'staging')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front E327Kyyyyyyyyy
    @elseif ($release_type === 'product')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front EJW3Hzzzzzzzzz
    @endif
    /usr/bin/php {{ $releases_dir }}/artisan cache:clear
@endtaskへ

変数の値によって処理を変えています。

 

 

Envoy.blade.php

@setup
    $sync_s3_dir_names = [
      'images/account',
      // ●(略)
      'images/profile'
    ];
    $output = shell_exec('aws ec2 describe-instances --filters Name=tag:Name,Values=sample_app-production Name=instance-state-code,Values=16 --query "Reservations[*].Instances[*].PublicIpAddress"');
    $output = trim($output);
    $outputs = explode("\n", $output);
@endsetup

@servers(['web' => ['www-data@111.111.111.111'], 'web_slave' => ['www-data@222.222.222.222'], 'web_stg' => ['www-data@333.333.333.333'], 'web_stg_slave' => ['www-data@444.444.444.444'], 'web_tiler' => ['www-data@555.555.555.555'], 'web_prod' => ['www-data@666.666.666.666'], 'web_prod_slave' => ['www-data@777.777.777.777']])

@story('deploy', ['on' => ['web']])
    checkout
    setup_composer
    artisan_cache_clear
    setup_npm
    upload_s3
    migrate
    refresh_data
@endstory

@story('deploy_slave', ['on' => ['web_slave']])
    checkout
    setup_composer
    artisan_cache_clear
@endstory

@story('deploy_stg', ['on' => ['web_stg']])
    checkout
    setup_composer
    artisan_cache_clear
    setup_npm
    upload_s3
    migrate
    refresh_data
@endstory

@story('deploy_stg_slave', ['on' => ['web_stg_slave']])
    checkout
    setup_composer
    artisan_cache_clear
@endstory

@story('deploy_prod', ['on' => ['web_prod']])
    checkout
    setup_composer
    artisan_cache_clear
    setup_npm
    upload_s3
    migrate
    refresh_data
@endstory

@story('deploy_prod_slave', ['on' => ['web_prod_slave']])
    checkout
    setup_composer
    artisan_cache_clear
@endstory


@task('checkout', ['parallel' => true])
    /usr/bin/git -C {{ $releases_dir }} checkout .
    /usr/bin/git -C {{ $releases_dir }} clean -f
    /usr/bin/git -C {{ $releases_dir }} pull
@endtask

@task('setup_composer', ['parallel' => true])
    /usr/bin/composer --working-dir={{ $releases_dir }} install
@endtask

@task('setup_npm', ['parallel' => true])
    /usr/bin/npm install --prefix {{ $releases_dir }}
    @if ($release_type === 'develop')
        /usr/bin/npm run dev --prefix {{ $releases_dir }}
    @elseif ($release_type === 'staging')
        /usr/bin/npm run stg --prefix {{ $releases_dir }}
    @elseif ($release_type === 'product')
        /usr/bin/npm run prod --prefix {{ $releases_dir }}
    @endif
@endtask

@task('upload_s3', ['parallel' => true])
  /usr/local/bin/aws s3 cp {{ $releases_dir }}/public/mix-manifest.json s3://{{ $release_type }}.sample_app/
  /usr/local/bin/aws s3 sync {{ $releases_dir }}/public/profile s3://{{ $release_type }}.sample_app/profile/ --delete

  @if ($release_type === 'develop')
    @foreach($sync_s3_dir_names as $sync_dir)
      /usr/local/bin/aws s3 sync {{ $releases_dir }}/public/{{ $sync_dir }}/ s3://{{ $release_type }}.sample_app/{{ $sync_dir }}/ --delete
    @endforeach
  @elseif ($release_type === 'staging')
    @foreach($sync_s3_dir_names as $sync_dir)
      /usr/local/bin/aws s3 sync s3://develop.sample_app/{{ $sync_dir }}/ s3://{{ $release_type }}.sample_app/{{ $sync_dir }}/ --delete
    @endforeach
  @elseif ($release_type === 'product')
    @foreach($sync_s3_dir_names as $sync_dir)
      /usr/local/bin/aws s3 sync s3://staging.sample_app/{{ $sync_dir }}/ s3://{{ $release_type }}.sample_app/{{ $sync_dir }}/ --delete
    @endforeach
  @endif
@endtask

@task('migrate', ['parallel' => true])
    /usr/bin/php {{ $releases_dir }}/artisan migrate
@endtask

@task('migrate_fresh', ['parallel' => true])
    /usr/bin/php {{ $releases_dir }}/artisan migrate:fresh --seed
@endtask

@task('artisan_cache_clear', ['parallel' => true])
    /usr/bin/php {{ $releases_dir }}/artisan view:clear
    /usr/bin/php {{ $releases_dir }}/artisan cache:clear
    /usr/bin/php {{ $releases_dir }}/artisan config:clear
@endtask

@task('artisan_queue_restart', ['parallel' => true])
    /usr/bin/php {{ $releases_dir }}/artisan queue:restart
@endtask

@task('db_seed', ['parallel' => true])
    /usr/bin/php {{ $releases_dir }}/artisan db:seed
@endtask

@task('refresh_data', ['parallel' => true])
    // Seederの実行
    /usr/bin/php {{ $releases_dir }}/artisan db:seed --class=xxxSeeder
    /usr/bin/php {{ $releases_dir }}/artisan db:seed --class=yyySeeder
    // CloudFrontのキャッシュ削除
    @if ($release_type === 'develop')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front E1CKNxxxxxxxxx
    @elseif ($release_type === 'staging')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front E327Kyyyyyyyyy
    @elseif ($release_type === 'product')
        /usr/bin/php {{ $releases_dir }}/artisan aws:clear_cache_cloud_front EJW3Hzzzzzzzzz
    @endif
    /usr/bin/php {{ $releases_dir }}/artisan cache:clear
@endtask

 

 

gitlab-ci.yml

image: registry.gitlab.com/yuu3-sampleapp/beta-project:laravel-dusk-ci

services:
  - mysql:5.7

variables:
  MYSQL_DATABASE: homestead
  DB_USERNAME: homestead
  MYSQL_ROOT_PASSWORD: secret
  MYSQL_USER: homestead
  MYSQL_PASSWORD: secret
  DB_HOST: mysql

.dev_aws_valiables: &dev_aws_valiables
  variables:
    COOLDOWN_TIME: "180"
    EC2_MAIN_MASTER_NAME: "sampleapp-dev-master-main"
    AMI_NAME_PREFIX: "sampleapp-dev"
    LAUNCH_TEMPLATE_NAME: "SampleappDevMasterAutoScalingPolicy"
    LAUNCH_TEMPLATE_ID: "lt-xxxxxxx123"
    LAUNCH_TEMPLATE_REGION: "ap-northeast-1"
    AUTO_SCALING_MASTER_NAME: "SampleappDevMasterAutoScaling"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_END: "1"
    AUTO_SCALING_SLAVE_NAME: "SampleappDevSlaveAutoScaling"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_END: "1"
    AUTO_SCALING_SLAVE_MIN_START: "1"
    AUTO_SCALING_SLAVE_MIN_END: "1"
    ALB_LISTENER_HTTP: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappDevLB/yyyyyyy/345345rew"
    ALB_LISTENER_HTTPS: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappDevLB/yyyyyy/567567tre"
    TARGET_GROUP_MASTER_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappDevTG/789789123"
    TARGET_GROUP_SLAVE_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappDev-Slave-TG/789789456"

.stg_aws_valiables: &stg_aws_valiables
  variables:
    COOLDOWN_TIME: "180"
    EC2_MAIN_MASTER_NAME: "sampleapp-stg-master-main"
    AMI_NAME_PREFIX: "sampleapp-stg"
    LAUNCH_TEMPLATE_NAME: "SampleappStgMasterAutoScalingPolicy"
    LAUNCH_TEMPLATE_ID: "lt-yyyyyy456"
    LAUNCH_TEMPLATE_REGION: "ap-northeast-1"
    AUTO_SCALING_MASTER_NAME: "SampleappStgMasterAutoScaling"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_END: "3"
    AUTO_SCALING_SLAVE_NAME: "SampleappStgSlaveAutoScaling"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_END: "3"
    AUTO_SCALING_SLAVE_MIN_START: "1"
    AUTO_SCALING_SLAVE_MIN_END: "2"
    ALB_LISTENER_HTTP: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappSTGLB/xxxxxx/123123abctyu"
    ALB_LISTENER_HTTPS: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappSTGLB/xxxxxx/123123hjkr"
    TARGET_GROUP_MASTER_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappStg-Master-TG/456456uio"
    TARGET_GROUP_SLAVE_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappStg-Slave-TG/4456456tyue"

.prod_aws_valiables: &prod_aws_valiables
  variables:
    COOLDOWN_TIME: "180"
    EC2_MAIN_MASTER_NAME: "sampleapp-prod-master-main"
    AMI_NAME_PREFIX: "sampleapp-prod"
    LAUNCH_TEMPLATE_NAME: "SampleappProdMasterAutoScalingPolicy"
    LAUNCH_TEMPLATE_ID: "lt-zzzzzz789"
    LAUNCH_TEMPLATE_REGION: "ap-northeast-1"
    AUTO_SCALING_MASTER_NAME: "SampleappProdMasterAutoScaling"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_MASTER_DESIRED_CAPACITY_END: "20"
    AUTO_SCALING_SLAVE_NAME: "SampleappProdSlaveAutoScaling"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_START: "1"
    AUTO_SCALING_SLAVE_DESIRED_CAPACITY_END: "20"
    AUTO_SCALING_SLAVE_MIN_START: "1"
    AUTO_SCALING_SLAVE_MIN_END: "20"
    ALB_LISTENER_HTTP: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappProdLB/zzzzzz/789789aaa"
    ALB_LISTENER_HTTPS: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:listener/app/SampleappProdLB/zzzzzz/789789bbb"
    TARGET_GROUP_MASTER_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappProd-Master-TG/999999ccc"
    TARGET_GROUP_SLAVE_ARN: "arn:aws:elasticloadbalancing:ap-northeast-1:0312312399977:targetgroup/SampleappProd-Slave-TG/999999ddd"

.aws_cli_setup: &aws_cli_setup
  before_script:
    - IP=`curl https://checkip.amazonaws.com`
    - echo $IP
    - mkdir -p ~/.aws
    - echo -e $AWS_CREDENTIALS > ~/.aws/credentials
    - chmod 600 ~/.aws/credentials
    - echo -e $AWS_CONFIG > ~/.aws/config
    - chmod 600 ~/.aws/config
    - curl "https://bootstrap.pypa.io/pip/2.7/get-pip.py" -o "get-pip.py"
    - python get-pip.py
    - pip install awscli

.update_launch_template: &update_launch_template
  script:
    - date +%Y%m%d%H%M > run_timestamp
    - aws ec2 describe-instances --filters Name=tag:Name,Values=${EC2_MAIN_MASTER_NAME} Name=instance-state-code,Values=16 --query "Reservations[*].Instances[*].[InstanceId]" --output=text > instance_id
    - head -1 instance_id
    - aws ec2 create-image --instance-id `head -1 instance_id` --name ${AMI_NAME_PREFIX}-`head -1 run_timestamp` --no-reboot > ami_id
    - head -1 ami_id
    - aws ec2 describe-launch-templates --filters Name=launch-template-name,Values=${LAUNCH_TEMPLATE_NAME} --query "LaunchTemplates[*].LatestVersionNumber" --output=text > launch_template_latest_version
    - head -1 launch_template_latest_version
    - aws ec2 create-launch-template-version --launch-template-id ${LAUNCH_TEMPLATE_ID} --source `head -1 launch_template_latest_version` --launch-template-data '{"ImageId":"'`head -1 ami_id`'"}' --query "LaunchTemplateVersion.VersionNumber" --output=text > created_launch_template_version
    - aws ec2 modify-launch-template --launch-template-id ${LAUNCH_TEMPLATE_ID} --default-version `head -1 created_launch_template_version` --region ${LAUNCH_TEMPLATE_REGION}
.refresh_master_auto_scaling: &refresh_master_auto_scaling
  script:
    - aws autoscaling set-desired-capacity --auto-scaling-group-name ${AUTO_SCALING_MASTER_NAME} --desired-capacity ${AUTO_SCALING_MASTER_DESIRED_CAPACITY_START}
    - sleep ${COOLDOWN_TIME}
    - aws autoscaling set-desired-capacity --auto-scaling-group-name ${AUTO_SCALING_MASTER_NAME} --desired-capacity ${AUTO_SCALING_MASTER_DESIRED_CAPACITY_END}
    - sleep ${COOLDOWN_TIME}
.refresh_slave_auto_scaling: &refresh_slave_auto_scaling
  script:
    - aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${AUTO_SCALING_SLAVE_NAME} --min-size ${AUTO_SCALING_SLAVE_MIN_START} --desired-capacity ${AUTO_SCALING_SLAVE_DESIRED_CAPACITY_START}
    - sleep ${COOLDOWN_TIME}
    - aws autoscaling update-auto-scaling-group --auto-scaling-group-name ${AUTO_SCALING_SLAVE_NAME} --min-size ${AUTO_SCALING_SLAVE_MIN_END} --desired-capacity ${AUTO_SCALING_SLAVE_DESIRED_CAPACITY_END}
    - sleep ${COOLDOWN_TIME}
.change_alb_slave_to_master: &change_alb_slave_to_master
  script:
    - aws elbv2 modify-listener --listener-arn ${ALB_LISTENER_HTTP} --default-actions Type=forward,TargetGroupArn=${TARGET_GROUP_MASTER_ARN}
    - aws elbv2 modify-listener --listener-arn ${ALB_LISTENER_HTTPS} --default-actions Type=forward,TargetGroupArn=${TARGET_GROUP_MASTER_ARN}
.change_alb_master_to_slave: &change_alb_master_to_slave
  script:
    - aws elbv2 modify-listener --listener-arn ${ALB_LISTENER_HTTP} --default-actions Type=forward,TargetGroupArn=${TARGET_GROUP_SLAVE_ARN}
    - aws elbv2 modify-listener --listener-arn ${ALB_LISTENER_HTTPS} --default-actions Type=forward,TargetGroupArn=${TARGET_GROUP_SLAVE_ARN}
.refresh_master_auto_scaling_after_slave: &refresh_master_auto_scaling_after_slave
  script:
    - aws autoscaling set-desired-capacity --auto-scaling-group-name ${AUTO_SCALING_MASTER_NAME} --desired-capacity ${AUTO_SCALING_MASTER_DESIRED_CAPACITY_START}
    - sleep ${COOLDOWN_TIME}
# ここの箇所が実行される onlyでブランチが指定される
stages:
  - test
  - deploy
  - update_launch_template
  - refresh_master_auto_scaling
  - change_alb_slave_to_master
  - deploy_slave
  - refresh_slave_auto_scaling
  - change_alb_master_to_slave
  - refresh_master_auto_scaling_after_slave

# dev aws deploy
dev_update_launch_template:
  stage: update_launch_template
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *update_launch_template
  only:
    - master
dev_refresh_master_auto_scaling:
  stage: refresh_master_auto_scaling
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling
  only:
    refs:
      - master
    variables:
      - $DEV_CI_SKIP == "true"
dev_refresh_slave_auto_scaling:
  stage: refresh_slave_auto_scaling
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_slave_auto_scaling
  only:
    refs:
      - master
    variables:
      - $DEV_CI_SKIP == "true"
dev_change_alb_slave_to_master:
  stage: change_alb_slave_to_master
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_slave_to_master
  only:
    - master
dev_change_alb_master_to_slave:
  stage: change_alb_master_to_slave
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_master_to_slave
  only:
    - master
dev_refresh_master_auto_scaling_after_slave:
  stage: refresh_master_auto_scaling_after_slave
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling_after_slave
  only:
    refs:
      - master
    variables:
      - $DEV_CI_SKIP == "true"
# stg aws deploy
stg_update_launch_template:
  stage: update_launch_template
  <<: *stg_aws_valiables
  <<: *aws_cli_setup
  <<: *update_launch_template
  only:
    - staging
stg_refresh_master_auto_scaling:
  stage: refresh_master_auto_scaling
  <<: *stg_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling
  only:
    refs:
      - staging
    variables:
      - $STG_CI_SKIP == "true"
stg_refresh_slave_auto_scaling:
  stage: refresh_slave_auto_scaling
  <<: *stg_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_slave_auto_scaling
  only:
    refs:
      - staging
    variables:
      - $STG_CI_SKIP == "true"
stg_change_alb_slave_to_master:
  stage: change_alb_slave_to_master
  <<: *stg_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_slave_to_master
  only:
    - staging
stg_change_alb_master_to_slave:
  stage: change_alb_master_to_slave
  <<: *stg_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_master_to_slave
  only:
    - staging
stg_refresh_master_auto_scaling_after_slave:
  stage: refresh_master_auto_scaling_after_slave
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling_after_slave
  only:
    refs:
      - staging
    variables:
      - $STG_CI_SKIP == "true"

# prod aws deploy
prod_update_launch_template:
  stage: update_launch_template
  <<: *prod_aws_valiables
  <<: *aws_cli_setup
  <<: *update_launch_template
  only:
    - product
prod_refresh_master_auto_scaling:
  stage: refresh_master_auto_scaling
  <<: *prod_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling
  only:
    refs:
      - product
    variables:
      - $PROD_CI_SKIP == "true"
prod_refresh_slave_auto_scaling:
  stage: refresh_slave_auto_scaling
  <<: *prod_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_slave_auto_scaling
  only:
    refs:
      - product
    variables:
      - $PROD_CI_SKIP == "true"
prod_change_alb_slave_to_master:
  stage: change_alb_slave_to_master
  <<: *prod_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_slave_to_master
  only:
    - product
prod_change_alb_master_to_slave:
  stage: change_alb_master_to_slave
  <<: *prod_aws_valiables
  <<: *aws_cli_setup
  <<: *change_alb_master_to_slave
  only:
    - product
prod_refresh_master_auto_scaling_after_slave:
  stage: refresh_master_auto_scaling_after_slave
  <<: *dev_aws_valiables
  <<: *aws_cli_setup
  <<: *refresh_master_auto_scaling_after_slave only: refs: - product variables: - $PROD_CI_SKIP == "true" format_test: stage: test script: - composer global require squizlabs/php_codesniffer - phpcs app --standard=PSR2 --extensions=php -n except: # MR時にフォーマットテスト省略。ブランチに入る際にフォーマットテストが入るから省略する - master - staging - product unit_test: stage: test script: - mv .env.testing.example .env - echo "FIREBASE_API_KEY=$FIREBASE_API_KEY" >> .env
    - composer config --global --auth gitlab-oauth.gitlab.com $GITLAB_ACCESS_TOKEN
    - composer install
    - php artisan key:generate
    - php artisan db:restore test-data.sql
    - phpdbg -d memory_limit=3072M -qrr vendor/bin/phpunit --colors=never --coverage-php build/coverage/Feature-Controller.cov tests/Feature/Controller/
    - phpdbg -d memory_limit=3072M -qrr vendor/bin/phpunit --colors=never --coverage-php build/coverage/Unit-Repositories.cov tests/Unit/Repositories/
    - phpdbg -d memory_limit=3072M -qrr vendor/bin/phpunit --colors=never --coverage-php build/coverage/Unit-Services.cov tests/Unit/Services/
    - phpdbg -d memory_limit=3072M -qrr vendor/bin/phpcov merge --html=build/html build/coverage
  cache:
    paths:
      - vendor/
  artifacts:
    paths:
      - build/html/
    expire_in: 1 days
  only:
    - unit_test

deploy_develop:
  stage: deploy
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy --releases_dir=/var/www/beta-project --release_type=develop
  only:
    - master
deploy_develop_slave:
  stage: deploy_slave
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy_slave --releases_dir=/var/www/beta-project --release_type=develop
  only:
    - master


deploy_staging:
  stage: deploy
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy_stg --releases_dir=/var/www/staging-project --release_type=staging
  only:
    - staging

deploy_staging_slave:
  stage: deploy_slave
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy_stg_slave --releases_dir=/var/www/staging-project --release_type=staging
  only:
    - staging



deploy_product:
  stage: deploy
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy_prod --releases_dir=/var/www/product-project --release_type=product
  only:
    - product

deploy_product_slave:
  stage: deploy_slave
  <<: *aws_cli_setup
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy_prod_slave --releases_dir=/var/www/product-project --release_type=product
  only:
    - product

 

 

ここの箇所が全体で実施される

# ここの箇所が実行される onlyでブランチが指定される
stages:
  - test
  - deploy
  - update_launch_template
  - refresh_master_auto_scaling
  - change_alb_slave_to_master
  - deploy_slave
  - refresh_slave_auto_scaling
  - change_alb_master_to_slave
  - refresh_master_auto_scaling_after_slave

 

deploy_develop:
  stage: deploy #stagesのdeployで実行される
  <<: *aws_cli_setup # アンカーの実行
  script:
    - 'which ssh-agent'
    - apt-get update
    - apt-get install ssh-askpass
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAMPLEAPP_SSH_KEY") - ssh-add -l - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null\n\n" > ~/.ssh/config'
    - composer global require laravel/envoy:1.6.1
    - envoy run deploy --releases_dir=/var/www/beta-project --release_type=develop
  only:
    - master #masterブランチのみでの実行

 

 

composer config –global –auth gitlab-oauth.gitlab.com $GITLAB_ACCESS_TOKEN

composer.jsonの中に自身で管理しているプライベートリポジトリからのインストールを指定する場合に、これが必要。

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)