forked from JasonHaley/hello-python
-
Notifications
You must be signed in to change notification settings - Fork 0
111 lines (93 loc) · 2.77 KB
/
newaws.yml
File metadata and controls
111 lines (93 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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: python-app
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 }}