Skip to content

feat: refactor deployment workflow for improved build and artifact ma… #72

feat: refactor deployment workflow for improved build and artifact ma…

feat: refactor deployment workflow for improved build and artifact ma… #72

Workflow file for this run

name: Build and Deploy to VM
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: 'maven'
# Configure Maven for reliability
- name: Configure Maven settings
run: |
mkdir -p ~/.m2
cat > ~/.m2/settings.xml << EOF
<settings>
<mirrors>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage-download.googleapis.com/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>
EOF
# Build with retries
- name: Build with Maven (with retries)
run: |
max_attempts=3
attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Build attempt $attempt of $max_attempts"
if mvn -B clean package -DskipTests; then
echo "Build succeeded"
break
else
echo "Build failed, retrying..."
attempt=$((attempt+1))
sleep 30
fi
done
if [ $attempt -gt $max_attempts ]; then
echo "Build failed after $max_attempts attempts"
exit 1
fi
# Verify JAR files exist
- name: Verify build artifacts
run: |
echo "Checking JAR files..."
for service in eureka-server api-gateway recommendation-service statistics-service user-tracking-service; do
if [ ! -f "$service/target/$service-*.jar" ] && [ ! -f "$service/target/$service.jar" ]; then
echo "ERROR: JAR file for $service not found!"
find $service/target -type f -name "*.jar" || echo "No JARs found"
exit 1
fi
done
# Critical step: Prepare Docker build context
- name: Prepare Docker build context
run: |
# Create vm-deploy directories
mkdir -p vm-deploy/eureka-server
mkdir -p vm-deploy/api-gateway
mkdir -p vm-deploy/recommendation-service
mkdir -p vm-deploy/statistics-service
mkdir -p vm-deploy/user-tracking-service
# Copy JAR files to vm-deploy directories
cp $(find eureka-server/target -name "eureka-server*.jar" | grep -v original) vm-deploy/eureka-server/eureka-server.jar
cp $(find api-gateway/target -name "api-gateway*.jar" | grep -v original) vm-deploy/api-gateway/api-gateway.jar
cp $(find recommendation-service/target -name "recommendation-service*.jar" | grep -v original) vm-deploy/recommendation-service/recommendation-service.jar
cp $(find statistics-service/target -name "statistics-service*.jar" | grep -v original) vm-deploy/statistics-service/statistics-service.jar
cp $(find user-tracking-service/target -name "user-tracking-service*.jar" | grep -v original) vm-deploy/user-tracking-service/user-tracking-service.jar
# Verify files were copied
echo "Verifying copied JAR files:"
ls -la vm-deploy/*/
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.VM_IP_ADDRESS }} >> ~/.ssh/known_hosts
- name: Deploy to Oracle VM
env:
ORACLE_VM_IP: ${{ secrets.VM_IP_ADDRESS }}
DOCKER_REGISTRY: ${{ secrets.OCI_REGISTRY }}
OCI_USERNAME: ${{ secrets.OCI_USERNAME }}
OCI_AUTH_TOKEN: ${{ secrets.OCI_AUTH_TOKEN }}
run: |
# Verify VM connection
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa opc@${ORACLE_VM_IP} "echo VM connection verified"
# Create directories on VM
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa opc@${ORACLE_VM_IP} "mkdir -p ~/music-analytics/vm-deploy/{eureka-server,api-gateway,recommendation-service,statistics-service,user-tracking-service}"
# Copy Dockerfiles and JAR files to VM
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa vm-deploy/eureka-server/Dockerfile vm-deploy/eureka-server/eureka-server.jar opc@${ORACLE_VM_IP}:~/music-analytics/vm-deploy/eureka-server/
# Repeat for other services...
# [Add similar scp commands for other services]
# Copy docker-compose file
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa cloud-deploy/docker-compose.direct.yml opc@${ORACLE_VM_IP}:~/music-analytics/vm-deploy/docker-compose.yml
# Build and run containers
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa opc@${ORACLE_VM_IP} "cd ~/music-analytics/vm-deploy && \
docker login ${DOCKER_REGISTRY} -u ${OCI_USERNAME} -p ${OCI_AUTH_TOKEN} && \
docker-compose down --remove-orphans || true && \
docker-compose up -d && \
docker-compose ps"
# Debug command
- name: Debug on failure
if: failure()
run: |
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa opc@${ORACLE_VM_IP} '
echo "==== EUREKA SERVER LOGS ====" &&
docker logs vm-deploy-eureka-server-1 || echo "No logs available"
'