1+ name : Deploy to AWS Lambda
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ - develop
8+ pull_request :
9+ branches :
10+ - main
11+ workflow_dispatch :
12+ inputs :
13+ environment :
14+ description : ' Environment to deploy to'
15+ required : true
16+ default : ' dev'
17+ type : choice
18+ options :
19+ - dev
20+ - staging
21+ - prod
22+
23+ permissions :
24+ id-token : write
25+ contents : read
26+
27+ env :
28+ AWS_REGION : ap-northeast-2
29+ ECR_REPOSITORY : invidious-api-lambda
30+
31+ jobs :
32+ build-and-deploy :
33+ runs-on : ubuntu-latest
34+
35+ strategy :
36+ matrix :
37+ include :
38+ - environment : dev
39+ branch : develop
40+ - environment : prod
41+ branch : main
42+
43+ # Only run the matrix job if the branch matches
44+ if : |
45+ (github.event_name == 'workflow_dispatch') ||
46+ (github.event_name == 'push' && github.ref_name == matrix.branch) ||
47+ (github.event_name == 'pull_request')
48+
49+ environment : ${{ matrix.environment }}
50+
51+ steps :
52+ - name : Checkout code
53+ uses : actions/checkout@v4
54+ with :
55+ fetch-depth : 0 # Need full history for git versioning
56+
57+ - name : Configure AWS credentials
58+ uses : aws-actions/configure-aws-credentials@v4
59+ with :
60+ role-to-assume : ${{ secrets.AWS_ROLE_ARN }}
61+ role-session-name : github-actions-${{ github.run_id }}
62+ aws-region : ${{ env.AWS_REGION }}
63+
64+ - name : Login to Amazon ECR
65+ id : login-ecr
66+ uses : aws-actions/amazon-ecr-login@v2
67+
68+ - name : Set up Docker Buildx
69+ uses : docker/setup-buildx-action@v3
70+
71+ - name : Generate image metadata
72+ id : meta
73+ run : |
74+ # Generate version tag
75+ VERSION=$(git describe --tags --always --dirty)
76+ TIMESTAMP=$(date +%Y%m%d%H%M%S)
77+
78+ # Set image tags
79+ if [ "${{ matrix.environment }}" == "prod" ]; then
80+ TAGS="${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:latest"
81+ TAGS="$TAGS,${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:$VERSION"
82+ TAGS="$TAGS,${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:prod-$TIMESTAMP"
83+ else
84+ TAGS="${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ matrix.environment }}"
85+ TAGS="$TAGS,${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ matrix.environment }}-$VERSION"
86+ TAGS="$TAGS,${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ matrix.environment }}-$TIMESTAMP"
87+ fi
88+
89+ echo "tags=$TAGS" >> $GITHUB_OUTPUT
90+ echo "version=$VERSION" >> $GITHUB_OUTPUT
91+ echo "timestamp=$TIMESTAMP" >> $GITHUB_OUTPUT
92+
93+ - name : Build and push Docker image
94+ uses : docker/build-push-action@v5
95+ with :
96+ context : .
97+ file : docker/Dockerfile.lambda
98+ platforms : linux/amd64,linux/arm64
99+ push : true
100+ tags : ${{ steps.meta.outputs.tags }}
101+ cache-from : type=gha
102+ cache-to : type=gha,mode=max
103+ build-args : |
104+ BUILD_DATE=${{ steps.meta.outputs.timestamp }}
105+ VERSION=${{ steps.meta.outputs.version }}
106+
107+ - name : Setup Terraform
108+ uses : hashicorp/setup-terraform@v3
109+ with :
110+ terraform_version : " 1.5.0"
111+
112+ - name : Terraform Init
113+ working-directory : terraform
114+ run : |
115+ terraform init \
116+ -backend-config="bucket=${{ secrets.TERRAFORM_STATE_BUCKET }}" \
117+ -backend-config="key=${{ matrix.environment }}/invidious-lambda/terraform.tfstate" \
118+ -backend-config="region=${{ env.AWS_REGION }}" \
119+ -backend-config="dynamodb_table=${{ secrets.TERRAFORM_STATE_LOCK_TABLE }}"
120+
121+ - name : Terraform Plan
122+ working-directory : terraform
123+ run : |
124+ terraform plan \
125+ -var="environment=${{ matrix.environment }}" \
126+ -var="image_tag=${{ matrix.environment }}-${{ steps.meta.outputs.version }}" \
127+ -var="aws_region=${{ env.AWS_REGION }}" \
128+ -out=tfplan
129+
130+ - name : Terraform Apply
131+ if : github.event_name == 'push' || github.event_name == 'workflow_dispatch'
132+ working-directory : terraform
133+ run : |
134+ terraform apply -auto-approve tfplan
135+
136+ - name : Update Lambda function
137+ if : github.event_name == 'push' || github.event_name == 'workflow_dispatch'
138+ run : |
139+ # Get the latest image URI
140+ IMAGE_URI="${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ matrix.environment }}-${{ steps.meta.outputs.version }}"
141+
142+ # Update Lambda function with new image
143+ aws lambda update-function-code \
144+ --function-name invidious-api-${{ matrix.environment }} \
145+ --image-uri $IMAGE_URI \
146+ --region ${{ env.AWS_REGION }}
147+
148+ # Wait for update to complete
149+ aws lambda wait function-updated \
150+ --function-name invidious-api-${{ matrix.environment }} \
151+ --region ${{ env.AWS_REGION }}
152+
153+ - name : Get deployment info
154+ if : github.event_name == 'push' || github.event_name == 'workflow_dispatch'
155+ working-directory : terraform
156+ run : |
157+ echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY
158+ echo "- **Environment**: ${{ matrix.environment }}" >> $GITHUB_STEP_SUMMARY
159+ echo "- **Version**: ${{ steps.meta.outputs.version }}" >> $GITHUB_STEP_SUMMARY
160+ echo "- **Timestamp**: ${{ steps.meta.outputs.timestamp }}" >> $GITHUB_STEP_SUMMARY
161+ echo "" >> $GITHUB_STEP_SUMMARY
162+ echo "### Endpoints" >> $GITHUB_STEP_SUMMARY
163+ echo "- **API Gateway**: $(terraform output -raw api_endpoint)" >> $GITHUB_STEP_SUMMARY
164+ if [ "$(terraform output -raw function_url)" != "null" ]; then
165+ echo "- **Function URL**: $(terraform output -raw function_url)" >> $GITHUB_STEP_SUMMARY
166+ fi
167+ echo "" >> $GITHUB_STEP_SUMMARY
168+ echo "### Resources" >> $GITHUB_STEP_SUMMARY
169+ echo "- **Lambda Function**: $(terraform output -raw lambda_function_name)" >> $GITHUB_STEP_SUMMARY
170+ echo "- **CloudWatch Logs**: [View Logs](https://console.aws.amazon.com/cloudwatch/home?region=${{ env.AWS_REGION }}#logsV2:log-groups/log-group/$(terraform output -raw cloudwatch_log_group))" >> $GITHUB_STEP_SUMMARY
0 commit comments