CD Workflow #7
Workflow file for this run
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: CD Workflow | |
on: [workflow_dispatch] | |
jobs: | |
deploy: | |
name: Build Docker Image and Deploy to EC2 | |
runs-on: ubuntu-24.04 | |
environment: Development | |
steps: | |
- name: Check out Code | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: '21' | |
cache: 'maven' | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v3 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} | |
- name: Extract Version | |
id: extract-version | |
run: | | |
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Log Version | |
run: | | |
echo "Extracted Version: ${{ env.VERSION }}" | |
- name: Build and Tag Docker Image | |
run: | | |
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:${{ env.VERSION }} . | |
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:${{ env.VERSION }} ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:latest | |
- name: Push Docker Image to Docker Hub | |
run: | | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:${{ env.VERSION }} | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:latest | |
- 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: ${{ vars.AWS_REGION }} | |
- name: Deploy to EC2 via SSM | |
run: | | |
aws ssm send-command \ | |
--document-name "AWS-RunShellScript" \ | |
--targets "Key=instanceIds,Values=${{ secrets.EC2_INSTANCE_ID }}" \ | |
--parameters 'commands=[ | |
"docker pull ${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:latest", | |
"docker stop e-commerce || true", | |
"docker rm e-commerce || true", | |
"docker run -d --name e-commerce \ | |
-p 8080:8080 \ | |
-e DB_URL=${{ secrets.DB_URL }} \ | |
-e DB_USER=${{ secrets.DB_USER }} \ | |
-e DB_PASSWORD=${{ secrets.DB_PASSWORD }} \ | |
-e DB_SCHEMA=${{ secrets.DB_SCHEMA }} \ | |
-e TOPIC_ARN=${{ secrets.TOPIC_ARN }} \ | |
${{ secrets.DOCKERHUB_USERNAME }}/e-commerce:latest" | |
]' \ | |
--timeout-seconds 600 \ | |
--comment "Deploying latest Docker image to EC2" \ | |
--output text |