Codeship Basic uses fast, performant VMs with preinstalled technology stacks to make it super easy and fast to get started running a CI/CD pipeline.
We already deploy the application on GCE by creating a VM instance. If you have no idea about that you can refer it from here. I personally experienced that whenever new version of the application occurs or any new feature added to the application, I had to deploy it again. Such as the need to follow
- ssh to vm
- git pull
- create docker image
- and the finally deploy
I personlly hate this method. I wanted everyone steps has to be automated. Whenever the commit is pushed to github ,I want first to test the code and if test pass then deploy.
The solution for above problem is codeship basic pipeline. Follow the below to steps to create the CI-CD pipeline to deploy on GCE(Google Compute Engine)
1. Connect to source code provider
When you signup to Codeship, connect your Github or Bitbucket account.
When it comes time to configure your project. You will see
2. Configure Test
Select the technology you want , it will automatically create the test command. Otherwise select “I want to create my own custom commands”
For now all I have here is sbt test which runs my unit test cases.
Click save, go to dashboard and follow the on-boarding steps to push your project.
Do a git push to your master branch and let codeship do it’s magic. If all works well you’ll see this.
If your test cases failed you get “failure” in red at place of success. See the logs and fix it before going to production.
Now uptil now we set the CI.
Now the question is how i deploy to the google compute engine ?
3. Create the vm on GCP.
If you are not aware about how to create CLICK HERE. Setup the enviroment on VM you need to run the application. As i m running my scala application using container ,I setup the following things on VM
You have to setup the enviroment as per your requirement.
4. Preparing GCE to accept the codeship deployment.
After creating vm . SSH into your machine.
Now , go to your home directory and create a folder called MyApp. (mkdir MyApp) or you can give meaningfull name as you want.
Then, go back to Codeship, go to project settings and click on general.
Copy Codeship’s public ssh key from here. This will let Codeship deploy to your server easily.
Then back in your terminal, You need to add the CodeShip public SSH key for your project to the ~/.ssh/authorized_keys file on your server.
Go to project setting , Select deploy -> script ->
rsync -avz ~/clone/ firstname.lastname@example.org:MyApp ssh email@example.com 'cd codeship-rest && sudo docker build -t codeship-rest:0.1 . && sudo docker stop knolx-rest && sudo docker rm knolx-rest && sudo docker run --name knolx-rest --env-file env.list -d --restart always --network host codeship-rest:0.1'
Click on “Save Changes”. Onec you push commit to master branch your pipeline get automatically trigger.
First, rsync -av copies all the files from the clone directory which Codeship creates on every push, into your MyApp folder that we previous created on your host GCE system.
Next we run some commands over ssh. The commands are we run basically cd into the MyApp directory, build the docker image, stop all previously running docker instances and remove them and then run the docker app on localhost.
(Note: Here i already created a dockerfile which is present on my git repo, so if you need to run application using docker container you need to fisrt create Dockerfile and push to your github repo)
5 . Build Trigger
In build trigger you can select which branch you need to build, which branches you don’t want to build or need to build all branches.
Also select when you want to build
- Build on commit, merge, and tags
- Build only on PR events
If you need to export some environment variables , you can specify the in the environment section.