Showing posts with label Continuous Integration. Show all posts
Showing posts with label Continuous Integration. Show all posts

Tuesday, November 23, 2021

DevOps - Importance of deleting container images by CI pipeline after pushing to registry

When we use self-hosted Azure pipeline agents, we may encounter the below issue during the build process. This is not a hard issue to troubleshoot. The reason is there in the error message.

Error processing tar file(exit status 1): open /root/.local/share/NuGet/v3-cache/670c1461c29885f9aa22c281d8b7da90845b38e4$ps:_api.nuget.org_v3_index.json/nupkg_system.reflection.metadata.1.4.2.dat: no space left on device

##[error]Error processing tar file(exit status 1): open /root/.local/share/NuGet/v3-cache/670c1461c29885f9aa22c281d8b7da90845b38e4$ps:_api.nuget.org_v3_index.json/nupkg_system.reflection.metadata.1.4.2.dat: no space left on device

##[error]The process '/usr/bin/docker' failed with exit code 1

Solution

Clean up the artifacts from the build server once it is uploaded to the permanent artifact's location. Also, clean up any residual that is produced as part of the build process such as unit test results. In our case, we faced this issue due to the leftover container images in a Linux build server. We have to delete the local images after pushing to Azure Container Registry.

Workaround

By default, Docker files (containers, images, etc) are located in /var/lib/docker which is part of the / filesystem. We can check how much space is left by using the df -h Linux command.

There are at least 2 workarounds

Option 1 - increase storage

Increase the size of the current / file system. This is easy if the build server is in the cloud environment. There will be some downtime of course.

Option 2 - change docker storage location

Mount new storage to the build server and point the docker to use that as the storage location. Below are the steps.

  • The default location of docker images is specified in the /etc/docker/daemon.json file. The property name is "data-root". 
  • That can be changed to the newly mounted location.
  • Restart the Docker daemon.
    • sudo systemctl daemon-reload 
    • sudo systemctl restart docker
A totally different option is to get rid of self-hosted agents and use the Azure agents. This depends on the project requirements and enterprise policies.

References

Tuesday, June 18, 2019

PlantUML - Code, Generate and Publish Mind-maps from GitPages

Introduction

This post mainly aims to provide an idea how we can use PlantUML to create mind maps and host them from GitPages without spending anything. It obviously need a computing device with browser and internet connection.

PlantUML

No need to have introduction again, as there were posts written earlier on the same. Just to summarize
  • It is a textual representation of diagrams.
  • Helps to version and compare changes applied.
  • Can render in different visual formats from the same textual definitions.

Mind maps

It is diagramming model where information about one concept is represented in relation with other related topics. More on Wiki.

Coding Mind maps

The setup explained in previous post is enough for coding mind maps. PlantUML supports mind maps out of the box in latest versions as beta.

Rendering diagram

Though the ideal way to publish into web is HTML, there seems some issues in generating HTML files out of the box. So better get it into image formats and embed into HTML using <img> tag. 

If we are using our machines during development, it is easy to have control over the environment and install GraphViz and Java runtime needed for PlantUML. Now a days everybody uses continuous integration and delivery method. The CI/CD environments with full control mostly require fees and here we are trying to achieve without any cost. Let is see how to get the diagrams published in free?

One way is to use containers where entire ecosystem would be present and use it in CI/CD environment. Another way which we are going to see is via proxy service of PlantUML

Using the proxy service is very simple. The official www.plantuml.com site has a service hosted in below url

http://www.plantuml.com/plantuml/proxy?src=<accessible URL to the plantuml file>

This src query string pair accepts a source url where the PlantUML code file is present and returns converted image representation.

eg:The below URL gives the png file corresponds to the PlantUML file located at the path mentioned in src query string parameter
http://www.plantuml.com/plantuml/proxy?src=https://raw.githubusercontent.com/mind-maps/software/master/debugging/before-debugging-others-problem.puml

More details on controlling the behavior can be found in this post.

The moment we got the png back via an URL, we can easily publish using html files. No need to have CI/CD systems and dealing with containers or build environments.

Publishing

GitPages provides free sub domain based web site hosting free and we can host mind maps directly from the GitHub repository. Using the markdown, we can easily convert the above mentioned url into an link <a> or <img> tag.

eg:A link in markdown file (.md) can be denoted as below

[Debugging](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/mind-maps/software/master/debugging/before-debugging-others-problem.puml)

Sample repo

Source repo which hosts the puml files & hosting .md files - https://github.com/mind-maps/software
Sub domain & URL based web site which GitPages gives for repo- https://mind-maps.github.io/software/

The .md files gets automatically converted into HTML and served. If there is index.md it would be taken as default when we didn't mention anything in the url. GitPages internally uses Jekyll and customization can be done. One such customization in the above repo is in below URL. Since Jekyll and customization is not in the scope of this post, please google for more details.

Below are not essential but just to give complete picture
Sub domain based organization URL - https://mind-maps.github.io/
Repo of organization web site - https://github.com/mind-maps/mind-maps.github.io

Enjoy documenting...

Tuesday, October 30, 2018

Travis-CI - Continuous deployment to staging but production deployment only on tag commit

Background

Travis-CI is a hosted Continuous Integration & Continuous Delivery tool. Based on configurations, users can have their application deployed to production. They provide free service to open source projects. The CI&CD environment currently using Linux.

Aim

  • Have the CI & CD pipeline trigger for each and every commit going into GitHub. Deploy only to staging or testing area.
  • Testing / staging area is GitHub pages hosted in the same repo. Deploy to staging should not trigger another build which ends up as infinite loop. Not into gh-pages branch but to \docs folder.
  • When there is tagged commit deploy to production.

How its done

Avoid build on commit from Travis-CI bot

The word bot is used to indicate the CI&CD account used by Travis-CI. We can see that account on the commits from Travis-CI. When the bot commits, it uses message as follows.

""Deploy <github userid>/<project> to github.com/<github userid>/<project>.git:master""

One such example given below.

"Deploy joymon/prayerbook to github.com/joymon/prayerbook.git:master"

We need to write build conditions at the top of .travis.yml file to bye pass the build if the commit message is matching.

Build and deploy on commit tag

This is another condition we need to put into condition. If we don't put this Travis-CI will not build on tag commit. Below is one example.

if: tag IS present OR commit_message != "Deploy joymon/prayerbook to github.com/joymon/prayerbook.git:master" OR tag = true

If we use tag = true alone it may not work. Needs more analysis on the Travis-CI yml schema.

A sample travis.yml file can be found below which works based on the above conditions.

https://github.com/joymon/prayerbook/blob/master/.travis.yml

References

https://stackoverflow.com/questions/18216991/create-a-tag-in-github-repository
https://docs.travis-ci.com/user/conditions-v1
https://github.com/travis-ci/travis-ci/issues/8518

Disclaimer

The author is not working for Travis-CI or any of their subsidiaries or partners. There is no guarantee that the technique mentioned in this post will work forever. Travis-CI may change their system the way they want it to be. 
This is not at a sponsored post.

Tuesday, September 25, 2018

Travis-CI v/s Windows development environment

We are in the world of DevOps where we do CI & CD activities automatically for even individual code commits. Don't think DevOps means just doing CI & CD. One of the practice in DevOps is CI&CD. Though there are so many CI&CD tools, the leading 'free for open source hosted SaaS' tools are AppVeyor & Travis-CI. Azure Pipeline is new player in the market giving free service.

AppVeyor is my default choice. But in order to get a feel of another CI&CD mechanism, but I used Travis-CI in one of my personal projects.

Problem

It was going good till the builds started failing recently. After inspecting logs, I could understand that there are new NPM module versions available and conflicting. They were used because I used ^ in versions which is mentioned in packages.json. So decided to fix.

It started working fine in my local machine but when I pushed into GitHub, the CI&CD pipeline again failed.

This time its was on the tests. Some files were not served during tests. The http error code is 404-Not found. Initially I thought, since the file names have unicode characters in it, Webpack dev server is not able to serve the files. But it worked in my machine. The classic case of 'it works in my machine'.  But here it is my personal project and I am the only one to fix.

Cause

After hours of debugging via log statements, I was able to figure the silly issue. It is the difference in Linux v/s Windows. Windows is not case sensitive for file paths but Linux is. The files are accessible in Windows but due to case sensitivity the files are not accessible in Linux. Travis-CI uses Linux in their build environment.

Seems others too faced the same issue and asked Travis-CI to add documentation.
https://github.com/travis-ci/docs-travis-ci-com/issues/209

But Travis-CI is smart and had the documentation already. It was I who is now into the lazy to read group.
https://docs.travis-ci.com/user/multi-os/#operating-system-differences

Moral of the story

Better be consistent with Dev, QA, Stg and Prod environments. 10th item in the 12-Factor App methodology.
https://12factor.net/dev-prod-parity

Updates

15Oct2018

More best practices on TravisCI - https://eng.localytics.com/best-practices-and-common-mistakes-with-travis-ci/

Tuesday, August 28, 2018

Another .Net helper library via nuget package system

Background

Over the past 13 years, to be precisely from Nov 2005 till today, I had written lot of .Net code for day job as well as to personal projects. When I started .Net, I though yes I will master it and enjoy rest of my career. But I soon realized that is not going to work with the collapse of Silverlight. Microsoft was telling or people was arguing that Silverlight will not die as Microsoft is using it for their Azure portal. All of a sudden MSFT replaced Azure Silverlight site with HTML and that was kind of last nail on Silverlight. More details on it can be found in my last post in SilverlightedWeb blog which is a readonly blog now.  Then I thought Silverlight technology's end is inevitable as it is replaced by HTML5 but .Net will live long.

That thought got shaken when MSFT released their so called code editor now becoming full fledged IDE named VS Code. That didn't use WPF which was the star of desktop programming from MSFT at that time. Instead it used Electron from Github which depends on Chrome. Yes, the browser from Google powering web. Essentially we develop browser application and show as standalone executable. That was the time I said good bye to WPF technology. More details here. Then whats left, only ASP.Net which was and still struggling to compete with NodeJS. Don't bring Windows Phone here as that is the one of very hand countable things MSFT properly shut down. No idea how long something called UWP will live.

.Net Core

Finally something came named .Net Core. Its like Angular 1.x and 2. Only name is same, internally all most new. That is what now MSFT fans betting on, as return of .Net. .It is advertised as another true cross platform which will run on Linux! Yes its the second cross platform .Net. The original one also advertised as cross platform with the intermediate language and JIT similar to JVM echo system.

The another factor is performance. .Net Core is expected to beat NodeJS for serving http responses. There are some case studies people are claiming it is faster such as on Bing.

Another areas where .Net was weak is AI, Machine Learning, distributed computing etc... Now there is ML.Net SDK also announced.

Yes it may be faster and may become powerful than Python in AI programming.  But will this technology enough to feed my family in future?

So what is next?

Personally I don't see a bright future for .Net unless .Net Core becomes a big hit. So better to reduce focus on .Net and consider other technologies as well seriously. Electron for desktop development, Angular + NodeJS for web front end, Scala for distributed programming etc...

But what I should do with all my .Net knowledge acquired in past 13 years as I still have hope on huge return of .Net core?

Offload from brain and move on. The better place to offload code level techniques is a nuget package at this point. I could have added the helper classes to my first nuget package, but unfortunately that was towards a specific problem of Orchestration. Hence I had to start another nuget library for my helper classes and coding techniques. Link to Github repo below.

https://github.com/joymon/dotnet-helpers

This library uses multi targeting feature so that one code base can be compiled to multiple targets. This is useful especially to provide libraries for .Net Core.

Thanks to AppVeyor for giving free CI&CD support to publish to nuget repo.

Why I didn't join with other helper nuget libraries is described in the readme of the repo.

Tuesday, May 16, 2017

Protractor - CI with AppVeyor issue on webdriver-manager update

Protractor is a nice tool to do testing of Angular applications. It really shines when used in continuous delivery scenarios because of its end to end testing capabilities. We can simply host the site and run our tests with Protractor. But when we use it with hosted CI solutions there is a chance for getting the below exception in the build process.

(node:1184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to make Github request, rate limit reached.

This happens when we issue the below command which we cannot avoid to get it working.

webdriver-manager update

One such failure can be seen here. What is really happening is that during the update, the webdriver-manager is trying to download something from GitHub for Gecko driver. Since most of the hosted CI solutions use same single IP to access outside internet, the GitHub API limits comes into play.

This is already known issue and the only way is to disable Gecko driver for now. Command to disable Gecko driver is as follows.

webdriver-manager update --gecko false

Thanks to the AppVeyor support team.

Tuesday, January 24, 2017

My JavaScript Module experiments - CI & CD

This is second in the series "My JavaScript Module experiments". This post talks about setting up a simple CI & CD pipeline in modularied JavaScript environment using AppVeyor as CI & CD runner.

Other posts in this series below.

1 My JavaScript Module experiments
2 My JavaScript Module experiments - CI & CD

Introduction

This post directly jumps to setting up CI & CD for modularized JavaScript. Here also Webpack is used as module loader as well as to do other operations such as TypeScript transpilation, minification etc...

AppVeyor & GitHub Pages

AppVeyor & GitHub pages are 2 external services used here. AppVeyor provides free CI & CD service for open source projects and GitHub Pages provides free hosting from Github repositories. Basic knowledge about these services are considered as prerequisites.

AppVeyor is mainly targeted to Microsoft .Net stack and it automatically takes care, if there is a Visual Studio solution file. But we can make it work for Node as well by disabling the default build process. That can be done by simple changes inside appveyor.yml file. AppVeyor supports configuring CI & CD activities either in a appveyor.yml file found in the GitHub repo or through their UI. Here we are using the appveyor.yml  file as its easy to understand. Though is a file publicly accessible file in repo, we can include sensitive information inside it with encryption.

Separation of Concerns - What does what

When we look at the technologies used, we can see those can so same tasks. AppVeyor can run normal commands as well as PowerShell and even start NodeJS which in turn starts Webpack. Using normal commands, PowerShell & Node we can do file copy operations. Webpack which can be started as command can also execute code which does file copy. What is the problem if all the technologies can do same task? The problem is lack of clarity on which component does what.

For small projects, it is fine. We can have AppVeyor scripts and Webpack do file copy. But when we think of larger projects we need to have clear distinction on that should do what. Else it would be difficult to maintain in future.

The simple rule can be to assign the dev related tasks such as compilation and bundling  to Webpack and deployment tasks to the AppVeyor scripts such as pushing to GitHub. Ultimately AppVeyor is ruling the environment. So there should be one AppVeyor command which starts Webpack. Summarized duties as follows.
  • AppVeyor
    • Downloads the source code
    • Install proper Node version and setup environment by installing packages.
      • This include the Webpack NPM package too.
    • Starts Webpack
    • Run tests.
    • Collect the artifact and deploy  
  • Webpack
    • Compilation
    • Do minification / Uglify
    • Create bundle
    • Emit the output to \dist folder. (\dist is just a convention. It can be any folder)

The CI & CD workflow

The CI & CD workflow can be simple. After the transpilation &  bundling the output HTML app which includes html, js, images etc...needs to be pushed to the \docs folder of repository. The GitHub repository can be configured to serve web pages from \docs folder.

Sample

It is easy to explain the but there are high possibility for missing steps. So to make it more understandable, please refer sample repo located at below location

  • It does CI & CD on 'TypeScript-Angular1-Adv folder'.Other folders are showing different Webpack samples.
  • Push the output to the \docs folder in same repo. \Docs folder is configured to GitHub pages to serve from.
  • It is little advanced Angular 1 sample done using TypeScript.
  • Node & NPM used as dev technology.
  • AppVeyor.yml has all the CI & CD steps. The GitHub token is encrypted and kept inside it.
  • Since the aim of that folder is to minimally show the webpack features, at this point there are no tests included.

Tuesday, July 5, 2016

Published .Net Orchestration library to nuget via AppVeyor

Introduction

There is no need to explain about the importance of nuget package ecosystem to .Net development community. Now a days its very difficult to consider someone as .Net if he don't have a nuget package. Of course now I can tell because I just published my first nuget package :)

Its the same Orchestration library I have hosted some months back.

Standard way

How to package our .Net library(.dll or scripts) as nuget package and publish to www.nuget.org is documented very clearly in nuget web site. Link below.

http://docs.nuget.org/create/creating-and-publishing-a-package

So I followed the same guidelines and published it. All manual and using Visual Studio and command line tool nuget.exe. Its available in below location.

https://www.nuget.org/packages/Orchestration

CI & CD way

Manually publishing every time from our development machine is really boring task. Its highly repetitive. Also how to ensure that the the library actually works after each change? If we cannot solve our this problem, how can we solve other's problem.

As everybody knows, the answer to this problem is continuous integration. If that is hosted its too easy. If its free too, don't ask any more questions, just do it.

As we have seen in some of previous posts, AppVeyor is the best place for doing free hosted CI & CD for .Net projects at this time. I will highly recommend than Microsoft's Visual Studio Online service which gives some free build minutes. Infact Microsoft itself using AppVeyor to do their integration.

Below are the official AppVeyor documentation about how to setup CI for nuget projects.

http://www.appveyor.com/docs/nuget#automatic-publishing-of-nuget-projects
http://blog.appveyor.com/blog/2014/02/21/nuget-support-in-appveyor-ci

Compilation of the project and running unit tests are trivial. There won't be usually any issues at all. But we want to automatically create the nuget package and increment it's version, we may need to think twice.

There are basically 2 options. The first option is kind of obsolete now.
  1. Explicitly run our own script to auto increment the nupkg file version and fire the nuget pack command to create the .nupkg file
    1. http://www.codeproject.com/Tips/806257/Automating-NuGet-Package-Creation-using-AppVeyor
    2. https://code.msdn.microsoft.com/Create-and-Push-NuGet-c6072402
  2. Use AppVeyor's out of the box settings to auto increment the version.
    1. Enable Assembly patching in AppVeyor
    2. Use $version$ token in version property inside .nuspec file
    3. Enable the nuget build in AppVeyor to automatically package the file.
We can take the second approach which is out of the box which don't.require any coding. Remember less code less defects!!!. Once this is working file we can see the .nupkg file in the build artifacts. From there we can push via AppVeyor itself. We need to setup our nuget API key either in appveyor.yml (encrypted) or in the web interface.

I was in little trouble in the beginning to do things without custom scripts and contacted their support. They helped immediately. I really appreciate it.
http://help.appveyor.com/discussions/problems/525-nupkg-getting-created-at-same-version-every-time

Tuesday, February 23, 2016

Why I moved www.JoymonOnline.in code to Github from VS Online

The related between me ad VSOnline was very good. It was all working fine till I wanted to setup continuous integration and delivery to my personal web site www.JoymonOnline.in.

My Requirements

Its was simple.
  • Free - Need to get the continuous integration in free.
  • CI & CD - Support for hosted service preferred
    • Support for running unit tests and integration tests.
  • Integration with source control - The integration should happen as soon as I check-in
Everything available there in VS Online. 1000+ minutes of build is more than enough for doing CI activities. But still why I had to move to Github + AppVeyor combination?

Issue 1 - No IIS

I was using IIS hosted tests for the web site. In my development machine I can host the project in IIS and run from Visual Studio. The test methods were attributed with [HostType("ASP NET")] to get the tests run by calling the IIS hosted pages. When I tried to run the same tests from VS Online, I could see that I need to write extra scripts to create IIS web app more than that VS Online build machines  does not have IIS. The suggestion is to have machine somewhere else on premise or in cloud and deploy the app there and run unit tests.

To be frank, I don't have enough budget to have a machine in the internet for doing integration testing of my web site.

Issue 2 - No admin access

When the build machine is allocated for us from VS Online, our build is not running with admin privileges. So even if we try to host web app with other frameworks such as MVCIntegrationTestFramework, it won't allow.

Link to list of software in VS Online build machine.

What I get in Github + AppVeyor

  • Hosting in Github is free. Only thing is my source will be public. But what is there in it? HTML & CSS files which anyone can copy from browser.
  • AppVeyor is free if the source is in Github. They provide admin access to our build activities and has IIS in their machine along with other software.
  • Secure storage of credentials to do Web Deploy or FTP upload
More details are in one of the earlier post.

So which is better? Absolutely going to Github and setting up CI & CD via AppVeyor is the way

Now everything is good. After my check in AppVeyor build the site run unit testing and deploy to staging. After manual testing in staging, if I put a release tag on Github, AppVeyor will deploy to my production www.joymononline.in server which is in GoDaddy.

Tuesday, February 16, 2016

CI & CD for pure HTML+JS browser apps using Travis-CI

When its said pure HTML+JS browser app, it really mean. There is no web services or server side HTML generation involved. Its simple HTML+JS site running inside browser. People may wonder why such an browser app needs continuous integration where there is no compilation involved.

That's true that it doesn't need compilation. But it always can have tests and those tests can be run after each git push and deploy to staging or production. Many of the enterprise people will not have such a scenario of pure client app. However lets look how can we setup Continuous Integration & Continuous Delivery for such an pure HTML+JS SPA.

Objectives

  • Upon git push the integration process should start
  • It should unit test the tests written using Jasmine framework.
  • If the tests are working, deploy to a FTP location in a web site.

Tools & Frameworks used

The app uses HTML5,JS and jQuery. Below is the list used for CI & CD activities.
  • Github - where the code is stored
  • Travis-CI - which provides machine to run the CI & CD activities
  • Jasmine - Test Framework API
  • Karma - Test runner
  • PhantonJS - The headless browser where Karma can run the tests
  • NodeJS
  • NPM Modules other than required by above
    • ftp-deploy

Node.JS

Do not confuse with the usage of Node.JS. We are not going to run anything in server, its pure HTML+JS SPA. NodeJS is just to do npm package management and to upload to FTP for deployment. The test runner Karma which we are going to use comes as npm package. Also this is the language type supported in Travis-CI for pure HTML+CSS+JavaScript projects.

Prerequisites

This assumes the understanding of
  • Basic web development using HTML+JS+CSS (browser side alone)
  • Using Github.
  • Understanding about NodeJS and its package management ecosystem npm.

Setting up source - production & test code

Application

The application which we are going to use is HTML5 based Karel simulator. Instead of creating separate branch inside it's git repository, better we can create another repository named travis-ci-test in Github itself and uploaded the same source there. /Src is the folder which contains source code.

Tests

There is no question that the leader in client side web testing is Jasmine framework. This provides the API to write test code. Since this is not a post to discuss about Jasmine framework, lets go to Jasmine test code directly. The test JavaScript files can reside inside in the /Tests folder.

Running tests using SpecRunner.html

Jasmine is a JavaScript test framework written in JavaScript and run tests inside browser. For that we just need to create a html file and refer Jasmine js files, app/production js files and test scripts. In this case a SpecRunner.html is available in below location. Browsing it will run the tests and show the results.


Make sure the tests are running from SpecRunner.html. It is always recommended to do a big job in small tasks which can be done in less than 1 hour.

Setting up NodeJS & NPM

The above setup will work really great in our machine. But when in CI environment, we need to run the tests from program. Travis-CI doesn't have client side only JavaScript+HTML app support. But it has node_js support which we can leverage here to test our app.

Next step is to add some Node.JS things. Basically we need to convert our folder to Node.JS application. We can create/convert a Node.JS app by simply issuing 'npm init' command, from any command prompt inside our folder. It will ask for some questions and will create package.json file.

Introducing Karma & PhantomJS

Once our folder is npm enabled or in other sense we got a package.json, we can install packages. Karma is a test runner program which can run our Jasmine tests we wrote in previous step. The JavaScript needs a engine to execute the script and Karma is not a js engine. So it needs a js engine. It can either use any browser itself or can use headless browser called PhantomJS. PhantomJS comes as npm package. All the packages mentioned till here can be installed via npm. See the devDependencies section in package.json file for the packages what we need.

In our local machine we can use npm install <package name> --user-dev command to install the packages. But in CI machine, it will do automatically by reading the package.json.

Setting up Karma & PhantomJS

Karma wants to know where are the production & test js files to run tests. For that it needs a config file. In this case, its  karma.config.js


This file can be created using karma init command. It will ask some questions and create the file. This file is self explanatory.

How npm knows Karma is the test runner to be used

Once the setup is done we are going to execute the tests by issuing below command.

npm test

How does npm knows to run the tests using Karma? Its again configuration inside package.json.

There is a section called 'Scripts' and it contains 'test' section inside. Test node expects an executable command. There we can give the command to run Karma along with karma config we created in previous step.

Once this step is proper we should be able to issue 'npm test' command and it should should run the tests and show results. Testing every step is important.

Upload to FTP via ftp-deploy npm module

Now comes to deployment. If all the tests are passed, we can deploy to our staging or production environment. In this case I want to deploy to a FTP location. The hosted CI platform Travis-CI which we are going to use, doesn't have direct support to deploy a folder to FTP. We had to rely on curl command which can transfer one file at a time. One file at a time doesn't work most of the time.

Now there are 2 ways
  • Use tools such as grunt / gulp which can orchestrate the CI activities. They have FTP deploy methods.(Google knowledge. Didn't try myself)
  • We can write some code in shell script or NodeJS and hook it after the tests run successfully.
As there are already enough tools and technologies just for CI & CD than the production code, lets write some custom NodeJS code to upload files to FTP. No to Grunt and Gulp for now.

The written FTP upload code uses ftp-deploy npm package to upload the files. Why should we rewrite code again for reading the folders, reading the files and to upload using FTP protocol. That is something many people already solved and made open source.

The FTP code can be hooked to the integration process after test by editing the package.json file. We can use 'posttest' section for it.

Testing everything in local machine

If the above environment is ready, we should be able to run everything in our machine. The command 'npm test' should run the unit tests and if everything pass, deploy to FTP folder. In our local environment to work we need to give the FTP credentials to ftpupload script via environment variables. We will see why its via environment variables in next section.

Intro to Travis-CI and getting started with it

Travis-CI is a free hosted CI&CD SaaS environment for open source projects. This is similar to AppVeyor. We could have used AppVeyor which is already referred in previous posts and have already used in other .Net projects. But this time lets try something new. Below are the steps to achieve the same.

  • Upload the code to Github.
  • Create account in Travis-CI using Github.
  • Connect to Github project.
  • Setup below environment variables for FTP access.
    • ftp_host
    • ftp_user
    • ftp_password - Do not turn the flag to display in build log. Build log is public
    • ftp_localPath - Path to the folder inside the CI machine. Usually src
    • ftp_remotePath - Path to where the files should be copied. Only relative url
Refer a build log for sample values. Once this is working. Just do a push to Github to start the integration process.

UI testing in Travis-CI

Travis-CI don't support running in chrome out of the box. They are telling we need to give some commands to get it running. To me it didn't work out. It tried 2-3 times and said failed to start Chrome. So decided to use PhantomJS. Anyway in this sample application, the main objective is test the JavaScript. So it works.

If there are any steps missing and difficult to follow just fork / download the sample. There is no good documentation than the code itself.

References

http://orizens.com/wp/topics/first-steps-in-setting-up-travis-ci-to-your-javascript-project/
https://blog.logentries.com/2015/01/unit-testing-with-karma-and-jasmine-for-angularjs/
http://swizec.com/blog/how-to-run-javascript-tests-in-chrome-on-travis/swizec/6647
http://gis.utah.gov/how-to-wire-up-travis-ci-to-your-js-projects/
http://www.sitepoint.com/testing-javascript-jasmine-travis-karma/

Tuesday, February 9, 2016

Automated integration testing in ASP.Net MVC & Forms without IIS

Background

I have wrote about testing ASP.Net web forms in one of my previous post. This the era of MVC and I am still writing about web forms. Mainly I am working with web forms only for my web site http://joymononline.in. I have started it before MVC days and still I don't see any reason for converting to MVC. Its serving its purpose using the web forms technology? Why should I change? Another reason I am keep it there in web forms just to realize that technology should not drive our architectural decisions. Every time before I suggest MVC for any project, I think about my own web site and think twice whether we should go to MVC or not? None in my company agree or do in old web forms but its still worth considering and making sure there is specific reason for selecting ASP.Net MVC. According to them we should never do anything in web forms.

Recently I tried to implement Continuous Integration and Continuous Delivery to my web site. Details of doing it via AppVeyor can be found in another post. It was very easy to run unit tests in the integration server.

Problem

But the one of the issue I found during that exercise is on running Integration tests. What I wanted was to make sure all the pages in my site is browsable after each check and and deploy to staging. I was using [HostType("ASP.NET")] attribute to run the tests against IIS. This is mentioned in my earlier post about testing web forms. This works well in development machine as I have created IIS web application from Visual Studio and the project was pointing to that web application(Project Properties->Web->Server->Local IIS in VS 2013). But in build machine, this hosting will not happen automatically. We need to write script to host the application to IIS which is according to me extra task.

Possible approaches

This problem is not new. Many people have solved it in many ways. There are free solutions such as using tools like Selenium or can use hosting libraries such as WatiN, MVCInterationTestFramework etc... Some more options can be seen in the below link.
http://stackoverflow.com/questions/118531/what-is-the-best-way-to-test-webforms-apps-asp-net

My Solution

The requirement is clear. We need to host the site in a web server to run integration tests. So why can't we start a web server before the test begins and the test request site page. This lead to MVCIntergationTestFramework library. This .Net library helps us to host any folder as IIS web application. It also has some features to test results from MVC response.

No need to get confused with MVC prefix. As mentioned earlier, we can host any folder as IIS web application. So web forms also works. The main git project has many clones and one of those provide nuget as well named FakeHost. So its easy to include into VS test project.

Continuous integration

Since it doesn't need upfront IIS web application, it is easy to run tests from CI server provided it has IIS in it. AppVeyor has IIS in it and it runs smoothly there.

Once again this is my solution towards a small problem of integration testing of my personal web site. It may not be applicable always. If anybody wants to take a look at production code how its done, have a look at my personal web site code in Github. Now I am really enjoying the free CI & CD life. I don't know how to write down the joyful feeling of getting our source compiled after each check in, automatically testing the same and publish to staging and production if everything goes well. Everything free.

Happy CI & CD

Tuesday, January 26, 2016

AppVeyor a free cloud CI - First experience

Background

Though open source doesn't mean free, most of the people working in open source area wants to get things free which ever is required for their projects. Github as free source control repositoty, Google hangout for meetings etc...AppVeyor is one of the tool which should be added to this list especially its .Net project. If we google we can get lot of information about AppVeyor. This post is going to be discussed about my experience with AppVeyor. I am really late into this open source world.

When we consider starting of any software project, there are many things other than writing code. Most of those things needs minimum research before starting. Else it will take more time to setup later. As seen earlier there should be some place to host the code. We cannot have keep the code in each and every developer's machine and pass it in zip files. Github now became one stop destination for hosting code free. So at least that piece doesn't require any more research. Another thing is Continuous Integration.

Continuous Integration

The need for CI

If we have experience more than 10 years, probably we might not have heard this word in college or during the initial years inside software industry. At that time we were considering a release as something special like Kumbamela which occurs in predefined intervals. Intervals were in years and in some places months. Nobody saw even in dream that they can give something bi-weekly to customer. But things changed with the adoption of a magic word called Agile. People started to give releases in 2 weeks to customers or at least forced to do so. That automatically bring the demand for continuous integration especially in automated ways.

In the earlier days when the release is approaching one person/team will be allocated for creating builds. All he needs to do is to compile the project and create MSI. Install it. Run basic tests and give to QA for detailed testing. Some teams opted for rotational policy for this build person / team. 

Software engineers are expected to solve others problems especially repeatable and involves human power. Who solves their problems? Of course themselves. In order to save a build person/team's pain the Continuous Integration is born.

What is continuous integration

As the name says integrate the works of developers continuously instead of waiting till the release date. Integration should mean getting the code changes of all developers, compiling the same and generating binaries/installers. Nice to have features are running unit tests, deploying to test machines and running integration tests. Advanced practitioners can even push to production if all the tests are passed. Everything is expected to be automatic. Better integration should start based on a souce code check-in.

This ensure that the defects are caught early instead of waiting till the release date.

CI Platforms

We saw that it needs to happen automatically on check-in. Where all these operations can run? They need dedicated one or more machines because developers cannot just sit idle during this time by running it in their machines.

In the initial days of CI people used separate systems for doing integration. Obviously when we automate integration we need software to do the process. Some people wrote their own programs to do all these operations and clever people created products out of it idea and made money. There were open source CI systems also emerged.

Even if buy or get OSS, it required a machine to run CI. Open source people at least who are working as individuals and small teams were struggling in this area. A machine is not free. Even if we keep old system for CI, we need to invest for internet, power etc...Enterprises also suffered a little as they need to maintain the CI machines.

Advances in the cloud technology solved this problem. Cloud allowed anybody to have a machine in cloud for short span of time. The SaaS model ie Software as a Service made the things more simple. They started giving CI as a Service. In other words, anybody can out soure the CI activities to cloud based systems without worrying about setting up a machine in cloud. All we had to do is to connect our source code with CI as a Service system. They have machines in cloud in which they download our source compile it, package it, run unit tests and even deploy it to production.

What is there for open source. There started some hosted CI providers who give free CI to open source projects along with their paid service. Wow...Anybody can do CI free in cloud. There are many major players. Lists can be found in many places in internet. One of those is AppVeyor.

AppVeyor experience

I started my research when I continuously saw a "Build Passing" flag in many open source projects in Github. Most of those lead me to travis-ci. When I started looking for a CI for my open source projects, I had no confusion to go to travis-ci. But when I read their .Net support, I got little confused. They have Linux servers only which points to mono. My first project which needs CI is not just an app. Its visual studio extension which directly depend on VS SDK. I started research again for CI which has native .Net support. The below links gave me confidence to try with AppVeyor.

Nuget restore

When I started AppVeyor, first stop was on nuget restoration. I know there is nuget restore command. But I was not sure where to enter the command. First google lead me to AppVeyor page where they explains how to deal with nuget packages

It didn't help me in first shot. It was telling that solution was not found

sln not finding

The exact error goes as below

MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
This folder contains no solution files, nor packages.config files.
Command exited with code 1

Nowhere it tells its due to nuget except 'nor packages.config'. Googled and immediately found the below link which explains if the solution is in inner folder in github src, we need to explicitly mention the .sln file path in nuget restore command as parameter.
http://help.appveyor.com/discussions/problems/1446-yml-file-isnt-finding-my-solution-file

VS Extension issue due to AWS

AppVeyor's console output for build is really awesome. It shows real time status about build. It showed below error message.

Setting up Visual Studio for debugging extensions. This one-time operation may take a minute or more.
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe /RootSuffix Exp /ResetSettings General.vssettings /Embedding /Command File.Exit
log4net : error XmlHierarchyConfigurator: Could not create Appender [RollingFileAppender] of type [log4net.Appender.RollingFileAppender]. Reported error follows. [C:\projects\joyful-visualstudio\src\VS2013\JoyfulTools\VSExtension\VSExtension.csproj]
System.IO.FileNotFoundException: Could not load file or assembly 'AWSToolkit.Util' or one of its dependencies. The system cannot find the file specified.
File name: 'AWSToolkit.Util'
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(Assembly relativeAssembly, String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.SystemInfo.GetTypeFromString(String typeName, Boolean throwOnError, Boolean ignoreCase)
at log4net.Util.TypeConverters.TypeConverter.ConvertFrom(Object source)
at log4net.Util.OptionConverter.ConvertStringTo(Type target, String txt)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Type type, String value)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element, Object target)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(XmlElement element, Type defaultTargetType, Type typeConstraint)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element, Object target)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.CreateObjectFromXml(XmlElement element, Type defaultTargetType, Type typeConstraint)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element, Object target)
at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)

I am not at all using AWSToolkit or log4net. This is the time the impression about AppVeyor went down. My initial google didn't give me proper answers so I created a support ticket in their forum

http://help.appveyor.com/discussions/problems/3348-vsix-project-build-fail-on-starting-integration-test

The response to the question came in amazing speed which I never experienced in any other support systems. He really knows the system well he clearly told the issue is related with an extension which is in the Visual Studio installed in their CI machines. Disabling that will solve the issue. He gave PowerShell command to disable the same.

I was not much clear on where to apply this script. This issues was already faced by others as well and they tried the same workaround. So I looked there and tried the normal command to disable the AWSToolkit.
http://help.appveyor.com/discussions/questions/1275-build-failed-log4net-error-xmlhierarchyconfigurator-could-not-create-appender-rollingfileappender

Yes it rocks. Now there is integration happening for each and every check in and I can get the artifact in their site.
https://ci.appveyor.com/project/joymon/joyful-visualstudio/build/1.0.0.27/artifacts

Note : I really don't have any relation with AppVeyor except using their free service. I cannot just stop me from writing about their free service and excellent support for free customers. May be in future they change their policy. But still then they are great.