CI/CD Pipeline #1
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: | |
workflow_dispatch: | |
inputs: | |
dry_run: | |
description: 'Perform a dry run' | |
required: false | |
default: 'true' | |
jobs: | |
build: | |
if: github.event.inputs.dry_run != 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log in to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
with: | |
region: ${{ secrets.AWS_REGION }} | |
continue-on-error: false | |
- name: Build, tag, and push Docker image to Amazon ECR | |
run: | | |
IMAGE_URI="${{ secrets.AWS_ECR_REPOSITORY }}:latest" | |
echo "Building Docker image..." | |
docker build --platform linux/amd64 -t $IMAGE_URI . | |
echo "Tagging Docker image..." | |
docker tag $IMAGE_URI $IMAGE_URI | |
echo "Pushing Docker image to ECR..." | |
docker push $IMAGE_URI | |
continue-on-error: false | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Execute remote commands via SSH | |
if: github.event.inputs.dry_run != 'true' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
set -e | |
export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} | |
export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
export AWS_DEFAULT_REGION=${{ secrets.AWS_REGION }} | |
echo "Logging into AWS ECR..." | |
aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REPOSITORY }} | |
echo "Pulling Docker image..." | |
docker pull ${{ secrets.AWS_ECR_REPOSITORY }}:latest | |
echo "Stopping current Docker container..." | |
docker ps -q --filter ancestor=${{ secrets.AWS_ECR_REPOSITORY }}:latest | xargs -r docker stop | |
echo "Starting new Docker container..." | |
docker run -d -p 8080:8080 --env-file /path/to/.env ${{ secrets.AWS_ECR_REPOSITORY }}:latest | |
- name: Dry run - list Docker images | |
if: github.event.inputs.dry_run == 'true' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
echo "Listing Docker images..." | |
docker images | |
echo "Listing running Docker containers..." | |
docker ps | |
- name: Confirm server is up | |
if: github.event.inputs.dry_run != 'true' | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
echo "Waiting for server to start..." | |
sleep 30 # Adjust sleep time as needed | |
echo "Checking server status..." | |
if curl -s --head --request GET http://localhost:8080 | grep "200 OK" > /dev/null; then | |
echo "Server is up and running." | |
else | |
echo "Server is not running. Deployment failed." >&2 | |
exit 1 | |
fi |