Skip to content

Deploy to Dev Environment #21

Deploy to Dev Environment

Deploy to Dev Environment #21

Workflow file for this run

name: Deploy to Dev Environment
on:
workflow_dispatch:
inputs:
environment:
description: 'Which environment to deploy to'
required: true
default: 'development'
deploy_message:
description: 'Deployment message'
required: false
jobs:
deploy:
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: '17'
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Decode Google Cloud credentials
run: echo "${{ secrets.GOOGLE_APPLICATION_CREDENTIALS_JSON }}" | base64 --decode > gcloud-key.json
- name: Set GOOGLE_APPLICATION_CREDENTIALS environment variable
run: echo "GOOGLE_APPLICATION_CREDENTIALS=$GITHUB_WORKSPACE/gcloud-key.json" >> $GITHUB_ENV
- name: Build with Maven
env:
GOOGLE_APPLICATION_CREDENTIALS: ${{ env.GOOGLE_APPLICATION_CREDENTIALS }}
run: mvn clean package
- name: Upload JAR artifact
uses: actions/upload-artifact@v3
with:
name: app-jar
path: target/quizly-0.0.1-SNAPSHOT.jar
- name: Download artifact
uses: actions/download-artifact@v3
with:
name: app-jar
path: ./target
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_KEY }}" > ~/.ssh/ec2-key.pem
chmod 600 ~/.ssh/ec2-key.pem
- name: Add EC2 host to known_hosts
run: |
ssh-keyscan -H ${{ secrets.EC2_IP }} >> ~/.ssh/known_hosts
- name: Deploy to EC2
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
EC2_IP: ${{ secrets.EC2_IP }}
EC2_JAR_PATH: ${{ secrets.EC2_JAR_PATH }}
run: |
scp -i ~/.ssh/ec2-key.pem target/quizly-0.0.1-SNAPSHOT.jar ec2-user@${{ secrets.EC2_IP }}:${{ secrets.EC2_JAR_PATH }}
ssh -i ~/.ssh/ec2-key.pem ec2-user@${{ secrets.EC2_IP }} 'sudo systemctl restart quizly'