Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deploy Code to Containers From Git Automatically

DZone's Guide to

Deploy Code to Containers From Git Automatically

Moving your code from your git repository of choice to your container can be a pain. Read on to learn how to automate the process.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

There is a number of options to deploy your source code from a Git repo to containers including a redeploy of the whole container, instant redeploy via volumes, or the “git clone” approach. However, when it comes to automation of this process and moving to continuous deployment, many developers can face the complexity, as they need to know how to properly combine all the application components with the required interconnection points.

Specifically, in the containers world, you have to manage builds of your stack images dealing with the extra complexity of the CI/CD pipeline. And the whole containers redeploy may not be the best approach if you do frequent commits without configuration changes in the operating system, application server stack, or its dependencies.

github ci cd

To ease deployment automation, Jelastic prepared a special dedicated Git-Push-Deploy package for code delivery into the preliminarily built container images. This package implements a number of configurations to set up the automatic deployment of committed changes within your Git application source repository to the cloud, making them available for further testing with minimal delays.

Git-Push-Deploy Specifics

The Git-Push-Deploy package can be integrated with GitHub and GitLab repositories. It is developed for automatic delivery of updates within your Java, PHP, Ruby, Node.js, and Python application sources and can be applied to the following certified stack templates:

  • Java – Tomcat 6/7/8/9, TomEE, GlassFish 3/4, Jetty 6/8/9, WildFly 8/9/10, JBoss AS 7, Spring Boot 1.x
  • PHP – Apache 2.4, NGINX 1.10
  • Ruby – Apache 2.4, NGINX 1.10
  • Node.js – Node.js 0.x-6.x
  • Python – Apache 2.4

The workflow depends on the programming language used in your project:

  • for Java-based projects, the package initiates the creation of a separate environment with a Maven build node, which will be responsible for interaction with remote Git repository, triggering your application builds and their deployment to the application server.
  • for PHP/Ruby/Node.js/Python applications, the package sets up a pipeline for project deployment directly to the ROOT context on a web server (here, consider that Ruby app servers are supplied with a deployment mode instead of a context within the dashboard, though the actual project location is the same).
  • This deployment automation package is compatible with Jelastic PaaS of the 4.9.5 version and higher. To review and compare available hosting platforms with a particular Jelastic version, refer to the Jelastic Cloud Union Catalog.

    Repository Pre-Configurations

    For a proper add-on installation, you’ll need to provide a Personal API Token for your Git account. This enables the package to set up a webhook for the corresponding repository, which will initiate application redeployment each time a change is contributed to its code.

    So let’s generate one. Follow the instructions below according to the used Git VCS, i.e. either GitHub or GitLab.

    Generating Access Token on GitHub

    To get your personal access token for your GitHub account, navigate to the Settings > Personal access tokens and click the Generate new token button.

    node js app deployment automation

    In the opened page, specify the Token description and select the repo and admin:repo_hook scopes. Click Generate token at the bottom of the page.

    github auto deploy

    Once redirected, copy and save the shown access token anywhere else (as it can’t be viewed again after you leave this page).

    ruby continuous deployment

    After this is accomplished, proceed to the Install Git-Push-Deploy Package section further along in this article.

    Generating Access Tokens on GitLab

    To generate a personal access token on GitLab, enter your account Settings and switch to the Access Tokens tab.

    Here, specify the optional token Name, its Expiry date (which can be left blank) and tick the api permission scope.

    python app deployment automation

    Click the Create Personal Access Token button.

    In the opened page, copy and temporarily store your access token value anywhere else (as you won’t be able to see it again after leaving this page).

    github ci cd php

    Now, you are ready for package installation.

    Extra Pre-Configurations for Java Projects

    If running a Java-based project, you need to preliminarily ensure its proper interaction with the Maven build node by adding a special Project Object Model (POM) file to its structure.

    So, create a pom.xml file in your project repository root, with the following content as an obligatory basis:

    <project>
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.mycompany.app</groupId>
       <artifactId>my-app</artifactId>
       <version>1.0</version>
       <packaging>war</packaging>
       <build>
           <finalName>${project.artifactId}</finalName>
       </build>
    </project>


    Where optional values are:

    • groupId – group of a project (e.g. company name).
    • artifactId – name of a project.
    • version – your application version.

    All the rest of parameters should be left unchanged. You can check how it should be done in our sample.

    Install Git-Push-Deploy Package

    Git-Push-Deploy package is an add-on, so it can be installed only on top of an environment. We have prepared two separate environments with Tomcat and Apache-PHP application servers to show the workflow for different programming languages.

    If you are going to use a previously created environment, note that the package will overwrite the application deployed to the ROOT context. So to keep your already deployed application, move it to the custom context. We recommend creating a new environment and then proceeding to the installation:

    1. Click the Import button on the top pane of the dashboard and insert a manifest.jps link for Git-Push-Deploy project within the opened URL tab:

    https://github.com/jelastic-jps/git-push-deploy/blob/master/manifest.jps

    ruby application deployment automation

    Click Import to continue.

    2. In the opened frame, specify the following details about your repository and target environment:

    • Git Repo URL –  HTTPS link to your application repo (either .git or of a common view). You can fork our sample Hello World application to test the flow.
    • Branch – a project branch to be used.
    • User – enter your Git account login.
    • Token – specify the access token you’ve previously created for webhook generation.
    • Environment name – choose an environment to which your application will be deployed.
    • Nodes – application server name (is fetched automatically upon selecting the environment).

    java continuous deployment

    Click Install to continue.

    3. Wait a minute for Jelastic to fetch your application sources from GitHub and configure webhook for continuous deployment.

    php github auto deploy

    Close the notification frame when installation is finished.

    4. Depending on a project type, the result will be the following:

  • for Java-based infrastructure, you’ll see a new environment appear on your dashboard with a Maven build node inside; it will build and deploy your application to the ROOT context on a web server each time the source code is updated
    application ci/cd automation
  • Pay attention that it might take some time for Maven to compile a project (though the package installation itself has already finished), so you need to wait a few minutes before launching it. The current progress of this operation can be tracked in real time via vcs_update log file on Maven:
    jps automation git push

  • for PHP-based infrastructure (and the rest of supported languages), your application will be deployed directly to the chosen server ROOT.
  • Herewith, consider that the similar Projects section for Ruby application servers provides information on used deployment mode (development by default) instead of a context, whilst actual app location refers to server root as well.

    To start your application, click on Open in browser next to your web server.

    node js continuous deployment

    That’s it! Now a new version of your application is automatically delivered to the application server upon each commit to a repository.

    Redeployment Policies for Different Stacks

    The table below lists behavior of different application servers after receiving the updated code.

    Stack Name Policy
    Tomcat 6 Restart
    Tomcat 7 Restart
    Tomcat 8 Restart
    Tomcat 9 Restart
    TomEE Restart
    GlassFish 3 Hot Redeploy via Server API
    GlassFish 4 Hot Redeploy via Server API
    GlassFish 5 Hot Redeploy via Server API
    Jetty 6 Restart
    Jetty 8 Restart
    Jetty 9 Restart
    JBoss 7 Restart
    Wildfly 8 Restart
    Wildfly 9 Restart
    Wildfly 10 Restart
    Railo Restart
    SpringBoot Restart
    Apache-PHP Advanced ZDT
    Nginx-PHP Advanced ZDT
    Apache-Ruby Graceful Reload
    Nginx-Ruby Graceful Reload
    NodeJS Restart
    Python Restart

     

    To eliminate possible application downtime for a server with Restart update policy, scale it out to be run over multiple containers. In this case, the required updates will be applied to the instances sequentially, with a 30-second delay by default.

    Test Automated Deploy From Git

    And now let’s check how this process actually works. Make some minor adjustment to the code in a repo and ensure everything is automated:

    1. Click Edit this file for some item within your project repository and Commit changes to it – for example, we’ll modify the text at our HelloWorld start page.

    github continuous deployment

    2. As a result, the appropriate webhook will be triggered to deploy the made changes into your hosting environment –  refer to the repository Settings > Webhooks section for the details.

    python continuous deployment

    Upon clicking on this string you’ll see the list of Recent Deliveries, initiated by the webhook, and the result of their execution.

    3. As the last checkpoint, return back to your application page and refresh it (whilst remembering that it may take an extra minute for Maven to build and deploy your Java-based project).

    java app deployment automation

    That’s it! As you can see, the modifications were successfully applied, so the solution works as intended.

    Simply update your code, make commits as you usually do, and all the changes will be pushed to your Jelastic environment automatically. No need to switch between processes or make manual updates eliminates human errors and accelerates time to market for your application. 

    Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

    Topics:
    cloud ,container ,git

    Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    THE DZONE NEWSLETTER

    Dev Resources & Solutions Straight to Your Inbox

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

    X

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}