diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..802cda4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +# production +build +data +output + +# python +__pycache__ +.pytest_cache +.venv +.poetry + +# misc +.DS_Store + +# config +config/local.json +.envrc + +# logs +gobble.log +s3_upload.log diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml new file mode 100644 index 0000000..1c3cbf4 --- /dev/null +++ b/.github/workflows/container.yml @@ -0,0 +1,75 @@ +name: container + +on: + pull_request: + paths: + - Containerfile + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + env: + MBTA_V3_API_KEY: ${{ secrets.MBTA_V3_API_KEY }} + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Build container image + uses: docker/build-push-action@v6 + with: + context: . + file: ./Containerfile + push: false + tags: gobble:test + load: true + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + GIT_ABR_VERSION=${{ github.sha }} + + - name: Create config file + run: | + cat >> ./local.json << EOF + { + "mbta": { + "v3_api_key": "${{ secrets.MBTA_V3_API_KEY }}" + }, + "modes": ["cr", "bus"], + "gtfs": { + "refresh_interval_days": 7 + }, + "DATADOG_TRACE_ENABLED": true + } + EOF + + - name: Test container starts successfully + run: | + # Start the container in the background + docker run -d \ + -v ./local.json:/app/config/local.json:z \ + -v ./data:/app/data:z \ + --name gobble \ + gobble:test + + # Wait for the container to start + sleep 10 + + # Check if container is still running + if ! docker ps | grep -q gobble; then + echo "Container failed to start" + docker logs gobble + exit 1 + fi + + # Check if there are any files in data directory + find data -type f -print -quit || exit 1 + + # Cleanup + docker stop gobble + docker rm gobble diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..e9a4e08 --- /dev/null +++ b/Containerfile @@ -0,0 +1,18 @@ +FROM python:3.12-slim + +WORKDIR /app + +# Install uv for Python dependency management +COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv + +# Copy source code +COPY . . + +# Install dependencies +RUN uv sync + +# Set environment variables +ENV PYTHONUNBUFFERED=1 +ENV PYTHONPATH=/app/src + +ENTRYPOINT ["uv", "run", "python3", "src/gobble.py"] diff --git a/README.md b/README.md index c63077a..8325793 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # gobble ![lint](https://github.com/transitmatters/gobble/actions/workflows/lint.yml/badge.svg?branch=main) ![test](https://github.com/transitmatters/gobble/actions/workflows/test.yml/badge.svg?branch=main) +![deploy](https://github.com/transitmatters/gobble/actions/workflows/deploy.yml/badge.svg?branch=main) ![Screenshot in action](docs/screenshot.png) @@ -18,6 +19,20 @@ Gobble is a service that reads the [MBTA V3 Streaming API](https://www.mbta.com/ 3. Run `uv run src/gobble.py` to start. 4. Output will be in `data/` in your current working directory. Good luck! +### Container + +You can also run Gobble inside a container using the following `docker build` and `docker run` commands + +```bash +docker build -t gobble -f Containerfile . +docker run \ + -v ./config/local.json:/app/config/local.json:z \ + -v ./data:/app/data:z \ + gobble:latest +``` + +Output will be in `data/` in your current working directory. Good luck! + ### Linting You can run the linter against any code changes with the following commands