Val213 patch add ci #10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Deploy Java Backend | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: maven | |
- name: Cache Maven repository | |
uses: actions/cache@v3 | |
with: | |
path: ~/.m2/repository | |
key: ${{ runner.os }}-maven-${{ hashFiles('**/backend/pom.xml') }} | |
restore-keys: | | |
${{ runner.os }}-maven- | |
- name: Build and test Java backend | |
run: mvn -B package --file backend/pom.xml | |
- name: Upload Java build artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: java-build | |
path: backend/target/*.jar | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Download Java build artifacts | |
uses: actions/download-artifact@v3 | |
with: | |
name: java-build | |
path: backend/target | |
- name: Deploy Java backend | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_KEY }} | |
run: | | |
echo "$SSH_PRIVATE_KEY" > deploy_key | |
chmod 600 deploy_key | |
scp -o StrictHostKeyChecking=no -i deploy_key -r backend/target/*.jar [email protected]:/opt/myapp || { echo 'SCP failed'; exit 1; } | |
ssh -o StrictHostKeyChecking=no -i deploy_key [email protected] 'bash /opt/myapp/deploy.sh' || { echo 'SSH command failed'; exit 1; } | |
rm deploy_key |