Buy

Our 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.

Setting up the git Repo

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!

Configuring the Deploy

Back in Ansistrano land, the first thing we need to do is configure that ansistrano_deploy_via variable. Set it to git:

15 lines ansible/deploy.yml
---
- 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:

15 lines ansible/deploy.yml
---
- 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:

15 lines ansible/deploy.yml
---
- 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!

Leave a comment!

  • 2017-10-25 Victor Bocharsky

    You're welcome! If you still have this problem with those services - just let's us know ;)

    Cheers!

  • 2017-10-24 Cesar

    Thank you very much for the tip

  • 2017-10-24 Victor Bocharsky

    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!

  • 2017-10-23 Cesar

    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.