Skip to content

Update deploy.yml

Update deploy.yml #22

Workflow file for this run

name: Deploy Notes Backend
on:
pull_request:
types:
- closed
branches:
- master
jobs:
deploy:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
persist-credentials: true # Allow pushing to the repo later
- name: Setup Git config for creating PR
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Setup SSH
uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.DEPLOY_KEY }}
- name: Add server to known hosts
run: |
ssh-keyscan -H ${{ secrets.DEPLOY_SERVER_IP }} > ~/.ssh/known_hosts
- name: Build JAR
run: |
./gradlew bootJar
- name: Get JAR Version from build.gradle.kts
id: get_version
run: |
VERSION=$(grep -oP 'version\s*=\s*"\K[^"]+' build.gradle.kts)
echo "JAR_VERSION=$VERSION" >> $GITHUB_ENV
- name: Check if JAR exists on server
id: check_jar
run: |
JAR_NAME="spring_boot_crash_course-${{ env.JAR_VERSION }}.jar"
REMOTE_SERVER="admin@${{ secrets.DEPLOY_SERVER_IP }}"
REMOTE_JAR_DIR="/opt/notes"
if ssh $REMOTE_SERVER "[ -f $REMOTE_JAR_DIR/$JAR_NAME ]"; then
echo "JAR_ALREADY_EXISTS=true" >> $GITHUB_ENV
else
echo "JAR_ALREADY_EXISTS=false" >> $GITHUB_ENV
fi
- name: Bump version locally if JAR exists
if: env.JAR_ALREADY_EXISTS == 'true'
run: |
VERSION=$(grep -oP 'version\s*=\s*"\K[^"]+' build.gradle.kts)
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
sed -i "s/version = \".*\"/version = \"$NEW_VERSION\"/" build.gradle.kts
BRANCH_NAME="auto/bump-version-to-$NEW_VERSION"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
git checkout -b $BRANCH_NAME
git add build.gradle.kts
git commit -m "Bump version to $NEW_VERSION"
git push origin $BRANCH_NAME
- name: Create Pull Request manually
if: env.JAR_ALREADY_EXISTS == 'true'
run: |
PR_TITLE="Bump version to ${{ env.NEW_VERSION }} automatically"
PR_BODY="This PR was created automatically because the deployed JAR version already exists."
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
https://api.github.com/repos/${{ github.repository }}/pulls \
-d "{
\"title\": \"$PR_TITLE\",
\"body\": \"$PR_BODY\",
\"head\": \"${{ env.BRANCH_NAME }}\",
\"base\": \"master\"
}"
- name: Trigger auto-merge workflow
if: env.JAR_ALREADY_EXISTS == 'true'
uses: peter-evans/workflow-dispatch@v3
with:
repository: ${{ github.repository }}
workflow: auto-merge.yml # Ensure the name of your merge workflow is correct
ref: refs/heads/master
token: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy JAR to Server
if: env.JAR_ALREADY_EXISTS == 'false'
run: |
JAR_NAME="spring_boot_crash_course-${{ env.JAR_VERSION }}.jar"
LOCAL_JAR_PATH="build/libs/$JAR_NAME"
REMOTE_SERVER="admin@${{ secrets.DEPLOY_SERVER_IP }}"
REMOTE_JAR_DIR="/opt/notes"
TIMESTAMP=$(date +%Y%m%d%H%M%S) # Create timestamp
BACKUP_JAR_NAME="notes_$TIMESTAMP.jar" # Backup name with timestamp
# Copy new JAR to server
rsync -avz -e "ssh" $LOCAL_JAR_PATH $REMOTE_SERVER:$REMOTE_JAR_DIR/$JAR_NAME
ssh $REMOTE_SERVER << EOF
# Backup the current notes.jar
if [ -f $REMOTE_JAR_DIR/notes.jar ]; then
cp $REMOTE_JAR_DIR/notes.jar $REMOTE_JAR_DIR/$BACKUP_JAR_NAME
fi
# Replace with the new jar
mv $REMOTE_JAR_DIR/$JAR_NAME $REMOTE_JAR_DIR/notes.jar
# Restart service
sudo systemctl restart notes.service
EOF