DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Seamless CI/CD Integration: Playwright and GitHub Actions
  • Advanced CI/CD Pipeline Optimization Techniques Using GitHub Actions
  • Automate Web Portal Deployment in Minutes Using GitHub Actions
  • Deploy MuleSoft App to CloudHub2 Using GitHub Actions CI/CD Pipeline

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Automate Your Development Workflow With Github Actions

Automate Your Development Workflow With Github Actions

By 
Hacene KASDI user avatar
Hacene KASDI
·
May. 04, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
17.7K Views

Join the DZone community and get the full member experience.

Join For Free

Hi everybody. My name is Hacene. I am a software developer and interested in DevOps. Today, I will show you how to automate a development workflow life cycle using Github Actions.

On November 13, 2019, Github engineers revealed some news: GitHub Actions is supporting CI/CD now, and it's free for public repositories! We can manage and automate our development workflows right in our repository using GitHub Actions. The API supports multiple Operating Systems (Linux, Windows, MacOS…) and different languages.

In this article, I will give you an example of a CI/CD pipeline using Github Actions for Spring boot 2.x (java 8) web service. 

Introduction

The idea is to automate the development workflow by building a CI/CD pipeline using Github Actions. The aim is to deploy a Spring Boot web service on a remote server. We will use a succession of jobs to build, test, generate code coverage report, and deploy our code right from GitHub. The generated package will be deployed on the remote server. 

CI/CD pipeline

There are two ways to create a required workflow file :

We can create a workflow in our Github repository. The YAML file must be stored in the .github/workflows directory in the root of our Github repository. The workflow must have at least one job that contains a set of steps that perform individual tasks. Steps can run commands or use an action. Our workflow file should respect a YAML syntax and must have either a .yml or .yaml file extension. If you want to learn more about YAML, see "Learn YAML in five minutes."

Initial project structure

By creating a workflow file in the Actions section on GitHub repository, we choose maven workflow :

Workflow Actions
Java with Maven

When we click on Set up this workflow, we will see the pre-filled maven workflow below:

Maven workflow

The jobs are based on Matrix builds. A matrix build can generate at most 256 jobs per workflow run.

We configure a workflow to start when a GitHub event occurs. The jobs will be triggered after pushing code or making a pull request on the master branch. The main job in this example is the launching mvn package command. As it's specified in the YAML file, we have to commit the changes to the master branch.

Testing the Workflow

Let’s test it and see what happens …

Testing application workflow

After launching the workflow process, the configuration executed will define some actions that should be performed inside the Ubuntu container. The job makes a checkout of my git repository on the master branch and sets up a JDK 1.8. Then, it starts the mvn build command and puts up the required Maven packages included in my pom.xml. Finally, it runs the tests and generates the JAR file.

Here are some limits about using Github Actions, check this link.

Enhancing the Workflow

Now, we will divide our workflow into different jobs:

compile job: checks out the Github source code and compiles it.

test job: runs unit tests (it requires a compile job). 

build job: runs the mvn build command and generates the code coverage report, which will be sent to codecov.io. 

Below is the complete file:

YAML
xxxxxxxxxx
1
56
 
1
name: Java CI with Maven
2
3
on:
4
  push:
5
    branches: [ master ]
6
  pull_request:
7
    branches: [ master ]
8
9
jobs:
10
11
  compile:
12
    runs-on: ubuntu-latest
13
    name: Running Java ${{ matrix.java }} compile
14
    steps:
15
    - uses: actions/checkout@v2
16
    - name: Set up JDK 1.8
17
      uses: actions/setup-java@v1
18
      with:
19
        java-version: 1.8
20
    - name: Compile code
21
      run: mvn compile
22
23
  test:
24
    runs-on: ubuntu-latest
25
    name: Running tests
26
    needs: compile
27
    steps:
28
      - uses: actions/checkout@v2
29
      - name: Set up JDK 1.8
30
        uses: actions/setup-java@v1
31
        with:
32
          java-version: 1.8
33
      - name: Run unit tests
34
        run: mvn test
35
36
  build:
37
    runs-on: ubuntu-latest
38
    name: Run mvn build and generate coverage report
39
    needs: test
40
    steps:
41
      - uses: actions/checkout@v2
42
      - name: Set up JDK 1.8
43
        uses: actions/setup-java@v1
44
        with:
45
          java-version: 1.8
46
      - name: Build with Maven
47
        run: mvn -B package --file pom.xml -Dmaven.test.skip=true
48
      - name: generate report codecov
49
        run: mvn cobertura:cobertura
50
      - name: Upload coverage
51
        if: success()
52
        run: |
53
          curl -s https://codecov.io/bash | bash
54
        env:
55
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
56
        shell: bash


We use encrypted secrets that allow us to store sensitive information. A field CODECOV_TOKEN, is a token retrieved from codecov.io. It's persisted as a secret in the Github environment.

Once the actions have been performed, we can see the result in the Actions tab:

Checking Actions tab


In the last step, we need to add a job to deploy the jar file on the remote server. This job is pretty much the same as the preceding ones. We have to copy the executable file to the remote server; this operation is based on SFTP protocol. 

To get this working, we will use actions shared by the GitHub community, and we will customize them as needed, garygrossgarten/github-action-scp@release and fifsky/ssh-action@master. Then, we have to add the secrets (environment variables for ssh connection) on the Github secrets section of our repository. 

These variables will be available in the container that was started with Github Actions:

Github Secrets session


Adding a job to deploy the executable file:

YAML
xxxxxxxxxx
1
31
 
1
deploy:
2
  runs-on: ubuntu-latest
3
  name: Deploy the JAR file to the remote server
4
  needs: build
5
  steps:
6
    - uses: actions/checkout@v2
7
    - name: Set up JDK 1.8
8
      uses: actions/setup-java@v1
9
      with:
10
        java-version: 1.8
11
    - name: Generate the package
12
      run: mvn -B package --file pom.xml -Dmaven.test.skip=true
13
    - name: Deploy the package to the remote server
14
      uses: garygrossgarten/github-action-scp@release
15
      with:
16
        local: target/workflow-github-actions-1.0-SNAPSHOT.jar
17
        remote: hacene/demo/actions/workflow-github-actions.jar # My remote directory
18
        host: ${{ secrets.HOST }}
19
        username: ${{ secrets.SSH_USER }}
20
        password: ${{ secrets.SSH_PASSWORD }}
21
    - name: Run a script on remote server (start the application)
22
      if: always()
23
      uses: fifsky/ssh-action@master
24
      with:
25
        command: |
26
          cd hacene/demo/actions/ && java -jar workflow-github-actions.jar &
27
        host: ${{ secrets.HOST }}
28
        user: ${{ secrets.SSH_USER }}
29
        pass: ${{ secrets.SSH_PASSWORD }}
30
        args: "-tt"
31
        # The & in the command runs the process on background


The result looks like this :

Deploying application


After deploying and running the container using Postman, we call an exposed endpoint:

Calling endpoint via Postman


Everything seems to be working. Now, we add the well-deserved badge markdown on our README.md.

Shell
xxxxxxxxxx
1
 
1
![Java CI with Maven](https://github.com/kasdihacene/workflow-github-actions/workflows/Java%20CI%20with%20Maven/badge.svg)
Java CI with Maven

In the README.md file, the result looks promising :

Final README.md file

Conclusion

I hope that you have enjoyed the post and learned how to use Github actions to automate your CI/CD workflow.

Personally, I used to work on a CI platform with Jenkins that required a lot of prerequisites to set up the workflow. So, I’m pretty surprised how well the CI/CD pipeline is working, flawlessly, and quickly using Github Actions. We can take advantage of this flexibility to create and maintain customized development workflows to automate the software development lifecycle processes.

You can find the mentioned project on GitHub, you can also ask me your questions on my blog.

workflow GitHub

Opinions expressed by DZone contributors are their own.

Related

  • Seamless CI/CD Integration: Playwright and GitHub Actions
  • Advanced CI/CD Pipeline Optimization Techniques Using GitHub Actions
  • Automate Web Portal Deployment in Minutes Using GitHub Actions
  • Deploy MuleSoft App to CloudHub2 Using GitHub Actions CI/CD Pipeline

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!