Update maven-build.yml

This commit is contained in:
Aiman Zafar 2024-11-26 14:42:18 +05:00 committed by GitHub
parent bdfb8a73ab
commit 20b863a2d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,10 +1,14 @@
name: CI/CD Pipelineson: name: CI/CD Pipeline
on:
push: push:
branches: branches:
- main - main
pull_request: pull_request:
branches: branches:
- mainjobs: - main
jobs:
setup: setup:
runs-on: self-hosted runs-on: self-hosted
steps: steps:
@ -13,8 +17,8 @@ name: CI/CD Pipelineson:
if ! command -v git &> /dev/null if ! command -v git &> /dev/null
then then
echo "Git not found, installing..." echo "Git not found, installing..."
sudo apt-get update sudo yum update -y
sudo apt-get install -y git sudo yum install -y git
else else
echo "Git is already installed." echo "Git is already installed."
fi fi
@ -23,37 +27,20 @@ name: CI/CD Pipelineson:
if ! command -v docker &> /dev/null if ! command -v docker &> /dev/null
then then
echo "Docker not found, installing..." echo "Docker not found, installing..."
sudo apt-get update sudo yum update -y
sudo apt-get install -y \ sudo yum install -y docker
apt-transport-https \ sudo systemctl start docker
ca-certificates \ sudo systemctl enable docker
curl \
gnupg-agent \
software-properties-common && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && \
sudo apt-get update && \
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
else else
echo "Docker is already installed." echo "Docker is already installed."
fi fi
- name: Check for Docker Compose
run: |
if ! command -v docker-compose &> /dev/null
then
echo "Docker Compose not found, installing..."
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose is already installed."
fi
- name: Check for Maven - name: Check for Maven
run: | run: |
if ! command -v mvn &> /dev/null if ! command -v mvn &> /dev/null
then then
echo "Maven not found, installing..." echo "Maven not found, installing..."
sudo apt-get update sudo yum update -y
sudo apt-get install -y maven sudo yum install -y maven
else else
echo "Maven is already installed." echo "Maven is already installed."
fi fi
@ -62,57 +49,76 @@ name: CI/CD Pipelineson:
echo "Validating installation..." echo "Validating installation..."
command -v git command -v git
command -v docker command -v docker
command -v docker-compose command -v mvn
command -v mvn build:
build:
runs-on: self-hosted runs-on: self-hosted
needs: setup needs: setup
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 - name: Build with Maven uses: actions/checkout@v3
run: ./mvnw -B package test: - name: Build with Maven
run: ./mvnw -B package
test:
runs-on: self-hosted runs-on: self-hosted
needs: build needs: build
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v3 - name: Run tests uses: actions/checkout@v3
run: ./mvnw test deploy: - name: Run tests
runs-on: self-hosted run: ./mvnw test
needs: test
steps: deploy:
- name: Checkout code runs-on: self-hosted
uses: actions/checkout@v3 - name: Verify JAR file needs: test
run: | steps:
echo "Verifying JAR file presence..." - name: Checkout code
ls -al /home/aiman/spring-petclinic/target/ uses: actions/checkout@v3
if [ -z "$(find /home/aiman/spring-petclinic/target/ -name 'spring-petclinic-3.3.0-SNAPSHOT.jar')" ]; then
echo "JAR file not found in target directory!" - name: Verify JAR file
exit 1 run: |
fi - name: Setup SSH echo "Verifying JAR file presence..."
# List the files in the target directory
ls -al /home/aiman/Documents/CI-CD/spring-petclinic/actions-runner/_work/spring-petclinic/spring-petclinic/target/
# Check if the JAR file exists and display a message if not
JAR_PATH="/home/aiman/Documents/CI-CD/spring-petclinic/actions-runner/_work/spring-petclinic/spring-petclinic/target/spring-petclinic-3.3.0-SNAPSHOT.jar"
if [ ! -f "$JAR_PATH" ]; then
echo "JAR file not found at $JAR_PATH"
exit 1
else
echo "JAR file found at $JAR_PATH"
fi
- name: Setup SSH
run: | run: |
echo "Setting up SSH..." echo "Setting up SSH..."
mkdir -p ~/.ssh mkdir -p ~/.ssh
chmod 700 ~/.ssh chmod 700 ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H $DEPLOY_SERVER >> ~/.ssh/known_hosts || true - name: Deploy application ssh-keyscan -H $DEPLOY_SERVER >> ~/.ssh/known_hosts || true
- name: Deploy application
env: env:
DEPLOY_SERVER: '103.151.111.242' DEPLOY_SERVER: '192.168.1.199'
DEPLOY_PORT: '8080' DEPLOY_PORT: '8080'
DEPLOY_USER: 'aiman' DEPLOY_USER: 'aiman'
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: | run: |
echo "Deploying application to $DEPLOY_SERVER" echo "Deploying application to $DEPLOY_SERVER"
scp -i ~/.ssh/id_rsa /home/aiman/spring-petclinic/target/spring-petclinic-3.3.0-SNAPSHOT.jar $DEPLOY_USER@$DEPLOY_SERVER:/home/aiman/spring-petclinic/ scp -i ~/.ssh/id_rsa /home/aiman/Documents/CI-CD/spring-petclinic/actions-runner/_work/spring-petclinic/spring-petclinic/target/spring-petclinic-3.3.0-SNAPSHOT.jar $DEPLOY_USER@$DEPLOY_SERVER:/home/aiman/deployments/
ssh -i ~/.ssh/id_rsa $DEPLOY_USER@$DEPLOY_SERVER "nohup java -jar /home/aiman/spring-petclinic/spring-petclinic-3.3.0-SNAPSHOT.jar > /dev/null 2>&1 &" ssh -i ~/.ssh/id_rsa $DEPLOY_USER@$DEPLOY_SERVER "nohup java -jar /home/aiman/deployments/spring-petclinic-3.3.0-SNAPSHOT.jar > /dev/null 2>&1 &"
rm -f ~/.ssh/id_rsa - name: Wait for application to start rm -f ~/.ssh/id_rsa
- name: Wait for application to start
run: | run: |
echo "Waiting for application to start..." echo "Waiting for application to start..."
for i in {1..30}; do for i in {1..30}; do
if curl -s http://103.151.111.242:8080 > /dev/null; then if curl -s http://192.168.1.199:8080 > /dev/null; then
echo "Application is up!" echo "Application is up!"
break break
else else
echo "Application is not up yet, retrying in 10 seconds..." echo "Application is not up yet, retrying in 10 seconds..."
sleep 10 sleep 10
fi fi
done done