test #16
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: deploy app | |
| on: | |
| push: | |
| branches: [main] | |
| env: | |
| AWS_REGION: eu-west-2 | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| id-token: write | |
| contents: read | |
| 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 }} | |
| aws-region: eu-west-2 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| cache-dependency-path: 'src/handler/package.json' | |
| - name: Install dependencies | |
| working-directory: "./src/handler" | |
| run: npm ci --production | |
| - name: Package Lambda function | |
| run: | | |
| cd src | |
| zip -r deployment.zip . -x "*.git*" -x "node_modules/aws-sdk/*" | |
| - name: Get artifacts bucket name | |
| id: bucket | |
| run: | | |
| BUCKET=$(aws s3api list-buckets \ | |
| --query "Buckets[?starts_with(Name, 'node-api-dev-lambda-artifacts')].Name" \ | |
| --output text) | |
| echo "name=${BUCKET}" >> $GITHUB_OUTPUT | |
| echo "Using bucket: ${BUCKET}" | |
| - name: Upload to S3 | |
| run: | | |
| aws s3 cp deployment.zip \ | |
| s3://${{ steps.bucket.outputs.name }}/deployment.zip \ | |
| --metadata "git-commit=${{ github.sha }},git-branch=${{ github.ref_name }},deployed-by=github-actions" | |
| echo "Uploaded deployment package to S3" | |
| - name: Update Lambda function code | |
| id: update-lambda | |
| run: | | |
| FUNCTION_NAME="node-api-dev-api" | |
| echo "Updating Lambda function: ${FUNCTION_NAME}" | |
| aws lambda update-function-code \ | |
| --function-name ${FUNCTION_NAME} \ | |
| --s3-bucket ${{ steps.bucket.outputs.name }} \ | |
| --s3-key deployment.zip \ | |
| --publish | |
| echo "Waiting for function update to complete..." | |
| aws lambda wait function-updated \ | |
| --function-name ${FUNCTION_NAME} | |
| VERSION=$(aws lambda get-function \ | |
| --function-name ${FUNCTION_NAME} \ | |
| --query 'Configuration.Version' \ | |
| --output text) | |
| echo "version=${VERSION}" >> $GITHUB_OUTPUT | |
| echo "Lambda updated to version: ${VERSION}" | |
| - name: Get API endpoint | |
| id: api | |
| run: | | |
| API_ID=$(aws apigatewayv2 get-apis \ | |
| --query "Items[?Name==\`node-api-dev-api\`].ApiId" \ | |
| --output text) | |
| if [ -z "$API_ID" ] || [ "$API_ID" == "None" ]; then | |
| echo "ERROR: API 'node-api-dev-api' not found." | |
| exit 1 | |
| fi | |
| ENDPOINT=$(aws apigatewayv2 get-api \ | |
| --api-id "$API_ID" \ | |
| --query 'ApiEndpoint' \ | |
| --output text) | |
| echo "endpoint=${ENDPOINT}" >> $GITHUB_OUTPUT | |
| echo "API endpoint: $ENDPOINT" | |