72 lines
2.6 KiB
YAML
72 lines
2.6 KiB
YAML
name: Deploy jar to EC2
|
|
|
|
on:
|
|
push:
|
|
branches: [ "main" ]
|
|
|
|
permissions:
|
|
id-token: write # This is required for requesting the JWT
|
|
contents: read # This is required for actions/checkout
|
|
|
|
jobs:
|
|
build:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- name: Set up JDK 17
|
|
uses: actions/setup-java@v3
|
|
with:
|
|
java-version: '17'
|
|
distribution: 'temurin'
|
|
cache: maven
|
|
|
|
|
|
- name: Create application-local.properties file
|
|
run: |
|
|
echo "spring.datasource.url=${{ secrets.DB_URL }}" >> src/main/resources/application-local.properties
|
|
echo "spring.datasource.username=${{ secrets.DB_USERNAME }}" >> src/main/resources/application-local.properties
|
|
echo "spring.datasource.password=${{ secrets.DB_PASSWORD }}" >> src/main/resources/application-local.properties
|
|
echo "spring.datasource.driver-class-name=${{ secrets.DB_DRIVER }}" >> src/main/resources/application-local.properties
|
|
echo "spring.jpa.hibernate.ddl-auto=update" >> src/main/resources/application-local.properties
|
|
echo "spring.jpa.show-sql=true" >> src/main/resources/application-local.properties
|
|
|
|
- name: Build with Maven
|
|
run: mvn -B package --file pom.xml
|
|
|
|
- name: Configure AWS Credentials
|
|
id: configure-aws-credentials
|
|
uses: aws-actions/configure-aws-credentials@v4
|
|
with:
|
|
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.AWS_ROLE }}
|
|
aws-region: ${{ secrets.AWS_REGION }}
|
|
|
|
- name: Get Instance ID
|
|
id: get_instance_id
|
|
run: |
|
|
INSTANCE_ID=$(aws ec2 describe-instances --filters "Name=tag:Name,Values=safeqr-ec2" --query "Reservations[0].Instances[0].InstanceId" --output text)
|
|
echo "INSTANCE_ID=$INSTANCE_ID" >> $GITHUB_ENV
|
|
|
|
- name: Start SSM Port Forwarding
|
|
id: start-ssm-port-forwarding
|
|
run: |
|
|
aws ssm start-session --target ${{ env.INSTANCE_ID }} --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["1234"],"localPortNumber":["1234"]}' &
|
|
sleep 10
|
|
|
|
- name: Install netcat locally
|
|
run: sudo apt-get install -y netcat
|
|
|
|
- name: Send File to EC2
|
|
run: |
|
|
echo "Sending JAR file to EC2 instance..."
|
|
nc -w 3 127.0.0.1 1234 < target/app-0.0.1-SNAPSHOT.jar
|
|
continue-on-error: false
|
|
|
|
- name: Verify File on EC2
|
|
run: |
|
|
aws ssm send-command --instance-ids ${{ env.INSTANCE_ID }} --document-name "AWS-RunShellScript" --comment "Verify JAR file" --parameters 'commands=["ls -l /tmp/app-0.0.1-SNAPSHOT.jar"]'
|
|
|
|
|
|
|