-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔄 add CI/CD workflow for swift-lift-user-profile and implement user p…
…rofile retrieval in lambda function
- Loading branch information
1 parent
945df41
commit 55f75cd
Showing
2 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
name: swift-lift-user-profile CI-CD | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'swift-lift-user-profile/**' # Run only if changes are in swift-lift-user-profile directory | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
# 1. Checkout the code | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
|
||
# 2. Read current version from versions.json | ||
- name: Read Current Version | ||
id: read_version | ||
run: | | ||
VERSION=$(jq -r '."swift-lift-user-profile"' versions.json) | ||
echo "Current version: $VERSION" | ||
echo "VERSION=${VERSION}" >> $GITHUB_ENV | ||
# 3. Increment the patch version | ||
- name: Increment Patch Version | ||
id: increment_version | ||
run: | | ||
OLD_VERSION=${{ env.VERSION }} | ||
IFS='.' read -r MAJOR MINOR PATCH <<< "$OLD_VERSION" | ||
NEW_PATCH=$((PATCH + 1)) | ||
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}" | ||
echo "New version: $NEW_VERSION" | ||
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | ||
# 4. Create a zip file for the directory | ||
- name: Zip Directory | ||
run: | | ||
ZIP_NAME=swift-lift-user-profile-${{ env.NEW_VERSION }}.zip | ||
cd swift-lift-user-profile | ||
zip -r ../$ZIP_NAME ./* | ||
echo "ZIP_NAME=$ZIP_NAME" >> $GITHUB_ENV | ||
# 5.1 Authenticate to AWS | ||
- name: Authenticate to AWS | ||
uses: aws-actions/configure-aws-credentials@v3 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: us-east-1 | ||
# 5.2 Back up release version to s3 | ||
- name: Upload File | ||
run: | | ||
aws s3 cp ${{ env.ZIP_NAME }} s3://mea-munera-lambda/swift-lift-user-profile/ | ||
# 6. Deploy the lambda function code | ||
- name: Deploy the lambda function code | ||
uses: imehedi/actions-awscli-v2@latest | ||
with: | ||
args: "lambda update-function-code \ | ||
--function-name swift-lift-user-profile \ | ||
--zip-file fileb://${{ env.ZIP_NAME }}" | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
AWS_DEFAULT_REGION: "us-east-1" | ||
|
||
# 7. Update the versions.json file in the root | ||
- name: Update Versions JSON | ||
run: | | ||
jq --arg version "${{ env.NEW_VERSION }}" '."swift-lift-user-profile" = $version' versions.json > versions.json.tmp | ||
mv versions.json.tmp versions.json | ||
cat versions.json | ||
# 8. Commit and Push the updated versions.json | ||
- name: Commit Versions JSON | ||
run: | | ||
git config --global user.name "github-actions[bot]" | ||
git config --global user.email "github-actions[bot]@users.noreply.github.com" | ||
git add versions.json | ||
git commit -m "Update swift-lift-user-profile version to ${{ env.NEW_VERSION }}" | ||
git push |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import boto3 | ||
import json | ||
from datetime import datetime, timedelta, timezone | ||
from boto3.dynamodb.conditions import Key | ||
from decimal import Decimal | ||
from botocore.exceptions import ClientError | ||
|
||
# Initialize DynamoDB | ||
dynamodb = boto3.resource('dynamodb') | ||
trips_table_name = "Swift-lift-club-portal-trips" | ||
users_table_name = "Swift-lift-club-portal-users" | ||
trips_table = dynamodb.Table(trips_table_name) | ||
users_table = dynamodb.Table(users_table_name) | ||
print("DynamoDB Table initialized successfully") | ||
|
||
def custom_serializer(obj): | ||
if isinstance(obj, Decimal): | ||
return float(obj) # Convert Decimal to float | ||
raise TypeError(f"Type {type(obj)} not serializable") | ||
|
||
|
||
def get_passenger_profile(passenger_id): | ||
""" | ||
Get passenger name from users table | ||
""" | ||
try: | ||
response = users_table.query( | ||
KeyConditionExpression=Key('passenger_id').eq(passenger_id) | ||
) | ||
if response.get('Items') and len(response['Items']) > 0: | ||
return response['Items'][0] | ||
return None | ||
except Exception as e: | ||
print(f"Error getting passenger name: {e}") | ||
return None | ||
|
||
def lambda_handler(event, context): | ||
body = {} | ||
statusCode = 200 | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
# print(f"Received event: {json.dumps(event, indent=4, default=custom_serializer)}") | ||
|
||
try: | ||
print("Processing operation for getting user profile") | ||
|
||
passenger_id = event.get("queryStringParameters").get("passenger_id") | ||
if not passenger_id: | ||
raise ValueError("Missing required fields: passenger_id") | ||
|
||
# Get passenger profile | ||
passenger_profile = get_passenger_profile(passenger_id) | ||
if not passenger_profile: | ||
raise ValueError(f"Passenger with ID {passenger_id} not found in users table") | ||
|
||
body = passenger_profile | ||
return { | ||
'statusCode': statusCode, | ||
'body': json.dumps(body, default=custom_serializer), | ||
'headers': headers | ||
} | ||
|
||
except ValueError as ve: | ||
print(f"Value error: {ve}") | ||
statusCode = 400 | ||
body = {"error": str(ve)} | ||
|
||
except ClientError as ce: | ||
print(f"Client error: {ce}") | ||
statusCode = 500 | ||
body = {"error": "DynamoDB error occurred"} | ||
|
||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
statusCode = 500 | ||
body = {"error": "Internal server error"} | ||
|
||
return { | ||
"statusCode": statusCode, | ||
"body": json.dumps(body), | ||
"headers": headers | ||
} |