Brief Description
Will be sharing how I was able to deploy same codebase to different AWS account on Vapor using multiple vapor.yml
config file with the help of bash scripts.
At the time of writing this, Laravel Vapor did not support deploy command to receive a filepath to deploy against. Hopefully one day that feature can be added by the team.
These approach allows you to use separate AWS account for your staging and production environments if needed as well better than that deploy to multiple aws account or multiple aws region.
How it works
Assuming you already have your laravel project setup and working with Laravel Vapor.
- Rename
vapor.yml
tovapor.{account}.yml
, project should be a unique name different to the other file you need to create. eg.vapor.dev.yml
2.Create another vapor config file to deploy to the different account or region. eg.vapor.devcon.yml
and make sure config are correct. - Make sure
vapor.yml
file doesn't exist on your root directory of project anymore. - Copy the following scripts to the root directory of project and make sure to make them executable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/bin/env bash set -E trap 'throw_exception' ERR if [[ -z "${1}" ]]; then consolelog "usage: vapor.sh {account} {command}" "error" consolelog "example: vapor.sh account env:pull staging" exit fi VAPOR_PROJECT_FILE="vapor.$1.yml" if test -f "$VAPOR_PROJECT_FILE"; then echo "Using file $VAPOR_PROJECT_FILE" else consolelog "file $VAPOR_PROJECT_FILE does not exist." "error" exit fi VAPOR_FILE=vapor.yml if test -f "$VAPOR_FILE"; then consolelog "$VAPOR_FILE file exist, we are not allowed to run the vapor script" "error" exit fi # Move the file to a temporary "vapor.yml" which the vapor bin can use cp $VAPOR_PROJECT_FILE $VAPOR_FILE # Forward all arguments except the first one to the vapor binary vapor ${@:2} rm -f $VAPOR_FILE This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#!/usr/bin/env bash set -E trap 'throw_exception' ERR if [[ -z "${1}" ]] || [[ -z "${2}" ]]; then consolelog "usage: deploy.sh {account} {environment}" "error" consolelog "example: deploy.sh account staging" exit fi VAPOR_PROJECT_FILE="vapor.$1.yml" if test -f "$VAPOR_PROJECT_FILE"; then consolelog "Using file $VAPOR_PROJECT_FILE" else consolelog "file $VAPOR_PROJECT_FILE does not exist." "error" exit fi VAPOR_FILE=vapor.yml if test -f "$VAPOR_FILE"; then consolelog "$VAPOR_FILE file exist, we are not allowed to run the vapor script" "error" exit fi # Move the file to a temporary "vapor.yml" which the vapor bin can use cp $VAPOR_PROJECT_FILE $VAPOR_FILE vapor deploy $2 --commit="$(git show -s --format=%h)" --message="$(git show -s --format=%s)" rm -f $VAPOR_FILE
Interact with scripts
At this stage you should have 2 vapor config file on your root directory. From our example you would have:
vapor.dev.yml
vapor.devcon.yml
Vapor CLI:
./vapor-cli.sh dev env:pull staging
./vapor-cli.sh dev env:push staging
Deploy:
./vapor-deploy.sh dev staging
./vapor-deploy.sh devcon production
Take Away
This approach can be used to deploy Staging and Production to different AWS account or Region as well.
All that will be
To have correct file naming and config to have only correct environment setting on the related file.
To make changes to both script by to only allow 1 argument and use 1st argument as filename and environment to run against.
Eg.
vapor.staging.yml
vapor.production.yml
./vapor-deploy.sh staging
./vapor-deploy.sh production
Top comments (0)