This file contains hidden or 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: 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 | |
| LAMBDA_FUNCTION_NAME: lamdanew2234 | |
| 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 | |
| outputs: | |
| image-uri: ${{ steps.build-image.outputs.image }} | |
| 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: | |
| needs: docker | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: ${{ secrets.AWS_ROLE_ARN }} | |
| role-session-name: GitHubActions-Deploy | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Update Lambda function | |
| env: | |
| IMAGE_URI: ${{ needs.docker.outputs.image-uri }} | |
| run: | | |
| aws lambda update-function-code \ | |
| --function-name ${{ env.LAMBDA_FUNCTION_NAME }} \ | |
| --image-uri $IMAGE_URI | |
| - name: Wait for Lambda function update to complete | |
| run: | | |
| aws lambda wait function-updated \ | |
| --function-name ${{ env.LAMBDA_FUNCTION_NAME }} | |