Skip to content

Instantly share code, notes, and snippets.

@YoucefMegoura
Created June 18, 2025 07:59
Show Gist options
  • Select an option

  • Save YoucefMegoura/4bf1247ae278a6cb444cf865c25d32f6 to your computer and use it in GitHub Desktop.

Select an option

Save YoucefMegoura/4bf1247ae278a6cb444cf865c25d32f6 to your computer and use it in GitHub Desktop.
Github Action for telitem/consentia project
name: Consentia API Pipeline
on:
push:
paths:
- src/**
- pom.xml
- Dockerfile
- .github/workflows/**
- compose/**
branches:
- develop
jobs:
build-jar:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Build with Maven
run: mvn clean package
build-docker-image:
runs-on: ubuntu-latest
needs:
- build-jar
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Extract version from pom.xml
id: app_version
run: echo ::set-output name=version::$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
- name: Login to Docker Hub
id: login_docker
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build & Push Docker image
id: build_and_push_docker_image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile
tags: |
${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:dev
${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ steps.app_version.outputs.version }}
push: true
build-args: |
JAR_VERSION=${{ steps.app_version.outputs.version }}
PROFILE=dev
deploy:
name: Deploy to Server
runs-on: ubuntu-latest
needs: build-docker-image
steps:
- name: Execute remote SSH commands using password
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.VPS_SSH_HOST }}
port: ${{ secrets.VPS_SSH_PORT }}
username: ${{ secrets.VPS_SSH_USERNAME }}
password: ${{ secrets.VPS_SSH_PASSWORD }}
script: |
#!/bin/sh
echo "=============== VARIABLES ================="
echo "GITHUB_REPOSITORY_NAME: ${{ github.event.repository.name }}"
echo "VPS_SSH_USERNAME: ${{ secrets.VPS_SSH_USERNAME }}"
echo "TARGET_DIR: /home/${{ secrets.VPS_SSH_USERNAME }}/consentia-apps"
echo "REPO_DIR: /home/${{ secrets.VPS_SSH_USERNAME }}/consentia-apps/consentia-core-temp"
echo "COMPOSE_TARGET_DIR: /home/${{ secrets.VPS_SSH_USERNAME }}/consentia-apps/compose"
echo "==========================================="
TARGET_DIR=/home/${{ secrets.VPS_SSH_USERNAME }}/consentia-apps
REPO_DIR=$TARGET_DIR/consentia-temp # Temporary directory for cloning
COMPOSE_TARGET_DIR=$TARGET_DIR/compose
# Ensure target directory exists
mkdir -p $TARGET_DIR
# Remove existing temporary repo directory and target compose directory if they exist
rm -rf $REPO_DIR
rm -rf $COMPOSE_TARGET_DIR
# Clone the repository into the temporary directory
git clone --depth 1 --branch develop https://${{ secrets.READ_GITHUB_REPOSITORY_TOKEN_ }}@github.com/telitem/${{ github.event.repository.name }}.git $REPO_DIR
# Copy the compose folder from the cloned repo to the target directory
cp -r $REPO_DIR/compose $COMPOSE_TARGET_DIR
# Remove the temporary cloned repository
rm -rf $REPO_DIR
# Navigate to the copied compose directory
cd $COMPOSE_TARGET_DIR
# Pull the latest image and restart services
docker compose -f compose.yml -f compose-dev.yml down
docker container rm -f $(docker ps -aq --filter "name=${{ github.event.repository.name }}")
docker rmi -f $(docker images | grep "${{ github.event.repository.name }}" | awk '{print $3}')
docker compose --project-name ${{ github.event.repository.name }} -f compose.yml -f compose-dev.yml up -d &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment