Building CI/CD Pipeline without Jenkins

Sidath Weerasinghe
4 min readJun 6, 2020

--

In this blog, I'm talking about the building a CI/CD pipeline without using the Jenkins server. We can build the CI/CD pipeline using the GitHub and the Docker Hub.

Below image shows that the CI/CD flow with the integration of GitHub, Docker Hub and on-premise server.

Prerequisites

Steps

Please follow the below steps to setup simple CI-CD pipeline with your on-premise servers.

Step 1

First, create a Docker file for your codebase to deploy that in a docker container.

Create a file as “Dockerfile” in the code repository home and add the below content or you can write the docker scripts with respect to your application.

FROM maven:3.6.3-jdk-11-slimENV PROJECT_HOME /mnt/project# add the directory to the path
ADD . /mnt/project
#Add a directory for logs
RUN mkdir -p /mnt/project/logs
# Run maven
RUN cd /mnt/project && mvn clean install
# Expose the port
EXPOSE 8080
# Run the jar file
CMD ["java","-jar","-DlogPath=/mnt/project/logs","/mnt/project/target/simplecicd-1.0.0.jar"]

Step 2

Install docker in your on-premise server.

sudo apt-get update
sudo apt-get remove docker docker-engine docker.io
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

To verification

docker --version
docker ps

Step 3

Install webhook to the on-premise server.

https://packages.debian.org/sid/amd64/webhook/downloadwget http://ftp.cn.debian.org/debian/pool/main/w/webhook/webhook_2.6.9-1+b1_amd64.debsudo dpkg -i webhook_2.6.9-1+b1_amd64.deb

Step 4

Create a repository on Docker Hub

Step 5

Add the secrets to GitHub Secrets under repository settings.

  • DOCKER_REPO — address of your repository at Docker Hub
  • DOCKER_USER — Docker Hub username
  • DOCKER_PASS — Docker Hub password

Step 6

Create a GitHub workflow.

Log into GitHub -> go to your repository -> Action -> New Workflow -> set up a workflow yourself

name: CI-CD Pipelineon:
push:
branches: [ master ]
jobs:
build:
name: Building the codebase
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
docker:
name: Publish - Docker Hub
runs-on: ubuntu-16.04
needs: [build]
env:
REPO: ${{ secrets.DOCKER_REPO }}
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
- name: Build Docker image
run: docker build -t $REPO:latest .
- name: Publish Docker image
run: docker push $REPO

Step 7

Go to the GitHub action tab and see the logs on the workflow executing.

After execution has done. Go to the Docker Hub and check the docker image under your repository.

Step 8

Now, you need to pull the docker image from the docker HUB and run it in the server.

docker pull sidathweerasinghe/simple-cicd-pipeline:latestdocker run -itd --name demoapp -p 8080:8080 sidathweerasinghe/simple-cicd-pipeline:latest

Step 9

Let’s config the webhook on the server.

Create a folder on the server name as “scripts”.

Then add the “hooks.json” config to that folder.

[
{
"id": "redeploy-webhook",
"execute-command": "/mnt/scripts/redeploy.sh",
"command-working-directory": "/mnt/scripts"
}
]

Create the “redeploy.sh” to redeploy the docker image after webhook triggered from the GitHub.

#!/bin/sh
docker pull sidathweerasinghe/simple-cicd-pipeline:latest
docker stop demoapp
docker system prune -f
docker run -itd --name demoapp -p 8080:8080 sidathweerasinghe/simple-cicd-pipeline:latest

Change the permission on that file.

chmod +x redeploy.sh

Step 10

Run the webhook service and test.

webhook -hooks /mnt/scripts/hooks.json  -verbose &

Test URL -

http://<ip>:9000/hooks/redeploy-webhook

Step 11

Integrate with the GitHub workflow

Add the WEBHOOK_URL to the GitHub Secrets and edit the workflow.

name: CI-CD Pipelineon:
push:
branches: [ master ]
jobs:
build:
name: Building the codebase
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
docker:
name: Publish - Docker Hub
runs-on: ubuntu-16.04
needs: [build]
env:
REPO: ${{ secrets.DOCKER_REPO }}
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Login to Docker Hub
run: docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASS }}
- name: Build Docker image
run: docker build -t $REPO:latest .
- name: Publish Docker image
run: docker push $REPO
redeploy:
name: Trigger webhook to Redeploy
runs-on: ubuntu-16.04
needs: [docker]
steps:
- name: Invoke deployment hook
uses: joelwmale/webhook-action@master
env:
WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }}
data: "{ 'myField': 'myFieldValue'}"

Go to your GitHub action tab and see your workflow logs.

For more detail refer my GitHub repository — https://github.com/SidathWeerasinghe/simple-cicd-pipeline

You can watch this tutorial video —

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response