BLogic Systems

Setup new project in Jenkins

Last updated on 

Create a new project in Jenkins

  • Login to Jenkins Dashboard
  • Click on New Item -> Enter the name of the project -> Select Freestyle project -> Click OK

Configure the project

Click on Configure

General

  • Check Git under Source Code Management
  • Enter the Repository URL
  • Select Credentials from the dropdown. Please select blogicsystems/******
  • Enter Branches to build as */staging
  • Check GitHub hook trigger for GITScm polling under Build Triggers

Build Steps

  • Click on Add build step -> Select Execute shell. Enter the following commands:
#!/bin/bash

# Build docker image
REGISTRY='ghcr.io'
OWNER='blogicsystems'
IMAGE_NAME='<project_name>'

echo '🐳  Build docker image with tag latest'

# ghcr.io/blogicsystems/<project_name>
docker build -t "$REGISTRY/$OWNER/$IMAGE_NAME:latest" . || exit

echo 'πŸš€  Build done!'
echo ''
echo '🌏  Publish image to Github registry'

USERNAME='blogicsystems'
CR_PAT='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'

# login to github registry
echo $CR_PAT | docker login ghcr.io -u $USERNAME --password-stdin

# push image to github registry
docker push $REGISTRY/$OWNER/$IMAGE_NAME:latest || exit
  • Click on Add build step -> Select Execute shell. Enter the following commands:
#!/bin/bash

SERVER_IP='192.168.1.194'
IMAGE_NAME='ghcr.io/blogicsystems/<project_name>'
CONTAINER_NAME='<project_name>'

echo "πŸš€ Deploying via remote SSH"

ssh -i "/var/jenkins_home/.ssh/id_rsa" "bls@${SERVER_IP}" \
  "docker pull ${IMAGE_NAME} \
  && docker compose -f composes/${CONTAINER_NAME}/docker-compose.yaml down \
  && docker compose -f composes/${CONTAINER_NAME}/docker-compose.yaml up -d \
  && docker system prune -f"

echo "πŸŽ‰ Successfully deployed, hooray!"

Post-build Actions

  • Click on Add post-build action -> Select Slack Notifications
  • Check on
    • Notify Build Start
    • Notify Success
    • Notify Aborted
    • Notify Not Built
    • Notify Unstable
  • Click on Advanced and enter the following
    • Notification message includes: commit list with authors and titles
    • Credentials: slack-bot-token
    • Check on Custom slack app bot user
    • Username: Jennie
    • Channel: bls_deployment

Save

Don’t forget to save the project configuration.

Setup GitHub Webhooks:

Add webhook to repo by browse repo on Github, go to Settings > Webhooks > Add webhook.

In the form Payload URL, insert the url below

http://115.79.141.254:23997/webhooks

I built a reverse proxy to redirect public ip to Jenkins container. So Jenkins can know when ever code are push.

Thank Long Nguyen for support me to forward port 23997.

Setup docker compose on Staging server

  • Clone the deployment repo:
git clone https://github.com/blogicsystems/staging-deployment
  • Create a folder <project_name> in the root folder.
  • Create docker-compose.yaml file in <project_name> folder.
version: "3.4"

services:
  api:
    image: "ghcr.io/blogicsystems/<project_name>"
    container_name: "<project_name>"
    ports:
      - "6969:80"
      - "9696:443"
    restart: "unless-stopped"
  • Don’t forget to push to main branch.