diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..53a2d46 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,74 @@ +name: CD + +on: + workflow_dispatch: + push: + branches: + - main + +env: + CONTAINER_NAME: mdredd + ECS_CLUSTER: mhacks-ecs + ECS_SERVICE: mhacks-mdredd + ECS_TASK_DEFINITION: mhacks-mdredd + +jobs: + deploy: + runs-on: ubuntu-24.04-arm + permissions: + id-token: write + contents: read + steps: + - name: Checkout repository + uses: actions/checkout@v6 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v6 + with: + role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/mhacks-mdredd-github + aws-region: us-east-2 + + - name: Login to ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build image + id: build + uses: redhat-actions/buildah-build@v2 + with: + image: ${{ env.CONTAINER_NAME }} + tags: latest ${{ github.sha }} + containerfiles: ./Dockerfile + + - name: Push image to ECR + id: push + uses: redhat-actions/push-to-registry@v2 + with: + image: ${{ steps.build.outputs.image }} + tags: ${{ steps.build.outputs.tags }} + registry: ${{ steps.login-ecr.outputs.registry }} + + - name: Substitute account and volume ids in task definition + run: | + sed -i \ + -e "s/AWS_ACCOUNT_ID/${{ secrets.AWS_ACCOUNT_ID }}/g" \ + -e "s/EFS_FILE_SYSTEM_ID/${{ secrets.EFS_FILE_SYSTEM_ID }}/g" \ + -e "s/EFS_ACCESS_POINT_ID/${{ secrets.EFS_ACCESS_POINT_ID }}/g" \ + task-definition.json + + - name: Render task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: task-definition.json + container-name: ${{ env.CONTAINER_NAME }} + image: ${{ steps.push.outputs.registry-path }} + + - name: Deploy to ECS + uses: aws-actions/amazon-ecs-deploy-task-definition@v2 + timeout-minutes: 10 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a68752b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + check: + name: Sync and import + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v6 + + - name: Install uv + uses: astral-sh/setup-uv@v10.2.0 + with: + version: "0.8.22" + python-version: "3.14" + + - name: Install dependencies + run: uv sync --locked + + - name: Import the app + env: + MDREDD_API_TOKEN: ci-token-not-a-real-secret-0123456789 + run: uv run python -c "import app.main" diff --git a/Dockerfile b/Dockerfile index abf95ce..8ef56b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,8 @@ RUN --mount=type=cache,target=/root/.cache/uv \ # The app only needs to write its database, so it runs unprivileged and owns # only the data directory. -RUN useradd --system --uid 10001 --no-create-home mdredd \ +RUN groupadd --system --gid 10001 mdredd \ + && useradd --system --uid 10001 --gid 10001 --no-create-home mdredd \ && mkdir -p /app/data \ && chown mdredd:mdredd /app/data USER mdredd diff --git a/task-definition.json b/task-definition.json new file mode 100644 index 0000000..96ae29d --- /dev/null +++ b/task-definition.json @@ -0,0 +1,89 @@ +{ + "containerDefinitions": [ + { + "name": "mdredd", + "image": "AWS_ACCOUNT_ID.dkr.ecr.us-east-2.amazonaws.com/mdredd:latest", + "cpu": 0, + "portMappings": [ + { + "containerPort": 8000, + "hostPort": 8000, + "protocol": "tcp", + "name": "mdredd-8000-tcp" + } + ], + "essential": true, + "environment": [ + { + "name": "MDREDD_DB_FILE", + "value": "/app/data/mdredd.db" + }, + { + "name": "MDREDD_CORS_ORIGINS", + "value": "[\"https://www.mhacks.org\"]" + } + ], + "mountPoints": [ + { + "sourceVolume": "mdredd-data", + "containerPath": "/app/data", + "readOnly": false + } + ], + "volumesFrom": [], + "secrets": [ + { + "name": "MDREDD_API_TOKEN", + "valueFrom": "arn:aws:ssm:us-east-2:AWS_ACCOUNT_ID:parameter/mhacks-secrets/MDREDD_API_TOKEN" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/mhacks-mdredd", + "awslogs-region": "us-east-2", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "healthCheck": { + "command": [ + "CMD", + "python", + "-c", + "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=3)" + ], + "interval": 30, + "timeout": 5, + "retries": 3, + "startPeriod": 60 + }, + "systemControls": [] + } + ], + "family": "mhacks-mdredd", + "executionRoleArn": "arn:aws:iam::AWS_ACCOUNT_ID:role/mhacks-mdredd-exec", + "taskRoleArn": "arn:aws:iam::AWS_ACCOUNT_ID:role/mhacks-mdredd-task", + "networkMode": "awsvpc", + "volumes": [ + { + "name": "mdredd-data", + "efsVolumeConfiguration": { + "fileSystemId": "EFS_FILE_SYSTEM_ID", + "transitEncryption": "ENABLED", + "authorizationConfig": { + "accessPointId": "EFS_ACCESS_POINT_ID", + "iam": "ENABLED" + } + } + } + ], + "placementConstraints": [], + "runtimePlatform": { + "cpuArchitecture": "ARM64", + "operatingSystemFamily": "LINUX" + }, + "requiresCompatibilities": ["FARGATE"], + "cpu": "1024", + "memory": "2048" +}