Skip to content

ecr

ecr #2

Workflow file for this run

name: Python CI Pipeline
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
env:
PYTHON_VERSION: 3.11
AWS_REGION: us-east-1
ECR_REPOSITORY: meta/hello-world
permissions:
id-token: write
contents: read
jobs:
# Step 1: Install and Build
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: app
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# Step 2: Testing
test:
runs-on: ubuntu-latest
needs: build
defaults:
run:
working-directory: app
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install test dependencies and run tests
run: |
pip install -r requirements.txt
# Step 3: Docker image build and push (optional)
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: GitHubActions
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and push it to ECR
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
# deploy:
# name: Deploy to EC2
# runs-on: ubuntu-latest
# needs: docker
# steps:
# - name: Setup SSH key
# run: |
# mkdir -p ~/.ssh
# echo -e "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_rsa
# chmod 600 ~/.ssh/id_rsa
# - name: Add EC2 host to known hosts
# run: |
# ssh-keyscan -H ${{ vars.EC2_HOST }} >> ~/.ssh/known_hosts
# - name: Deploy Docker container on EC2
# run: |
# ssh -i ~/.ssh/id_rsa ${{ vars.EC2_USER }}@${{ vars.EC2_HOST }} << 'EOF'
# docker pull manojkumar8008/myapp1:latest
# docker stop myapp || true
# docker rm myapp || true
# docker run -d --name myapp -p 80:8000 manojkumar8008/myapp1:latest
# EOF