Deploy To Azure App Services With Codeship
CodeShip is an online service that lets you setup a CI pipeline fairly easily, for a price beginning at $0. I needed a CI pipeline for this blog, which runs on Jekyll, and decided to give it a shot, since they support Ruby.
They do offer deployment Google, AWS, and Heroku, but Azure is not in the list. Fortunately WebApps and functions allow you to deploy from git, which is the way that we’ll follow.
Pre-requisites:
- Your code needs to be hosted on GitHub, BitBucket or GitLab
- You have a website or a function on which you’ll deploy
Setup your App Service
Deployment is going to be done via git. To setup your App Service accordingly, connect to the Azure Portal, then open the “Deployment options” blade. Pick local git repository, then press OK.
Next go to the deployment credentials blade and save. I usually use the same as the publish settings, it makes for an easy way to find them again. Copy them somewhere, we’ll need them later.
You concretely created a git repo from which the web application is gonna pull your application.
Finally, go to the properties blade of your webapp, and pick the git deployment URL.
Add a script to deploy to Azure from your application
The concept is that we’re gonna commit our application binaries to the git repo on Azure. To do so, we’ll pull whatever is on Azure into the build directory, run the build, add all changes, then commit and push. This will trigger the deployment.
The steps in our build are then gonna be:
- Do pre-build stuff
- Prepare git directory with
./prepare.sh
- Build
- Commit and push with
./deploy.sh
- Do post-build stuff
Preparing the git directory is the following:
- Create directory if not existing
- Initialize the git repo if it’s not there.
if [ ! -d "$DEPLOYMENT_FOLDER/.git" ]; then
if [ ! -d "$DEPLOYMENT_FOLDER" ]; then
mkdir "$DEPLOYMENT_FOLDER"
fi
cd "$DEPLOYMENT_FOLDER"
git init
git remote add origin https://$DEPLOYMENT_USER:$DEPLOYMENT_PASSWORD@$DEPLOYMENT_URL
git pull origin master
cd -
fi
Deploying is simply adding everything to the repo and pushing
cd $DEPLOYMENT_FOLDER
git add -A
git commit -m "`date`"
git push origin master
cd -
These two files are available on GitHub.
Setup project in CodeShip
Create a new project. On the top bar - click “select a project”, then create. Connect whatever SCM you picked, then choose your repository.
Next in the setup commands (in the “Tests” configuration panel), you’ll need to inject whatever you need to prepare the build and run your tests. In my case, I install Jekyll and run npm install for some stuff.
Then switch to the “environment” section of the project settings. Set the following variables:
DEPLOYMENT_USER
to whatever you set it earlier. Azure seems to sometimes expect the username to be prefixed by a $ dollar sign. In this case, you’ll need to escape with a backslash.DEPLOYMENT_PASSWORD
to what you configured earlierDEPLOYMENT_URL
to the value copied from the webappDEPLOYMENT_FOLDER
to wherever the build is spitting the binaries.
Then switch to the “Deployment” section. In the “add deployment” grid, pick “Custom Script”
To deploy you need to make your shell scripts executable, prepare your deployment git repo, build your application, then trigger deployment.
In my case:
git config --global user.email "codeship@whatever.com"
git config --global user.name "Codeship deploy"
chmod +x prepare.sh
chmod +x deploy.sh
./prepare.sh
# DO YOUR BUILD - in my case:
jekyll build
./deploy.sh
All set!
The only thing you’ll need from there is to push to your own git repo, and check what’s happening (it may need some refinement for the build to pass - you can actually SSH into the VM if need be, to finalize your setup).
Build should trigger automatically, and deployment should happen automagically.