We now have a (mostly) functional app that has been uploaded to S3 and is live for the world to see. But opening up AWS and dragging files after a manual build is tedious and prone to error. To remedy this situation, we're going to write an automated deployment script that will run a series of commands to send our newly created site to S3.
Once we have our automated deployment set up, we'll create an account with CircleCI for continuous integration. If you're not familiar with continuous integration, it's a service that will automatically build your app, run your tests, and if everything seems to work, it will automatically deploy the current version.
Continuous integration is extremely useful for large organizations that have many developers pushing code to the same codebase at the same time while in production. That said, it requires rigorous testing or you might find yourself in a position where you deployed an update that broke something.
We're first going to set up automated deployment on our local environment, then we'll worry about continuous integration. We're going to need to install the AWS command line tool.
$ brew install awscliThen we need to configure our AWS credentials so we can deploy. You can do this one piece at a time, but I recommend writing a shell script so you can execute these the same way every time. Let's go ahead and create a new file called deploy.sh.
$ touch deploy.shWithin this file, we should add all of the necessary commands to deploy to S3. Remember from a previous lesson that we need to export all of our environment variables so we can access them later. This is what the source .env.sh command was for.
echo "Version number..."aws --versionecho "Configuring access id..."aws configure set aws_access_key_id $AWS_ACCESS_KEY_IDecho "Configuring secret key..."aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEYecho "Syncing with $BUCKET"aws s3 sync ./dist s3://$BUCKETIf you do not know your bucket location, you can run the following to find out your default region.
$ aws s3api get-bucket-location --bucket $BUCKETWe're also including echo commands so we can see in our terminal what step in the process our configuration has reached. This is basically just a console.log to let us know if and where we run into issues.
Now we should be ready to deploy. Make sure your terminal is in the root of phoenix-chat-frontend and simply run the commands in our deploy.sh script.
$ sh deploy.shAnd like magic, all of your files are uploaded. If you want to run a test to make sure your deploy.sh script is working, try changing the background to red or something obvious and re-deploy. You should see the changes immediately propagate when you refresh the page.
The next step is to set up continuous integration. Go ahead and sign up with CircleCI and link your Github account. If you have a preference for another continuous integration company, feel free to use them--they're mostly all the same, but the files will be different.
Go ahead and click on Add project from the menu on the left, then add those environment variables we have stored in our .env.sh file and API_HOST and SOCKET_HOST as environment variables to CircleCI.
Create a circle.yml file at the root of your project. This is required for CircleCI and it gives a series of instructions for CircleCI to execute in order to properly run your code.
$ touch circle.ymlThen add the following code to configure CircleCI. We will go over each line, but there are many more configuration options available. It might be worth your time to skim through the configuration docs to get a sense of how much you can do with CircleCI and the various continuous integration environments.
machine: node: version: 6.3.0 # React CSS Loader does not work with 0.X versions of nodedependencies: override: - npm install - npm install -g webpack # Need access to webpack cli to build the project - sudo pip install awscli # Need access to AWS CLI to deploy the projectdeployment: aws: branch: master commands: - chmod +x deploy.sh - webpack --config webpack.prod.config.js - ./deploy.shThe first line sets the version of Node to something we want to use. This is not strictly necessary, but it's good to know what version you're using rather than just accepting the default.
Next we set our dependencies. In this case, we are running npm install (which it will run by default anyway, but we're being safe), and two additional installations for the webpack command line tool that will allow CircleCI to compile our code and awscli which is the AWS command line tool we need to run our deploy.sh script.
Finally, we have our deployment to AWS. CircleCI has a special configuration for AWS since so many people use it. We are telling CircleCI to only use these commands for our master branch. If you wanted, you could set up special deploy configurations for other branches. It's not uncommon to deploy to beta or develop to make sure the site works on other branches as you go along.
The only tricky thing here is the chmod +x deploy.sh, which changes the permissions of our container to allow us to execute the deploy script. chmod is one of those things that is rarely covered and used often. If you ever run into errors related to permissions, it's probably an issue with chmod.
Save this file, git add, git commit, and git push, and you should see CircleCI create a container, build the project, run the tests, then deploy to AWS. Pretty cool!
Previous: Deploying the Frontend to S3Next: ESLint and Airbnb Style Guide