Skip to content

Commit b24192e

Browse files
committed
add test health step in publish, refactored the dockerfiles of es and os
1 parent b19aec9 commit b24192e

File tree

5 files changed

+295
-16
lines changed

5 files changed

+295
-16
lines changed

.github/workflows/publish.yml

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,65 +58,148 @@ jobs:
5858
5959
# Publish to PyPI
6060
twine upload dist/*
61+
6162
build-and-push-images:
6263
name: Build and Push Docker Images
6364
runs-on: ubuntu-latest
6465
permissions:
6566
contents: read
6667
packages: write
67-
68+
6869
steps:
6970
- name: Checkout repository
7071
uses: actions/checkout@v4
71-
72+
7273
- name: Set up QEMU
7374
uses: docker/setup-qemu-action@v3
74-
75+
7576
- name: Set up Docker Buildx
7677
uses: docker/setup-buildx-action@v3
7778

7879
- name: Log in to GitHub Container Registry
79-
uses: docker/login-action@v3.3.0
80+
uses: docker/login-action@v3
8081
with:
8182
registry: ghcr.io
8283
username: ${{ github.actor }}
8384
password: ${{ secrets.GITHUB_TOKEN }}
84-
85+
8586
- name: Extract metadata for Elasticsearch image
8687
id: meta-es
87-
uses: docker/metadata-action@v5.5.1
88+
uses: docker/metadata-action@v5
8889
with:
89-
images: ghcr.io/stac-utils/stac-fastapi-es
90+
images: ghcr.io/${{ github.repository_owner }}/stac-fastapi-es
9091
tags: |
9192
type=raw,value=latest
9293
type=ref,event=tag
94+
95+
- name: Build Elasticsearch image
96+
uses: docker/build-push-action@v6
97+
with:
98+
context: .
99+
file: dockerfiles/Dockerfile.ci.es
100+
platforms: linux/amd64
101+
push: false
102+
load: true
103+
tags: stac-fastapi-es:test
104+
cache-from: type=gha
105+
cache-to: type=gha,mode=max
106+
107+
- name: Test Elasticsearch image
108+
run: |
109+
docker run -d --name stac-es \
110+
-e APP_HOST=0.0.0.0 \
111+
-e APP_PORT=8080 \
112+
-e ES_HOST=localhost \
113+
-e ES_PORT=9200 \
114+
stac-fastapi-es:test
93115
94-
- name: Build and push Elasticsearch Docker image
95-
uses: docker/[email protected]
116+
timeout=120
117+
while [ $timeout -gt 0 ]; do
118+
if docker inspect stac-es --format='{{.State.Health.Status}}' | grep -q 'healthy'; then
119+
echo "Container is healthy"
120+
break
121+
fi
122+
if [ $timeout -eq 0 ]; then
123+
echo "Health check failed"
124+
docker logs stac-es
125+
docker stop stac-es
126+
docker rm stac-es
127+
exit 1
128+
fi
129+
sleep 5
130+
timeout=$((timeout-5))
131+
done
132+
133+
docker stop stac-es
134+
docker rm stac-es
135+
136+
- name: Push Elasticsearch image
137+
uses: docker/build-push-action@v6
96138
with:
97139
context: .
98-
file: dockerfiles/Dockerfile.deploy.es
140+
file: dockerfiles/Dockerfile.ci.es
99141
platforms: linux/amd64,linux/arm64
100142
push: true
101143
tags: ${{ steps.meta-es.outputs.tags }}
102144
labels: ${{ steps.meta-es.outputs.labels }}
103145
cache-from: type=gha
104146
cache-to: type=gha,mode=max
105-
147+
106148
- name: Extract metadata for OpenSearch image
107149
id: meta-os
108-
uses: docker/metadata-action@v5.5.1
150+
uses: docker/metadata-action@v5
109151
with:
110-
images: ghcr.io/stac-utils/stac-fastapi-os
152+
images: ghcr.io/${{ github.repository_owner }}/stac-fastapi-os
111153
tags: |
112154
type=raw,value=latest
113155
type=ref,event=tag
156+
157+
- name: Build OpenSearch image
158+
uses: docker/build-push-action@v6
159+
with:
160+
context: .
161+
file: dockerfiles/Dockerfile.ci.os
162+
platforms: linux/amd64
163+
push: false
164+
load: true
165+
tags: stac-fastapi-os:test
166+
cache-from: type=gha
167+
cache-to: type=gha,mode=max
168+
169+
- name: Test OpenSearch image
170+
run: |
171+
docker run -d --name stac-os \
172+
-e APP_HOST=0.0.0.0 \
173+
-e APP_PORT=8080 \
174+
-e OS_HOST=localhost \
175+
-e OS_PORT=9200 \
176+
stac-fastapi-os:test
177+
178+
timeout=120
179+
while [ $timeout -gt 0 ]; do
180+
if docker inspect stac-os --format='{{.State.Health.Status}}' | grep -q 'healthy'; then
181+
echo "Container is healthy"
182+
break
183+
fi
184+
if [ $timeout -eq 0 ]; then
185+
echo "Health check failed"
186+
docker logs stac-os
187+
docker stop stac-os
188+
docker rm stac-os
189+
exit 1
190+
fi
191+
sleep 5
192+
timeout=$((timeout-5))
193+
done
114194
115-
- name: Build and push OpenSearch Docker image
116-
uses: docker/[email protected]
195+
docker stop stac-os
196+
docker rm stac-os
197+
198+
- name: Push OpenSearch image
199+
uses: docker/build-push-action@v6
117200
with:
118201
context: .
119-
file: dockerfiles/Dockerfile.deploy.os
202+
file: dockerfiles/Dockerfile.ci.os
120203
platforms: linux/amd64,linux/arm64
121204
push: true
122205
tags: ${{ steps.meta-os.outputs.tags }}

dockerfiles/Dockerfile.ci.es

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
FROM debian:bookworm-slim AS base
2+
3+
ARG STAC_FASTAPI_TITLE
4+
ARG STAC_FASTAPI_DESCRIPTION
5+
ARG STAC_FASTAPI_VERSION
6+
ARG APP_HOST
7+
ARG APP_PORT
8+
ARG RELOAD
9+
ARG ENVIRONMENT
10+
ARG WEB_CONCURRENCY
11+
ARG ES_HOST
12+
ARG ES_PORT
13+
ARG ES_USE_SSL
14+
ARG ES_VERIFY_CERTS
15+
ARG BACKEND
16+
17+
ENV STAC_FASTAPI_TITLE=${STAC_FASTAPI_TITLE}
18+
ENV STAC_FASTAPI_DESCRIPTION=${STAC_FASTAPI_DESCRIPTION}
19+
ENV STAC_FASTAPI_VERSION=${STAC_FASTAPI_VERSION}
20+
ENV APP_HOST=${APP_HOST}
21+
ENV APP_PORT=${APP_PORT}
22+
ENV RELOAD=${RELOAD}
23+
ENV ENVIRONMENT=${ENVIRONMENT}
24+
ENV WEB_CONCURRENCY=${WEB_CONCURRENCY}
25+
ENV ES_HOST=${ES_HOST}
26+
ENV ES_PORT=${ES_PORT}
27+
ENV ES_USE_SSL=${ES_USE_SSL}
28+
ENV ES_VERIFY_CERTS=${ES_VERIFY_CERTS}
29+
ENV BACKEND=${BACKEND}
30+
31+
RUN apt-get update && \
32+
apt-get install -y --no-install-recommends \
33+
gcc \
34+
curl \
35+
python3 \
36+
python3-pip \
37+
python3-venv \
38+
&& apt-get clean && \
39+
rm -rf /var/lib/apt/lists/*
40+
41+
# set non-root user
42+
RUN groupadd -g 1000 elasticsearch && \
43+
useradd -u 1000 -g elasticsearch -s /bin/bash -m elasticsearch
44+
45+
# elasticsearch binaries and libraries
46+
COPY --from=docker.elastic.co/elasticsearch/elasticsearch:8.11.0 /usr/share/elasticsearch /usr/share/elasticsearch
47+
48+
# ser ownership
49+
RUN chown -R elasticsearch:elasticsearch /usr/share/elasticsearch
50+
51+
WORKDIR /app
52+
COPY . /app
53+
54+
# stac-fastapi-es installation
55+
RUN pip3 install --no-cache-dir --break-system-packages -e ./stac_fastapi/core && \
56+
pip3 install --no-cache-dir --break-system-packages ./stac_fastapi/elasticsearch[server]
57+
58+
COPY elasticsearch/config/elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml
59+
60+
COPY dockerfiles/entrypoint-es.sh /entrypoint.sh
61+
RUN chmod +x /entrypoint.sh
62+
63+
64+
ENV ES_JAVA_OPTS="-Xms512m -Xmx1g" \
65+
PATH="/usr/share/elasticsearch/bin:${PATH}"
66+
67+
68+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD \
69+
curl --silent --fail http://${ES_HOST}:${ES_PORT}/_cluster/health || exit 1 && \
70+
curl --silent --fail http://${APP_HOST}:${APP_PORT}/api.html || exit 1
71+
72+
EXPOSE $APP_PORT $ES_PORT
73+
74+
USER elasticsearch
75+
ENTRYPOINT ["/entrypoint.sh"]

dockerfiles/Dockerfile.ci.os

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
FROM debian:bookworm-slim AS base
2+
3+
ARG STAC_FASTAPI_TITLE
4+
ARG STAC_FASTAPI_DESCRIPTION
5+
ARG STAC_FASTAPI_VERSION
6+
ARG APP_HOST
7+
ARG APP_PORT
8+
ARG RELOAD
9+
ARG ENVIRONMENT
10+
ARG WEB_CONCURRENCY
11+
ARG ES_HOST
12+
ARG ES_PORT
13+
ARG ES_USE_SSL
14+
ARG ES_VERIFY_CERTS
15+
ARG BACKEND
16+
ARG STAC_FASTAPI_RATE_LIMIT
17+
18+
ENV STAC_FASTAPI_TITLE=${STAC_FASTAPI_TITLE}
19+
ENV STAC_FASTAPI_DESCRIPTION=${STAC_FASTAPI_DESCRIPTION}
20+
ENV STAC_FASTAPI_VERSION=${STAC_FASTAPI_VERSION}
21+
ENV APP_HOST=${APP_HOST}
22+
ENV APP_PORT=${APP_PORT}
23+
ENV RELOAD=${RELOAD}
24+
ENV ENVIRONMENT=${ENVIRONMENT}
25+
ENV WEB_CONCURRENCY=${WEB_CONCURRENCY}
26+
ENV ES_HOST=${ES_HOST}
27+
ENV ES_PORT=${ES_PORT}
28+
ENV ES_USE_SSL=${ES_USE_SSL}
29+
ENV ES_VERIFY_CERTS=${ES_VERIFY_CERTS}
30+
ENV BACKEND=${BACKEND}
31+
ENV STAC_FASTAPI_RATE_LIMIT=${STAC_FASTAPI_RATE_LIMIT}
32+
33+
RUN apt-get update && \
34+
apt-get install -y --no-install-recommends \
35+
gcc \
36+
curl \
37+
python3 \
38+
python3-pip \
39+
python3-venv \
40+
&& apt-get clean && \
41+
rm -rf /var/lib/apt/lists/*
42+
43+
# set non-root user
44+
RUN groupadd -g 1000 opensearch && \
45+
useradd -u 1000 -g opensearch -s /bin/bash -m opensearch
46+
47+
# opensearch binaries and libraries
48+
COPY --from=opensearchproject/opensearch:2.11.1 /usr/share/opensearch /usr/share/opensearch
49+
50+
# ser ownership
51+
RUN chown -R opensearch:opensearch /usr/share/opensearch
52+
53+
WORKDIR /app
54+
COPY . /app
55+
56+
# stac-fastapi-os installation
57+
RUN pip3 install --no-cache-dir --break-system-packages -e ./stac_fastapi/core && \
58+
pip3 install --no-cache-dir --break-system-packages ./stac_fastapi/opensearch[server]
59+
60+
COPY opensearch/config/opensearch.yml /usr/share/opensearch/config/opensearch.yml
61+
62+
COPY dockerfiles/entrypoint-os.sh /entrypoint.sh
63+
RUN chmod +x /entrypoint.sh
64+
65+
ENV OPENSEARCH_JAVA_OPTS="-Xms512m -Xmx1g" \
66+
PATH="/usr/share/opensearch/bin:${PATH}"
67+
68+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD \
69+
curl --silent --fail http://${ES_HOST}:${ES_PORT}/_cluster/health || exit 1 && \
70+
curl --silent --fail http://${APP_HOST}:${APP_PORT}/api.html || exit 1
71+
72+
EXPOSE $APP_PORT $ES_PORT
73+
74+
USER opensearch
75+
ENTRYPOINT ["/entrypoint.sh"]

dockerfiles/entrypoint-es.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
function validate_elasticsearch {
4+
health=$(curl -s -o /dev/null -w '%{http_code}' "http://${ES_HOST}:${ES_PORT}/_cluster/health")
5+
if [ "$health" -eq 200 ]; then
6+
return 0
7+
else
8+
return 1
9+
fi
10+
}
11+
12+
echo "start es"
13+
/usr/share/elasticsearch/bin/elasticsearch &
14+
15+
echo "wait for es to be ready"
16+
until validate_elasticsearch; do
17+
echo -n "."
18+
sleep 5
19+
done
20+
echo "Elasticsearch is up"
21+
22+
echo "start stac-fastapi-es"
23+
exec uvicorn stac_fastapi.elasticsearch.app:app --host "${APP_HOST}" --port "${APP_PORT}" --reload

dockerfiles/entrypoint-os.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
function validate_opensearch {
4+
health=$(curl -s -o /dev/null -w '%{http_code}' "http://${ES_HOST}:${ES_PORT}/_cluster/health")
5+
if [ "$health" -eq 200 ]; then
6+
return 0
7+
else
8+
return 1
9+
fi
10+
}
11+
12+
echo "start opensearch"
13+
/usr/share/opensearch/bin/opensearch &
14+
15+
echo "wait for opensearch to be ready"
16+
until validate_opensearch; do
17+
echo -n "."
18+
sleep 5
19+
done
20+
echo "opensearch is up."
21+
22+
echo "start stac-fastapi-os"
23+
exec uvicorn stac_fastapi.opensearch.app:app --host "${APP_HOST}" --port "${APP_PORT}" --reload

0 commit comments

Comments
 (0)