Skip to content

Update build.yml

Update build.yml #14

Workflow file for this run

name: CI/CD
on: [push]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8' # You can use the version you need
# Cache Python dependencies
- name: Cache Python dependencies
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-python-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-python-
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
deploy_to_production:
runs-on: ubuntu-latest
needs: [build-and-test]
steps:
- uses: actions/checkout@v2
- name: Configure AWS Credentials
run: |
echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id
echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key
- name: Set AWS Region
run: |
echo "AWS_REGION=us-east-1" >> $GITHUB_ENV # Replace with your AWS region
- name: Deploy to EKS
run: |
# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
# Install awscli
pip install awscli --upgrade
aws --version
# Configure AWS CLI with your credentials
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }}
aws eks update-kubeconfig --name my-cluster --region ${{ env.AWS_REGION }}
# Cache Docker layers
echo "Cache Docker layers"
DOCKER_CACHE_DIR=/tmp/.buildx-cache
mkdir -p $DOCKER_CACHE_DIR
# Cache Docker layers
docker buildx create --use
docker buildx build --cache-from type=local,src=$DOCKER_CACHE_DIR --cache-to type=local,dest=$DOCKER_CACHE_DIR,mode=max --tag ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA .
# Build and tag the Docker image
LATEST_SHA=$(git rev-parse HEAD)
docker build -t ${{ secrets.REPOSITORY }}:$LATEST_SHA .
docker tag ${{ secrets.REPOSITORY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA
docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA
docker tag ${{ secrets.REPOSITORY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest
docker push ${{ secrets.IMAGE_REGISTRY }}:latest
# Check if the deployment exists
DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.REPOSITORY }} -n default --ignore-not-found)
if [ -z "$DEPLOYMENT_EXISTS" ]; then
echo "Deployment does not exist. Creating deployment."
kubectl create deployment ${{ secrets.REPOSITORY }} --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default
else
echo "Deployment exists. Updating deployment."
kubectl set image deployment/${{ secrets.REPOSITORY }} ${{ secrets.REPOSITORY }}=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default
fi