Saturday, March 8, 2025

How to Implement CICD Pipeline using GitHub Actions | GitHub Actions Tutorials | GitHub Actions CICD Pipeline | How to Deploy Java WAR file using GitHub Actions and Maven to Tomcat Server

Please find steps for Deploying Java WAR file to Tomcat using GitHub Actions:

Watch Actions in YouTube:

    Pre-requisites:

    Implementation steps:

    We need to setup secrets to store tomcat user name, password and Tomcat url.

    Add Tomcat user name, password and Tomcat Host url as Secret in GitHub Actions

    Go to your GitHub Repo --> Settings --> 

    Click on Secrets and Variables under Security in left nav 
    Click new Repository Secret

    Create TOMCAT_HOST secret and add tomcat url

    Create TOMCAT_USER secret and add user name
    Create TOMCAT_PASSWORD secret and Tomcat password


    GitHub Actions Workflow YAML for Deploying a WAR file to Tomcat

    You will create this file .github/workflows/cicd.yaml inside GitHub Repo where your Java code is.

    name: Build a WAR file using Maven and Deploy Java App to Tomcat running in AWS EC2
    on:
      push:
        branches: [ "main" ]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
        - name: Set up JDK 11
          uses: actions/setup-java@v2
          with:
            distribution: 'adopt'
            java-version: '11'
        - name: Build with Maven
          run: mvn clean install -f MyWebApp/pom.xml
        - name: Deploy to Tomcat
          run: |
            curl -v -u ${{ secrets.TOMCAT_USER }}:${{ secrets.TOMCAT_PASSWORD }} \
            -T MyWebApp/target/MyWebApp.war \
            "http://${{ secrets.TOMCAT_HOST }}/manager/text/deploy?path=/MyWebApp&update=true"

    Commit the file.

    As soon as you commit, build will run immediately in GitHub Actions. 
    Now you can see the output of build in Actions tab.

    Check the output in Tomcat

    No comments:

    Post a Comment

    Coach AK Master DevSecOps BootCamp FAQs | Master AWS and Azure Cloud computing Program by Coach AK

    (More New Topics..New CICD tool included - GitHub Action, Trivy, Checkov Security scanner! ) 1. How many weeks is this DevSecOps Coaching pr...