Skip to content
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

add build push docker image image in the publish ci #311

Merged
merged 22 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6c0eac5
add build push docker image
mo-dkrz Nov 14, 2024
b19aec9
bump version
mo-dkrz Nov 14, 2024
b24192e
add test health step in publish, refactored the dockerfiles of es and os
mo-dkrz Nov 14, 2024
710b8f4
furnished readme and changelog
mo-dkrz Nov 14, 2024
795fa87
added default values and removed the unnecessary env vars from docker…
mo-dkrz Nov 16, 2024
bd5cddf
add WEB_CONCURRENCY in entrypoint
mo-dkrz Nov 17, 2024
c97d6f0
add RUN_LOCAL_OS and RUN_LOCAL_ES option to configure backend in cont…
mo-dkrz Nov 17, 2024
7226fc1
better docs
mo-dkrz Nov 17, 2024
0e70af2
check logs on produced images
mo-dkrz Nov 17, 2024
feb24d2
added better logs, and considered all different scenarios
mo-dkrz Nov 17, 2024
e8d6fc4
made README well documented
mo-dkrz Nov 17, 2024
15b0d65
added better recognition criteria for es and os
mo-dkrz Nov 17, 2024
4826f19
reflect comment on first method of installation of readme
mo-dkrz Nov 17, 2024
3e67674
minor changes in readme
mo-dkrz Nov 17, 2024
34f36a1
resolve the req change
mo-dkrz Nov 19, 2024
a2ee00b
dismantled the backends from dockerfiles and chnaged the REAME
mo-dkrz Nov 24, 2024
12af96a
prepared publish ci action
mo-dkrz Nov 24, 2024
808ee37
added latest version on image links
mo-dkrz Nov 24, 2024
f4ab110
removed env var from dockerfiles and minor change in docker-compose
mo-dkrz Nov 27, 2024
10eba9b
added forgotten build stage in docker-compose es and os services
mo-dkrz Nov 27, 2024
c033dfb
added docs in README
mo-dkrz Nov 28, 2024
562f7b3
Merge branch 'main' into add-image
jonhealy1 Nov 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 149 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "v*.*.*" # Triggers when a tag like 'v3.2.0' is pushed

jobs:
build-and-publish:
build-and-publish-pypi:
name: Build and Publish Packages
runs-on: ubuntu-latest

Expand Down Expand Up @@ -58,3 +58,151 @@ jobs:

# Publish to PyPI
twine upload dist/*

build-and-push-images:
name: Build and Push Docker Images
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Elasticsearch image
id: meta-es
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/stac-fastapi-es
tags: |
type=raw,value=latest
type=ref,event=tag

- name: Build Elasticsearch image
uses: docker/build-push-action@v6
with:
context: .
file: dockerfiles/Dockerfile.ci.es
platforms: linux/amd64
push: false
load: true
tags: stac-fastapi-es:test
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Test Elasticsearch image
run: |
docker run -d --name stac-es \
-e APP_HOST=0.0.0.0 \
-e APP_PORT=8080 \
-e ES_HOST=localhost \
-e ES_PORT=9200 \
stac-fastapi-es:test

timeout=120
while [ $timeout -gt 0 ]; do
if docker inspect stac-es --format='{{.State.Health.Status}}' | grep -q 'healthy'; then
echo "Container is healthy"
break
fi
if [ $timeout -eq 0 ]; then
echo "Health check failed"
docker logs stac-es
docker stop stac-es
docker rm stac-es
exit 1
fi
sleep 5
timeout=$((timeout-5))
done

docker stop stac-es
docker rm stac-es

- name: Push Elasticsearch image
uses: docker/build-push-action@v6
with:
context: .
file: dockerfiles/Dockerfile.ci.es
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta-es.outputs.tags }}
labels: ${{ steps.meta-es.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Extract metadata for OpenSearch image
id: meta-os
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository_owner }}/stac-fastapi-os
tags: |
type=raw,value=latest
type=ref,event=tag

- name: Build OpenSearch image
uses: docker/build-push-action@v6
with:
context: .
file: dockerfiles/Dockerfile.ci.os
platforms: linux/amd64
push: false
load: true
tags: stac-fastapi-os:test
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Test OpenSearch image
run: |
docker run -d --name stac-os \
-e APP_HOST=0.0.0.0 \
-e APP_PORT=8080 \
-e OS_HOST=localhost \
-e OS_PORT=9200 \
stac-fastapi-os:test

timeout=120
while [ $timeout -gt 0 ]; do
if docker inspect stac-os --format='{{.State.Health.Status}}' | grep -q 'healthy'; then
echo "Container is healthy"
break
fi
if [ $timeout -eq 0 ]; then
echo "Health check failed"
docker logs stac-os
docker stop stac-os
docker rm stac-os
exit 1
fi
sleep 5
timeout=$((timeout-5))
done

docker stop stac-os
docker rm stac-os

- name: Push OpenSearch image
uses: docker/build-push-action@v6
with:
context: .
file: dockerfiles/Dockerfile.ci.os
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta-os.outputs.tags }}
labels: ${{ steps.meta-os.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v3.2.1] - 2024-11-14

### Added
- Added `dockerfiles/Dockerfile.ci.os` and `dockerfiles/Dockerfile.ci.es`, along with their respective entrypoints [#311](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/311)

### Changed
- Updated the `publish.yml` workflow to include Docker image publishing to GitHub Container Registry [#311](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/311)
- Improved the README with detailed descriptions of the new Docker images, providing guidance for images. [#311](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/311)

## [v3.2.0] - 2024-10-09

### Added
Expand Down
96 changes: 78 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,98 @@
- We are always welcoming contributions. For the development notes: [Contributing](CONTRIBUTING.md)


### To install from PyPI:
### Installing STAC-FastAPI from PyPI

```shell
To install STAC-FastAPI with Elasticsearch or OpenSearch backend support, run the following command:

For Elasticsearch:
```bash
pip install stac_fastapi.elasticsearch
```
or
```

For OpenSearch:
```bash
pip install stac_fastapi.opensearch
```

## Build Elasticsearch API backend
## Running STAC-FastAPI Elasticsearch or OpenSearch API on `localhost:8080`

```shell
docker-compose up elasticsearch
docker-compose build app-elasticsearch
Before starting, [Docker](https://docs.docker.com/get-started/) or [Podman](https://podman.io/docs) has to be installed and running on your machine.

jonhealy1 marked this conversation as resolved.
Show resolved Hide resolved
### Step 1: Configure the `.env` File

You need to provide a `.env` file to configure the environment variables. Here's a list of variables you can configure:

- `STAC_FASTAPI_TITLE`: Title of the API shown in the documentation (default: `stac-fastapi-elasticsearch` or `stac-fastapi-opensearch`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make it clear which parameters are required?

- `STAC_FASTAPI_DESCRIPTION`: Description of the API in the documentation
- `STAC_FASTAPI_VERSION`: API version (default: `2.1`)
- `APP_HOST`: Host to bind the server (default: `0.0.0.0`)
- `APP_PORT`: Port to bind the server (default: `8080`)
- `RELOAD`: Enable auto-reload for development (default: `true`)
- `ENVIRONMENT`: Runtime environment (default: `local`)
- `WEB_CONCURRENCY`: Number of worker processes (default: `10`)
- `ES_HOST`: Elasticsearch or OpenSearch host (default: `localhost`)
- `ES_PORT`: Elasticsearch port (default: `9200` for Elasticsearch, `9202` for OpenSearch)
- `ES_USE_SSL`: Enable SSL for Elasticsearch (default: `false`)
- `ES_VERIFY_CERTS`: Verify SSL certificates (default: `false`)
- `BACKEND`: Backend type (`elasticsearch` or `opensearch`)
- `STAC_FASTAPI_RATE_LIMIT`: API rate limit per client (default: `200/minute`)

> [!NOTE]
> The variables `ES_HOST`, `ES_PORT`, `ES_USE_SSL`, and `ES_VERIFY_CERTS` apply to both Elasticsearch and OpenSearch, so there is no need to rename the key names to `OS_` even if you're using OpenSearch.

### Step 2: Running the Backend with Elasticsearch

To run the backend with Elasticsearch, use the following command:

```bash
docker run -d \
--env-file .env \
-p 9200:9200 \
-p 8080:8080 \
ghcr.io/stac-utils/stac-fastapi-es:latest
```
or
```bash
podman run -d \
--env-file .env \
-p 9200:9200 \
-p 8080:8080 \
ghcr.io/stac-utils/stac-fastapi-es:latest
```

## Running Elasticsearch API on localhost:8080
### Step 3: Running the Backend with OpenSearch

```shell
docker-compose up app-elasticsearch
To run the backend with OpenSearch, use the following command:

```bash
docker run -d \
--env-file .env \
-p 9202:9202 \
-p 8080:8080 \
ghcr.io/stac-utils/stac-fastapi-os:latest
```
or
```bash
podman run -d \
--env-file .env \
-p 9202:9202 \
-p 8080:8080 \
ghcr.io/stac-utils/stac-fastapi-os:latest
```
### Step 4: Verifying the Backend is Running

By default, docker-compose uses Elasticsearch 8.x and OpenSearch 2.11.1.
If you wish to use a different version, put the following in a
file named `.env` in the same directory you run docker-compose from:
To check if the container is running, use the following command depending on whether you're using Docker or Podman:

```shell
ELASTICSEARCH_VERSION=7.17.1
OPENSEARCH_VERSION=2.11.0
```bash
docker ps
```
The most recent Elasticsearch 7.x versions should also work. See the [opensearch-py docs](https://github.com/opensearch-project/opensearch-py/blob/main/COMPATIBILITY.md) for compatibility information.
or
```bash
podman ps
```

## Interacting with the API

To create a new Collection:

Expand Down
75 changes: 75 additions & 0 deletions dockerfiles/Dockerfile.ci.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
FROM debian:bookworm-slim AS base

ARG STAC_FASTAPI_TITLE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is setting all values not specified in the .env file to empty strings. Could you modify to handle default values?

ARG STAC_FASTAPI_DESCRIPTION
ARG STAC_FASTAPI_VERSION
ARG APP_HOST
ARG APP_PORT
ARG RELOAD
ARG ENVIRONMENT
ARG WEB_CONCURRENCY
ARG ES_HOST
ARG ES_PORT
ARG ES_USE_SSL
ARG ES_VERIFY_CERTS
ARG BACKEND

ENV STAC_FASTAPI_TITLE=${STAC_FASTAPI_TITLE}
ENV STAC_FASTAPI_DESCRIPTION=${STAC_FASTAPI_DESCRIPTION}
ENV STAC_FASTAPI_VERSION=${STAC_FASTAPI_VERSION}
ENV APP_HOST=${APP_HOST}
ENV APP_PORT=${APP_PORT}
ENV RELOAD=${RELOAD}
ENV ENVIRONMENT=${ENVIRONMENT}
ENV WEB_CONCURRENCY=${WEB_CONCURRENCY}
ENV ES_HOST=${ES_HOST}
ENV ES_PORT=${ES_PORT}
ENV ES_USE_SSL=${ES_USE_SSL}
ENV ES_VERIFY_CERTS=${ES_VERIFY_CERTS}
ENV BACKEND=${BACKEND}

RUN apt-get update && \
apt-get install -y --no-install-recommends \
gcc \
curl \
python3 \
python3-pip \
python3-venv \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*

# set non-root user
RUN groupadd -g 1000 elasticsearch && \
useradd -u 1000 -g elasticsearch -s /bin/bash -m elasticsearch

# elasticsearch binaries and libraries
COPY --from=docker.elastic.co/elasticsearch/elasticsearch:8.11.0 /usr/share/elasticsearch /usr/share/elasticsearch

# ser ownership
RUN chown -R elasticsearch:elasticsearch /usr/share/elasticsearch

WORKDIR /app
COPY . /app

# stac-fastapi-es installation
RUN pip3 install --no-cache-dir --break-system-packages -e ./stac_fastapi/core && \
pip3 install --no-cache-dir --break-system-packages ./stac_fastapi/elasticsearch[server]

COPY elasticsearch/config/elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml

COPY dockerfiles/entrypoint-es.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh


ENV ES_JAVA_OPTS="-Xms512m -Xmx1g" \
PATH="/usr/share/elasticsearch/bin:${PATH}"


HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD \
curl --silent --fail http://${ES_HOST}:${ES_PORT}/_cluster/health || exit 1 && \
curl --silent --fail http://${APP_HOST}:${APP_PORT}/api.html || exit 1

EXPOSE $APP_PORT $ES_PORT

USER elasticsearch
ENTRYPOINT ["/entrypoint.sh"]
Loading