Files
cfw-autumn/.github/workflows/build.yml
2025-12-03 10:30:23 +00:00

144 lines
4.6 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Build and Push to ECR
on:
push:
branches:
- main
- staging
workflow_dispatch:
inputs:
tag:
description: 'Manual deploy'
required: false
default: 'manual'
env:
AWS_REGION: us-west-2 # Change to your AWS region
ECR_REPOSITORY: autumn # Change to your ECR repository name
jobs:
checks:
name: Type Check # To add: Lint check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.2
- name: Install dependencies
run: bun install
- name: Run TypeScript type check
run: |
cd server && bun ts
build-and-push:
name: Build and Push Docker Image
needs: checks
runs-on: blacksmith-16vcpu-ubuntu-2404
permissions:
id-token: write
contents: read
pull-requests: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Extract metadata for Docker
id: meta
run: |
BRANCH_NAME=${GITHUB_REF#refs/heads/}
COMMIT_SHA=${GITHUB_SHA::8}
# Sanitize branch name for Docker tag (replace / with -)
SAFE_BRANCH=$(echo $BRANCH_NAME | sed 's/\//-/g')
echo "branch=$SAFE_BRANCH" >> $GITHUB_OUTPUT
echo "sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
# Set custom tag if provided
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.tag }}" ]; then
echo "custom_tag=${{ github.event.inputs.tag }}" >> $GITHUB_OUTPUT
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push Docker image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG_SHA: ${{ steps.meta.outputs.sha }}
IMAGE_TAG_BRANCH: ${{ steps.meta.outputs.branch }}
run: |
# Build single combined tag: branch-sha
COMBINED_TAG="${IMAGE_TAG_BRANCH}-${IMAGE_TAG_SHA}"
TAGS="${ECR_REGISTRY}/${ECR_REPOSITORY}:${COMBINED_TAG}"
# Build and push
docker buildx build \
--platform linux/amd64 \
--push \
--provenance=false \
--sbom=false \
--cache-from type=registry,ref=${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG_BRANCH} \
--cache-to type=inline \
--tag ${TAGS//,/ --tag } \
-f docker/Dockerfile \
.
- name: Output image details
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG_SHA: ${{ steps.meta.outputs.sha }}
IMAGE_TAG_BRANCH: ${{ steps.meta.outputs.branch }}
run: |
COMBINED_TAG="${IMAGE_TAG_BRANCH}-${IMAGE_TAG_SHA}"
echo "✅ Image pushed successfully!"
echo "📦 Repository: ${ECR_REGISTRY}/${ECR_REPOSITORY}"
echo "🏷️ Tag: ${COMBINED_TAG}"
- name: Auto-deploy fix branches
if: github.ref == 'refs/heads/main'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEPLOY_URL: ${{ secrets.DEPLOY_URL }}
DEPLOY_SECRET: ${{ secrets.DEPLOY_SECRET }}
COMMIT_SHA: ${{ github.sha }}
run: |
# Get the PR that was merged (if any)
PR_DATA=$(gh pr list --state merged --search "$COMMIT_SHA" --json headRefName --limit 1)
BRANCH_NAME=$(echo "$PR_DATA" | jq -r '.[0].headRefName // empty')
if [[ "$BRANCH_NAME" == fix/* ]]; then
echo "🚀 Detected merged fix/ branch: $BRANCH_NAME"
echo "🔄 Triggering auto-deploy..."
curl -X POST "$DEPLOY_URL/api/deploy/github" \
-H "Content-Type: application/json" \
-H "x-deploy-secret: $DEPLOY_SECRET" \
-d "{\"commitSha\": \"$COMMIT_SHA\", \"deploymentType\": \"server\"}"
echo "✅ Auto-deploy triggered!"
else
echo " Not a fix/ branch (branch: ${BRANCH_NAME:-direct push}), skipping auto-deploy"
fi