{"id":3213,"date":"2016-02-05T04:29:29","date_gmt":"2016-02-05T09:29:29","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=3213"},"modified":"2018-04-03T10:45:06","modified_gmt":"2018-04-03T14:45:06","slug":"circleci-nightly-automation","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/circleci-nightly-automation\/","title":{"rendered":"CircleCI nightly automation"},"content":{"rendered":"<p>A <a href=\"http:\/\/www.qxf2.com\/?utm_source=BS-CircleCI&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2<\/a> client is using <a href=\"https:\/\/circleci.com\/\">CircleCI<\/a> for their continuous integration and deployment needs. We decided to run a small subset of critical tests in our automation suite against every new commit and deploy. We also run our entire automation suite, which takes longer, once every night. This post outlines how we setup CircleCI to achieve this setup.<\/p>\n<figure id=\"attachment_3640\" aria-describedby=\"caption-attachment-3640\" style=\"width: 1036px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/02\/event_run_nightly_run.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/02\/event_run_nightly_run.jpg\" alt=\"CirleCI event and time triggered tests\" width=\"1036\" height=\"515\" class=\"size-full wp-image-3640\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/02\/event_run_nightly_run.jpg 1036w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/02\/event_run_nightly_run-300x149.jpg 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2016\/02\/event_run_nightly_run-1024x509.jpg 1024w\" sizes=\"auto, (max-width: 1036px) 100vw, 1036px\" \/><\/a><figcaption id=\"caption-attachment-3640\" class=\"wp-caption-text\">Figure 1. How we wanted to trigger tests in our test suite<\/figcaption><\/figure>\n<hr>\n<h3>Why this post?<\/h3>\n<p>CircleCI seems to be growing in popularity as an alternative to Jenkins. It took us a while to explore and get used to CircleCI. We thought we would share some of our learnings. <\/p>\n<hr>\n<h3>Setup<\/h3>\n<p>For this tutorial we will have one repository for all our application code and a separate repository for our tests. We are doing this to make the example more realistic. Please refer to our earlier <a href=\"https:\/\/qxf2.com\/blog\/get-started-with-circleci\/\">post<\/a> if you want to know how to integrate your GitHub repository with CircleCI.<\/p>\n<p><strong>a) Application repository<\/strong><br \/>\nTo keep things simple, let us add a single html page in our GitHub application repository. I added an html page which we had created earlier as part of <a href=\"https:\/\/qxf2.com\/blog\/selenium-tutorial-for-beginners\/\">Selenium training<\/a>. You can get the source from this <a href=\"http:\/\/qxf2.com\/selenium-tutorial-main\">link<\/a>. I will name the repository as &#8220;selenium_tutorial&#8221;.  Once you check in this repository you need to give CircleCI permission to access this repository.<\/p>\n<p><strong>b) Test repository<\/strong><br \/>\nLet us pretend that our automated test suite has three tests: Navigate_Qxf2_Tutorial_Test.py, Test1.py and Test2.py. We do not want to run Test1.py and Test2.py after every build. We only want to run Test1.py and Test2.py once every night. <\/p>\n<p>Let us add a simple Selenium script using Python to navigate to the Qxf2 Selenium Tutorial page and assert the page title. Once you have written the test, check-in your changes to a separate &#8220;selenium_tutorial_test&#8221; repository.<\/p>\n<pre lang='python'>\r\n\"\"\"\r\nSelenium Test to login to Qxf2 Tutorial Page and assert the title\r\n\"\"\"\r\nfrom selenium import webdriver\r\n\r\n# Create an instance of Firefox WebDriver\r\ndriver = webdriver.Firefox()\r\n# The driver.get method will navigate to a page given by the URL\r\ndriver.get(\"http:\/\/localhost\/selenium-tutorial-main.html\")\r\n# Assert the Page Title\r\nassert \"Qxf2 Services: Selenium training main\" in driver.title\r\n# Close the browser window\r\ndriver.close()\r\n<\/pre>\n<hr>\n<h3>Event based automation runs<\/h3>\n<p>To trigger our Navigate_Qxf2_Tutorial_Test.py after every build of our application repository, we simply need to add the following lines to the circle.yml file of the application test repository.<\/p>\n<pre lang='xml'>\r\n## Customize dependencies\r\ndependencies:\r\n  pre:\r\n    - sudo pip install -U selenium\r\n    - sudo apt-get update\r\n    - sudo apt-get install nginx\r\n     # Copy the html page to nginx default public www location\r\n    - sudo cp .\/selenium-tutorial-main.html \/var\/www\/html\/selenium-tutorial-main.html\r\n    \r\n## Running Tests\r\ntest:\r\n  # The key call to clone the test repository before we run the test\r\n  pre:\r\n    - git clone https:\/\/github.com\/Qxf2Circle\/selenium_tutorial_test.git\r\n    \r\n  override:\r\n    - python selenium_tutorial_test\/Navigate_Qxf2_Tutorial_Test.py\r\n<\/pre>\n<p>We ensure that <strong>Navigate_Qxf2_Tutorial_Test.py<\/strong> test runs after every build by specifying it in the override section<\/p>\n<hr>\n<h3>CircleCI nightly automation runs<\/h3>\n<p>We want to run Test1.py and Test2.py once every night. To make sure that they do not run after every build, we need to perform the following steps:<br \/>\n1. Setup an API token<br \/>\n2. Write a shell script that triggers a nightly build by calling a CircleCI API<br \/>\n3. Modify circle.yml file<br \/>\n4. Setup a cronjob to kick off the .sh script regularly<\/p>\n<p><strong>1. Setup an API token<\/strong><br \/>\nYou can create an API token in your &#8216;Account Settings&#8217; page. To create an API token, simply give it a name and click on the &#8216;Create new token&#8217; button. For now, copy your API token and keep it handy.<\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/10\/API_Token.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/10\/API_Token.png\" alt=\"CircleCI_API_Token\" width=\"1353\" height=\"470\" class=\"aligncenter size-full wp-image-3218\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/10\/API_Token.png 1353w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/10\/API_Token-300x104.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2015\/10\/API_Token-1024x356.png 1024w\" sizes=\"auto, (max-width: 1353px) 100vw, 1353px\" \/><\/a><\/p>\n<p><strong>2. Write a shell script that triggers a nightly build by calling a CircleCI API<\/strong><br \/>\nCircleCI provides an API which lets you kick-off builds. The API (simply a URL that you POST to) needs to know your project,branch and circle token. For this tutorial we will create a nightly_build.sh file which takes these three parameters as command line arguments.<\/p>\n<pre lang='bash'>\r\n#!\/bin\/bash\r\n#Src: https:\/\/circleci.com\/docs\/nightly-builds\r\n\r\n_project=$1\r\n_branch=$2\r\n_circle_token=$3\r\n\r\ntrigger_build_url=https:\/\/circleci.com\/api\/v1\/project\/${_project}\/tree\/${_branch}?circle-token=${_circle_token}\r\n\r\npost_data=$(cat <<EOF\r\n{\r\n  \"build_parameters\": {\r\n    \"NIGHTLY_BUILD\": \"true\"\r\n  }\r\n}\r\nEOF)\r\n\r\ncurl \\\r\n--header \"Accept: application\/json\" \\\r\n--header \"Content-Type: application\/json\" \\\r\n--request POST ${trigger_build_url}\r\n<\/pre>\n<p>Your project name is typically <company-name>\/<repo-name>. For example, Qxf2Circle\/selenium_tutorial. Your CircleCI token is what you created in Step 1. To trigger the nightly run, you would run the command:<\/p>\n<pre lang=\"bash\">\r\nnightly_build.sh <your_project_name> master <your_circleci_token>\r\n<\/pre>\n<p><strong>3. Modify your circle.yml file<\/strong><br \/>\nTo your existing circle.yml file, add logic to trigger Test1.py and Test2.py only if the POST has the NIGHTLY_BUILD parameter to True.<\/p>\n<pre lang='xml'>\r\n## Customize dependencies\r\ndependencies:\r\n  pre:\r\n    - sudo pip install -U selenium\r\n    - sudo apt-get update\r\n    - sudo apt-get install nginx\r\n     # Copy the html page to nginx default public www location\r\n    - sudo cp .\/selenium-tutorial-main.html \/var\/www\/html\/selenium-tutorial-main.html\r\n    \r\n## Running Tests\r\ntest:\r\n  # The key call to clone the test repository before we run the test\r\n  pre:\r\n    - git clone https:\/\/github.com\/Qxf2Circle\/selenium_tutorial_test.git\r\n    \r\n  override:\r\n    - python selenium_tutorial_test\/Navigate_Qxf2_Tutorial_Test.py\r\n  post:\r\n    - >\r\n      if [ -n \"${NIGHTLY_BUILD}\" ]; then\r\n         python .\/selenium_tutorial_test\/Test1.py\r\n         python .\/selenium_tutorial_test\/Test2.py \r\n      fi\r\n<\/pre>\n<p><strong>4. Setup a cronjob to kick off the .sh script regularly<\/strong><br \/>\nTo kick of the script we need to invoke scheduled job. To run the new job every night we will create a cron job by running crontab -e and adding:<\/p>\n<pre lang='bash'>\r\n30 0 * * * \/bin\/bash \/home\/avinash\/Downloads\/nightly_build.sh Qxf2Circle\/selenium_tutorial master $CIRCLE_TOKEN\r\n<\/pre>\n<p>This runs the nightly_build.sh script at 30 minutes past midnight with arguments<\/p>\n<p>_project=Qxf2Circle\/selenium_tutorial<br \/>\n_branch=master<br \/>\n_circle_token=Update your circleci token here<\/p>\n<p>If you wanted to trigger a run of the entire suite sometime during the day, simply call:<\/p>\n<pre lang='bash'>\r\nnightly_build.sh Qxf2Circle\/selenium_tutorial master $CIRCLE_TOKEN\r\n<\/pre>\n<hr>\n<p>And with that you know how to use CircleCI to run a small subset of your tests after every build, while running your entire automation suite every night. Let us know if you have any questions in the comments below!<\/p>\n<p><strong>If you are a startup finding it hard to hire technical QA engineers, learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2 Services<\/a>.<\/strong><\/p>\n<hr>\n<script>(function() {\n\twindow.mc4wp = window.mc4wp || {\n\t\tlisteners: [],\n\t\tforms: {\n\t\t\ton: function(evt, cb) {\n\t\t\t\twindow.mc4wp.listeners.push(\n\t\t\t\t\t{\n\t\t\t\t\t\tevent   : evt,\n\t\t\t\t\t\tcallback: cb\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n})();\n<\/script><!-- Mailchimp for WordPress v4.10.1 - https:\/\/wordpress.org\/plugins\/mailchimp-for-wp\/ --><form id=\"mc4wp-form-1\" class=\"mc4wp-form mc4wp-form-6165 mc4wp-form-theme mc4wp-form-theme-blue\" method=\"post\" data-id=\"6165\" data-name=\"Newsletter\" ><div class=\"mc4wp-form-fields\"><div style=\"border:3px; border-style:dashed;border-color:#56d1e1;padding:1.2em;\">\r\n  <h1 style=\"text-align: center; padding-top: 20px; padding-bottom: 20px; color: #592b1b;\">Subscribe to our weekly Newsletter<\/h1>\r\n  <input style=\"margin: auto;\" type=\"email\" name=\"EMAIL\" placeholder=\"Your email address\" required \/>\r\n  <br>\r\n  <p style=\"text-align: center;\">\r\n    <input style=\"background-color: #890c06 !important; border-color: #890c06;\" type=\"submit\" value=\"Sign up\" \/>\r\n    \r\n  <\/p>\r\n  <p style=\"text-align: center;\">\r\n    <a href=\"http:\/\/mailchi.mp\/c9c7b81ddf13\/the-informed-testers-newsletter-20-oct-2017\"><small>View a sample<\/small><\/a>\r\n  <\/p>\r\n  <br>\r\n<\/div><\/div><label style=\"display: none !important;\">Leave this field empty if you're human: <input type=\"text\" name=\"_mc4wp_honeypot\" value=\"\" tabindex=\"-1\" autocomplete=\"off\" \/><\/label><input type=\"hidden\" name=\"_mc4wp_timestamp\" value=\"1775566843\" \/><input type=\"hidden\" name=\"_mc4wp_form_id\" value=\"6165\" \/><input type=\"hidden\" name=\"_mc4wp_form_element_id\" value=\"mc4wp-form-1\" \/><div class=\"mc4wp-response\"><\/div><\/form><!-- \/ Mailchimp for WordPress Plugin -->\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>A Qxf2 client is using CircleCI for their continuous integration and deployment needs. We decided to run a small subset of critical tests in our automation suite against every new commit and deploy. We also run our entire automation suite, which takes longer, once every night. This post outlines how we setup CircleCI to achieve this setup. Why this post? [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,52],"tags":[],"class_list":["post-3213","post","type-post","status-publish","format-standard","hentry","category-circleci","category-continuous-integration"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=3213"}],"version-history":[{"count":29,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3213\/revisions"}],"predecessor-version":[{"id":6240,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/3213\/revisions\/6240"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=3213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=3213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=3213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}