CI/CD Pipeline #100
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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
build_and_push: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Make application.yml | |
run: | | |
cd ./src/main/resources | |
echo "${{ secrets.YML }}" > application.yml | |
- name: Make application-prod.yml | |
run: | | |
cd ./src/main/resources | |
echo "${{ secrets.PROD_YML }}" > application-prod.yml | |
- name: Gradle Caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Setup Gradle | |
uses: gradle/actions/[email protected] | |
- name: Build with Gradle Wrapper | |
run: ./gradlew clean build | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- name: Login to Amazon ECR Public | |
id: login-ecr-public | |
uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
registry-type: public | |
- name: Delete previous image in Amazon ECR Public | |
run: | | |
aws ecr-public batch-delete-image \ | |
--repository-name distribute/dev \ | |
--image-ids imageTag=latest \ | |
--region us-east-1 || echo "No existing image with tag latest found." | |
- name: Build, tag, and push docker image to Amazon ECR Public | |
run: | | |
docker build -t ${{ steps.login-ecr-public.outputs.registry }}/${{ secrets.DEV_ECR_REGISTRY_ALIAS }}/distribute/dev:latest . | |
docker push ${{ steps.login-ecr-public.outputs.registry }}/${{ secrets.DEV_ECR_REGISTRY_ALIAS }}/distribute/dev:latest | |
- name: Logout of Amazon ECR | |
run: docker logout ${{ steps.login-ecr-public.outputs.registry }} | |
deploy: | |
needs: build_and_push | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-access-key-id: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }} | |
aws-region: us-east-1 | |
- name: Login to Amazon ECR Public | |
run: | | |
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws | |
- name: SSH into EC2 and Deploy with Docker Compose | |
run: | | |
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ubuntu@${{ secrets.SERVER_IP }} << 'EOF' | |
sudo usermod -aG docker $USER || true | |
newgrp docker || true | |
# Stop existing containers | |
if [ -f docker-compose.yml ]; then | |
echo "Stopping existing containers..." | |
sudo docker-compose down | |
else | |
echo "No existing Docker Compose setup. Proceeding with deployment..." | |
fi | |
# Clean up previous files | |
sudo rm -f docker-compose.yml | |
EOF | |
- name: Install Docker if not present | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_IP }} | |
username: ubuntu | |
key: ${{ secrets.DEV_EC2_SSH_KEY }} | |
script: | | |
if ! command -v docker-compose >/dev/null 2>&1; then | |
echo "Installing Docker Compose..." | |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
else | |
echo "Docker Compose already installed." | |
fi | |
- name: Copy docker-compose.yml to EC2 | |
run: | | |
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
scp -o StrictHostKeyChecking=no -i ec2_key.pem ./docker-compose.yml ubuntu@${{ secrets.SERVER_IP }}:~/docker-compose.yml | |
- name: Start services using Docker Compose | |
run: | | |
echo "${{ secrets.DEV_EC2_SSH_KEY }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
ssh -o StrictHostKeyChecking=no -i ec2_key.pem ubuntu@${{ secrets.SERVER_IP }} << 'EOF' | |
cd /home/ubuntu | |
docker-compose -f docker-compose.yml pull | |
docker-compose -f docker-compose.yml up -d | |
EOF | |
- name: Cleanup SSH Key | |
run: rm -f ec2_key.pem |