Add cross-region sgs (#118) #418
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
| on: push | |
| name: Build | |
| jobs: | |
| build: | |
| name: Build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| # Avoid rate limit errors by logging into Docker registries | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v4 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| # Login to AWS to avoid rate limit | |
| - name: Configure AWS credentials | |
| id: aws-login | |
| uses: aws-actions/configure-aws-credentials@v6 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
| role-skip-session-tagging: true | |
| role-duration-seconds: 3600 | |
| aws-region: ap-southeast-1 | |
| - name: Login to AWS Public ECR | |
| uses: docker/login-action@v4 | |
| with: | |
| registry: public.ecr.aws | |
| env: | |
| AWS_REGION: us-east-1 | |
| - name: Bootstrap DB | |
| id: bootstrap_db | |
| run: | | |
| output=$(docker compose --profile somleng-bootstrap run -T somleng-bootstrap) | |
| account_sid=$(echo $output | grep -Po 'Account\s+SID:\s+\K\b[a-zA-Z0-9-]+\b') | |
| auth_token=$(echo $output | grep -Po 'Auth\s+Token:\s+\K\b[a-zA-Z0-9_-]+\b') | |
| phone_number=$(echo $output | grep -Po 'Phone\s+Number:\s+\K\b[a-zA-Z0-9-]+\b') | |
| sms_gateway_device_token=$(echo $output | grep -Po 'SMS\s+Gateway\s+Device\s+Token:\s+\K\b[a-zA-Z0-9-]+\b') | |
| echo "account_sid=$account_sid" >> $GITHUB_OUTPUT | |
| echo "auth_token=$auth_token" >> $GITHUB_OUTPUT | |
| echo "phone_number=$phone_number" >> $GITHUB_OUTPUT | |
| echo "sms_gateway_device_token=$sms_gateway_device_token" >> $GITHUB_OUTPUT | |
| - name: Run docker compose | |
| run: | | |
| docker compose up -d --wait | |
| - name: Start SMS Gateway | |
| run: | | |
| DEVICE_TOKEN="${{ steps.bootstrap_db.outputs.sms_gateway_device_token }}" docker compose up --wait -d sms-gateway | |
| - name: Test an outbound call | |
| run: | | |
| response=$(mktemp) | |
| http_code=$(curl -s \ | |
| -o "$response" \ | |
| -w "%{http_code}" \ | |
| -X POST "http://api.lvh.me:3000/2010-04-01/Accounts/${{ steps.bootstrap_db.outputs.account_sid }}/Calls" \ | |
| -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \ | |
| -u "${{ steps.bootstrap_db.outputs.account_sid }}:${{ steps.bootstrap_db.outputs.auth_token }}" \ | |
| --data-urlencode "Url=https://demo.twilio.com/docs/voice.xml" \ | |
| --data-urlencode "Method=GET" \ | |
| --data-urlencode "To=+299221234" \ | |
| --data-urlencode "From=${{ steps.bootstrap_db.outputs.phone_number }}" | |
| ) | |
| if [ "$http_code" -ge 400 ]; then | |
| if [ "$http_code" -lt 500 ]; then | |
| echo "Client error (HTTP $http_code)" | |
| echo "Response body:" | |
| cat "$response_file" | |
| else | |
| echo "Server error (HTTP $http_code)" | |
| echo "Dumping Somleng logs:" | |
| docker compose logs somleng | |
| fi | |
| exit 1 | |
| fi | |
| - name: Test an outbound SMS | |
| id: test_outbound_sms | |
| run: | | |
| response_file=$(mktemp) | |
| http_code=$(curl -s \ | |
| -o "$response_file" \ | |
| -w "%{http_code}" \ | |
| -X POST "http://api.lvh.me:3000/2010-04-01/Accounts/${{ steps.bootstrap_db.outputs.account_sid }}/Messages" \ | |
| -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \ | |
| -u "${{ steps.bootstrap_db.outputs.account_sid }}:${{ steps.bootstrap_db.outputs.auth_token }}" \ | |
| --data-urlencode "To=+299221234" \ | |
| --data-urlencode "Body=Hello World from Integration Test" \ | |
| --data-urlencode "From=${{ steps.bootstrap_db.outputs.phone_number }}" | |
| ) | |
| if [ "$http_code" -ge 400 ]; then | |
| if [ "$http_code" -lt 500 ]; then | |
| echo "Client error (HTTP $http_code)" | |
| echo "Response body:" | |
| cat "$response_file" | |
| else | |
| echo "Server error (HTTP $http_code)" | |
| echo "Dumping Somleng logs:" | |
| docker compose logs somleng | |
| fi | |
| exit 1 | |
| fi | |
| message_sid=$(jq -r '.sid' "$response_file") | |
| echo "message_sid=$message_sid" >> "$GITHUB_OUTPUT" | |
| - name: Assert SMS Gateway received message | |
| run: | | |
| echo "Waiting for SMS Gateway to receive the message..." | |
| MESSAGE="Hello World from Integration Test" | |
| MAX_RETRIES=15 | |
| SLEEP_INTERVAL=2 | |
| FOUND=0 | |
| for i in $(seq 1 $MAX_RETRIES); do | |
| if docker compose logs sms-gateway | grep -q "$MESSAGE"; then | |
| echo "✅ Message received in logs!" | |
| FOUND=1 | |
| break | |
| fi | |
| echo "Attempt $i/$MAX_RETRIES: message not yet in logs, waiting $SLEEP_INTERVAL seconds..." | |
| sleep $SLEEP_INTERVAL | |
| done | |
| if [ $FOUND -eq 0 ]; then | |
| echo "❌ Message not found in SMS Gateway logs after $((MAX_RETRIES * SLEEP_INTERVAL)) seconds." | |
| echo "Dumping full SMS Gateway logs for debugging:" | |
| docker compose logs sms-gateway | |
| exit 1 | |
| fi | |
| - name: Assert SMS Billing | |
| run: | | |
| echo "Waiting for pricing to be updated..." | |
| MAX_RETRIES=21 | |
| SLEEP_INTERVAL=3 | |
| FOUND=0 | |
| for i in $(seq 1 $MAX_RETRIES); do | |
| response=$(curl --fail -s -u "${{ steps.bootstrap_db.outputs.account_sid }}:${{ steps.bootstrap_db.outputs.auth_token }}" \ | |
| "http://api.lvh.me:3000/2010-04-01/Accounts/${{ steps.bootstrap_db.outputs.account_sid }}/Messages/${{ steps.test_outbound_sms.outputs.message_sid }}") | |
| price=$(echo $response | jq -r '.price') | |
| if [ "$price" != "null" ]; then | |
| echo "✅ Pricing updated!" | |
| FOUND=1 | |
| break | |
| fi | |
| echo "Attempt $i/$MAX_RETRIES: pricing not yet updated, waiting $SLEEP_INTERVAL seconds..." | |
| sleep $SLEEP_INTERVAL | |
| done | |
| if [ $FOUND -eq 0 ]; then | |
| echo "❌ Pricing not updated after $((MAX_RETRIES * SLEEP_INTERVAL)) seconds." | |
| echo "Dumping full response for debugging:" | |
| echo $response | |
| exit 1 | |
| fi | |
| deploy: | |
| name: Deploy | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v6 | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v6 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
| role-skip-session-tagging: true | |
| role-duration-seconds: 3600 | |
| aws-region: ap-southeast-1 | |
| - name: Deploy Website | |
| run: | | |
| aws s3 sync --acl public-read --exclude "*" --include "*.html" --content-type "text/html; charset=utf-8" public/website s3://www.somleng.org | |
| aws s3 sync --acl public-read --exclude "*.html" public/website s3://www.somleng.org | |
| - name: Invalidate Cache | |
| run: aws cloudfront create-invalidation --distribution-id E3962XCJFZ0KB1 --paths /\* |