|
| 1 | +name: Python CI Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "master" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "master" ] |
| 8 | + |
| 9 | +env: |
| 10 | + PYTHON_VERSION: 3.11 |
| 11 | + AWS_REGION: us-east-1 |
| 12 | + ECR_REPOSITORY: meta/hello-world |
| 13 | + |
| 14 | +jobs: |
| 15 | + # Step 1: Install and Build |
| 16 | + build: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + defaults: |
| 19 | + run: |
| 20 | + working-directory: app |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Set up Python |
| 26 | + uses: actions/setup-python@v4 |
| 27 | + with: |
| 28 | + python-version: ${{ env.PYTHON_VERSION }} |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: | |
| 32 | + python -m pip install --upgrade pip |
| 33 | + pip install -r requirements.txt |
| 34 | +
|
| 35 | +
|
| 36 | + # Step 2: Testing |
| 37 | + test: |
| 38 | + runs-on: ubuntu-latest |
| 39 | + needs: build |
| 40 | + defaults: |
| 41 | + run: |
| 42 | + working-directory: app |
| 43 | + steps: |
| 44 | + - name: Checkout code |
| 45 | + uses: actions/checkout@v4 |
| 46 | + |
| 47 | + - name: Install test dependencies and run tests |
| 48 | + run: | |
| 49 | + pip install -r requirements.txt |
| 50 | +
|
| 51 | + # Step 3: Docker image build and push (optional) |
| 52 | + docker: |
| 53 | + runs-on: ubuntu-latest |
| 54 | + |
| 55 | + steps: |
| 56 | + - name: Checkout code |
| 57 | + uses: actions/checkout@v4 |
| 58 | + |
| 59 | + - name: Configure AWS credentials |
| 60 | + uses: aws-actions/configure-aws-credentials@v4 |
| 61 | + with: |
| 62 | + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} |
| 63 | + role-session-name: GitHubActions |
| 64 | + aws-region: ${{ env.AWS_REGION }} |
| 65 | + |
| 66 | + - name: Login to Amazon ECR |
| 67 | + id: login-ecr |
| 68 | + uses: aws-actions/amazon-ecr-login@v2 |
| 69 | + |
| 70 | + - name: Build, tag, and push image to Amazon ECR |
| 71 | + id: build-image |
| 72 | + env: |
| 73 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 74 | + IMAGE_TAG: ${{ github.sha }} |
| 75 | + run: | |
| 76 | + # Build a docker container and push it to ECR |
| 77 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . |
| 78 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 79 | + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT |
| 80 | + # deploy: |
| 81 | + # name: Deploy to EC2 |
| 82 | + # runs-on: ubuntu-latest |
| 83 | + # needs: docker |
| 84 | + # steps: |
| 85 | + # - name: Setup SSH key |
| 86 | + # run: | |
| 87 | + # mkdir -p ~/.ssh |
| 88 | + # echo -e "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_rsa |
| 89 | + # chmod 600 ~/.ssh/id_rsa |
| 90 | + |
| 91 | + # - name: Add EC2 host to known hosts |
| 92 | + # run: | |
| 93 | + # ssh-keyscan -H ${{ vars.EC2_HOST }} >> ~/.ssh/known_hosts |
| 94 | + |
| 95 | + # - name: Deploy Docker container on EC2 |
| 96 | + # run: | |
| 97 | + # ssh -i ~/.ssh/id_rsa ${{ vars.EC2_USER }}@${{ vars.EC2_HOST }} << 'EOF' |
| 98 | + # docker pull manojkumar8008/myapp1:latest |
| 99 | + # docker stop myapp || true |
| 100 | + # docker rm myapp || true |
| 101 | + # docker run -d --name myapp -p 80:8000 manojkumar8008/myapp1:latest |
| 102 | + # EOF |
| 103 | + |
| 104 | + |
0 commit comments