If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeOur first deployment task is simple: we need to get our code to the server! By default, Ansistrano does that via rsync
... but it has a bunch of options! Check out the ansistrano_deploy_via
variable. Beyond rsync
, you can use copy
, git
, svn
, s3
- if you want to fetch your code from an S3 bucket - and download
. We're going to use the git
strategy! And most of the rest of the variables in the docs are specific to your deploy strategy.
Ok, so we're going to deploy via git
... which means... well, we should probably create a git repository! First, I'll commit everything locally. You may even need to initialize your git repository with git init
. I already have a repo, so I'll just commit:
git add .
git commit -m "I'm king of the world!"
Perfect! Next, we need to host our Git repository somewhere. It doesn't matter where, but I'll use GitHub. Create a brand new repository! Woo! I'm making this public, but we will talk soon about how to deploy private repositories.
Copy the 2 lines to add the remote and push our code. Then, in your terminal, paste them:
git remote add origin git@github.com:weaverryan/ansistrano-deploy.git git push -u origin master
Progress!
Back on GitHub, refresh! There is our beautiful code!
Back in Ansistrano land, the first thing we need to do is configure that ansistrano_deploy_via
variable. Set it to git
:
- hosts: aws | |
vars: | |
... lines 5 - 6 | |
ansistrano_deploy_via: git # Method used to deliver the code to the server. Options are copy, rsync, git, svn, s3 or download | |
... lines 8 - 15 |
For the Git-specific variables, we need to configure two: the URL to the repo and what branch to deploy. Copy ansistrano_git_repo
first and paste it. For the URL, go back to GitHub and click on "Clone or download". For now, use the https
version of the URL. We're going to change this in a few minutes - but this makes life simpler to start:
- hosts: aws | |
vars: | |
... lines 5 - 6 | |
ansistrano_deploy_via: git # Method used to deliver the code to the server. Options are copy, rsync, git, svn, s3 or download | |
# Variables used in the Git deployment strategy | |
ansistrano_git_repo: "https://github.com/knpuniversity/ansible.git" # Location of the git repository | |
... lines 11 - 15 |
Now copy the last variable: ansistrano_git_branch
. We don't really need to set this... because it defaults to master
. But let's set it anyways:
- hosts: aws | |
vars: | |
... lines 5 - 6 | |
ansistrano_deploy_via: git # Method used to deliver the code to the server. Options are copy, rsync, git, svn, s3 or download | |
# Variables used in the Git deployment strategy | |
ansistrano_git_repo: "https://github.com/knpuniversity/ansible.git" # Location of the git repository | |
ansistrano_git_branch: master # What version of the repository to check out. This can be the full 40-character SHA-1 hash, the literal string HEAD, a branch name, or a tag name | |
... lines 12 - 15 |
Moment of truth! Go back to your terminal and run the playbook again:
ansible-playbook -i ansible/hosts.ini ansible/deploy.yml
This time, we see some Git-related tasks. So that's probably good! And it finishes without any errors.
Let's go see what it did! I'll move back to my terminal that's SSH'ed onto the server. Move out of the current/
directory. That's important: the current
symlink did change, but until you move out of it, you're still looking at the old release directory.
Ok cool! There are two things in releases/
, and the symlink points to the new one. Move back into current/
. And... there's our project! Our code is deployed! Yea, we are missing some things, like parameters.yml
, but we'll get there. For now, celebrate!
Hey Cesar,
Nope, I haven't tried to deploy with S3 and actually, I'm not sure it's a good idea. If GitHub prices are high for you, you can use alternative services which allow you to hosted private repos for free. Some of them have limitations, but it's not a problem if you have a personal project. I'd recommend you to look at Bitbucket, I host a lot of private repositories there for free, but if you're on a big team - look at GitLab service. Those are the most popular I think. And both those services also have deploy keys (read only SSH keys), i.e. you can safely deploy your private repo with read-only access like in GitHub. So I'd rather use them instead of S3, but it's up to you.
Anyway, if you have a certain error on which you stuck, provide us a bit more information about the problem and we'll try to help you. But I think using Bitbucket or GitLab will automatically solve the problem ;)
Cheers!
You're welcome! If you still have this problem with those services - just let's us know ;)
Cheers!
// composer.json
{
"require": {
"php": ">=5.5.9",
"doctrine/doctrine-bundle": "^1.6", // 1.6.8
"doctrine/orm": "^2.5", // v2.7.2
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"sensio/distribution-bundle": "^5.0.19", // v5.0.20
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.26
"symfony/monolog-bundle": "^3.1.0", // v3.1.0
"symfony/polyfill-apcu": "^1.0", // v1.4.0
"symfony/swiftmailer-bundle": "^2.3.10", // v2.6.3
"symfony/symfony": "3.3.*", // v3.3.5
"twig/twig": "^1.0||^2.0", // v1.34.4
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
"predis/predis": "^1.1", // v1.1.1
"composer/package-versions-deprecated": "^1.11" // 1.11.99
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.6
"symfony/phpunit-bridge": "^3.0", // v3.3.5
"doctrine/data-fixtures": "^1.1", // 1.3.3
"hautelook/alice-bundle": "^1.3" // v1.4.1
}
}
# ansible/requirements.yml
-
src: DavidWittman.redis
version: 1.2.4
-
src: ansistrano.deploy
version: 2.7.0
-
src: ansistrano.rollback
version: 2.0.1
Hi. I know that you recommend git for deploying but I don't want to public my code and neither pay in Github for a private repository. In this example you are using an AWS EC2 instance, have you tried to deploy using Amazon S3? Do you know if there is any advantage doing this. I have tried to out but it was unsuccessful. I would appreciate any tip that you can give me.