A few weeks ago I was trying to update a side project's CircleCI config from version 1 to version 2 since they are depreciating V1 in August 2018. In the process, I was curious how I could deploy specific branches to specific environments in Laravel's Envoyer if the tests passed successfully.
My project has two main branches: develop
and master
. In Envoyer I have two projects, one for dev.project.com
which uses the develop
branch and the other for project.com
which uses the master
branch.
Here is the final result of the circle.yml
file. Let's work through each of the sections below.
Section 1: Defaults
By leveraging YAML anchors we can set a group of defaults that will be used for all of our later jobs. For now, this includes our
working directory
chosen CircleCI docker image
Section 2: Jobs
In this file we have three jobs: build
(and test), deploy_develop
, and deploy_master
.
Our build
job
Imports the defaults
Sets environment variables
Checks out the repo's code
Restores
composer
cache, if availableRuns
composer install
Saves a new
composer
cacheRuns the test suite with PHPUnit
Our "deploy" jobs:
Imports the defaults
Pings Envoyer to deploy the project
Section 3: Workflows
Now that we have our jobs set up, we need to implement workflows to pull everything together. Workflows are optional, but they can come in handy depending on what you'd like to do with your project.
In this example, we only need one workflow notify_deploy
which will notify Envoyer that we want to deploy a specific branch.
Within the workflow, you'll notice that we are listing all of our jobs: build
, deploy_develop
, and deploy_master
.
We start off running our build
job, and if that is successful, we'll move forward with our deploy jobs. Each deploy job requires the build
to run first; then we'll check if the version branch matches the filter on the workflow. So deploy_develop
is only run on the develop
branch and deploy_master
is only run on the master
branch.
By limiting the filters to only the develop
and master
branches we can guarantee that we're only deploying those specific branches, but the build
job will run on all branches (bug, hotfix, and feature branches), which is needed for pull requests.
Wrapping Up
Once we merge a branch into either develop
or master
CircleCI will build and notify Envoyer to deploy if successful. In our CircleCI dashboard, you'll now see a workflow similar to this.
You'll also need to make sure you turn off the "Deploy When Code Is Pushed" option in your Enoyer project.
Learn More
This is only scratching the surface of what you can do with CircleCI builds and workflows. I encourage you to look through the documentation and example projects to see what you can implement in your projects.
Please note - CircleCI nor Envoyer/Laravel paid me to write this article, I'm just a happy customer.