-
Notifications
You must be signed in to change notification settings - Fork 0
ci: build Docker image and push to registry from applicative's CI #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
af2b9c2
e024a22
b8167aa
144e52f
d6b51ae
c72157e
61aefa1
b0e8d75
e98ebdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,21 @@ parameters: | |
| publish-branch: | ||
| type: string | ||
| default: "main" | ||
| python-module: | ||
| type: string | ||
| default: "api_tabular" | ||
| api-port: | ||
| type: string | ||
| default: "8005" | ||
| docker-registry-url: | ||
| type: string | ||
| default: "registry.gitlab.com/etalab/data.gouv.fr/infra" | ||
| docker-image-name-tabular: | ||
| type: string | ||
| default: "tabular-api" | ||
| docker-image-name-metrics: | ||
| type: string | ||
| default: "metrics-api" | ||
|
|
||
| jobs: | ||
| lint: | ||
|
|
@@ -64,33 +79,56 @@ jobs: | |
| path: reports/python | ||
|
|
||
| build: | ||
| docker: | ||
| - image: ghcr.io/astral-sh/uv:python<< pipeline.parameters.python-version >>-trixie | ||
| machine: | ||
| image: ubuntu-2404:current | ||
| steps: | ||
| - checkout | ||
| - run: | ||
| name: Compute RELEASE_VERSION via setuptools_scm | ||
| name: Install uv and Python << pipeline.parameters.python-version >> | ||
| command: | | ||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| uv python install << pipeline.parameters.python-version >> | ||
| uv sync --frozen | ||
| - run: | ||
| name: Compute RELEASE_VERSION via setuptools_scm, and build wheel | ||
| command: | | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| uv pip install --system setuptools-scm | ||
| # derive from setuptools_scm or base version + build number | ||
| RELEASE_VERSION=$(python -m setuptools_scm) | ||
| echo "Building a wheel release with version $RELEASE_VERSION" | ||
| echo "Build number: $CIRCLE_BUILD_NUM" | ||
| echo "Commit hash: ${CIRCLE_SHA1:0:7}" | ||
| echo "Git tag: $CIRCLE_TAG" | ||
| - run: | ||
| name: Build a distributable package as a wheel release | ||
| command: | | ||
| echo "RELEASE_VERSION=$RELEASE_VERSION" >> "$BASH_ENV" | ||
| uv build --wheel | ||
| # Build already executed above; artifacts are in dist/ | ||
| - store_artifacts: | ||
| path: dist | ||
| - persist_to_workspace: | ||
| root: . | ||
| paths: | ||
| - . | ||
| - run: | ||
| name: Log in to Docker registry | ||
| command: | | ||
| echo "${DOCKER_REGISTRY_PASSWORD}" | docker login -u "oauth2" --password-stdin registry.gitlab.com | ||
| - run: | ||
| name: Build and push Docker images (Tabular API and Metrics API) | ||
| command: | | ||
| source "$BASH_ENV" | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| REGISTRY="<< pipeline.parameters.docker-registry-url >>" | ||
| # Docker tags allow only [a-zA-Z0-9_.-]; setuptools_scm can output e.g. 0.4.0.dev5+gabc1234 | ||
| DOCKER_TAG=$(echo "$RELEASE_VERSION" | tr '+' '-') | ||
| # Tabular API: tabular endpoints only | ||
| docker build --build-arg APP_MODULE=api_tabular.tabular.app:app_factory -t "${REGISTRY}/<< pipeline.parameters.docker-image-name-tabular >>:${DOCKER_TAG}" . | ||
| docker push "${REGISTRY}/<< pipeline.parameters.docker-image-name-tabular >>:${DOCKER_TAG}" | ||
| # Metrics API: metrics endpoints only | ||
| docker build --build-arg APP_MODULE=api_tabular.metrics.app:app_factory -t "${REGISTRY}/<< pipeline.parameters.docker-image-name-metrics >>:${DOCKER_TAG}" . | ||
| docker push "${REGISTRY}/<< pipeline.parameters.docker-image-name-metrics >>:${DOCKER_TAG}" | ||
|
|
||
| publish: | ||
| publish-pypi: | ||
| docker: | ||
| - image: ghcr.io/astral-sh/uv:python<< pipeline.parameters.python-version >>-trixie-slim | ||
| steps: | ||
|
|
@@ -110,7 +148,11 @@ workflows: | |
| requires: | ||
| - lint | ||
| - tests | ||
| - publish: | ||
| filters: | ||
| branches: | ||
| only: << pipeline.parameters.publish-branch >> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried running the pipeline by changing the filter to allow it on this branch?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it has been tested successfully using a test commit (later removed) on this branch: Images has been pushed successfully on our Docker registry. |
||
| context: org-global | ||
| - publish-pypi: | ||
| requires: | ||
| - build | ||
| filters: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| FROM astral/uv:python3.11-trixie-slim | ||
|
|
||
| # Which app to run (e.g. api_tabular.tabular.app:app_factory or api_tabular.metrics.app:app_factory) | ||
| ARG APP_MODULE=api_tabular.tabular.app:app_factory | ||
|
|
||
| # install needed apt packages | ||
| RUN apt-get update -y && \ | ||
| apt-get install -y --no-install-recommends git && \ | ||
| rm -rf /var/lib/apt/lists/* | ||
|
|
||
| # create user & group | ||
| RUN groupadd --system datagouv && \ | ||
| useradd --system --gid datagouv --create-home datagouv | ||
|
|
||
| # install | ||
| WORKDIR /home/datagouv | ||
| ADD . /home/datagouv/ | ||
| RUN uv sync --frozen | ||
| RUN chown -R datagouv:datagouv /home/datagouv | ||
|
|
||
| # run (ENV from ARG so shell can expand APP_MODULE at runtime) | ||
| USER datagouv | ||
| ENV APP_MODULE=${APP_MODULE} | ||
| # Use `python -m gunicorn` instead of `gunicorn` due to uv issue #15246: https://github.com/astral-sh/uv/issues/15246 | ||
| # Shell so APP_MODULE is expanded; bind to 8005 (map to different host ports when running both containers) | ||
| ENTRYPOINT ["/bin/sh", "-c"] | ||
| CMD ["uv run python -m gunicorn $APP_MODULE --bind 0.0.0.0:8005 --worker-class aiohttp.GunicornWebWorker --workers 2 --access-logfile -"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should use commit sha instead of a tag? I think it would be nice to have a single strategy between the different docker images naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but wouldn't that be more difficult if we had to quickly find an image on the Docker registry?