Skip to content

Commit e33f3d8

Browse files
authored
ci: add Dockerfile and GitHub actions (#90)
* ci: add Dockerfile and GitHub actions * added Makefile and test CI * fix: ci * fix: ci * chmod +x cbsurge.sh * fix: ci * fixed CI cache path * fix: ci * change arch to lowercase * fix: mkdir -p * fix: mkdir TAR_PATH * fix: mkdir cache folder
1 parent 32ec7c0 commit e33f3d8

File tree

7 files changed

+236
-2
lines changed

7 files changed

+236
-2
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
./.devcontainer/
2+
./.github/
3+
./.venv
4+
./venv
5+
.gitignore
6+
docker-compose.yaml
7+
Dockerfile
8+
Pipfile
9+
Pipfile.lock

.github/workflows/ci.yml

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
name: Build docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
tags:
8+
- "v*"
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
name: Build & test docker image
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
- name: Create image tag
19+
id: image_tag
20+
run: |
21+
# Define cache dir
22+
ARCH=${{ runner.arch }}
23+
CACHE_PATH="/tmp/docker_cache_${ARCH,,}"
24+
# Get Dockerfile hash for image cache
25+
IMAGE_HASH="${{ hashFiles('./Dockerfile') }}"
26+
# Create image tag
27+
VARIANT="$(TZ=UTC-9 date +%Y%m%d)_${IMAGE_HASH:0:7}"
28+
IMAGE_NAME="geo_cb_surge_${ARCH,,}"
29+
TAG="${IMAGE_NAME}:${VARIANT}"
30+
# Cache dir setting
31+
TAR_NAME="${IMAGE_NAME}_${VARIANT}.tar"
32+
TAR_PATH="${CACHE_PATH}/${TAR_NAME}"
33+
echo "TAG=${TAG}" >> $GITHUB_OUTPUT
34+
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_OUTPUT
35+
echo "TAR_PATH=${TAR_PATH}" >> $GITHUB_OUTPUT
36+
echo "CACHE_PATH=${CACHE_PATH}" >> $GITHUB_OUTPUT
37+
echo "CACHE_KEY=${IMAGE_NAME}_${VARIANT}" >> $GITHUB_OUTPUT
38+
- name: Enable cache
39+
id: cache
40+
uses: actions/cache@v4
41+
with:
42+
path: ${{ steps.image_tag.outputs.CACHE_PATH }}
43+
key: ${{ steps.image_tag.outputs.CACHE_KEY }}
44+
45+
- name: Load image from cache if exists
46+
if: steps.cache.outputs.cache-hit == 'true'
47+
run: |
48+
docker load -i ${{ steps.image_tag.outputs.TAR_PATH }}
49+
50+
- name: Build image if cache does not exist
51+
if: steps.cache.outputs.cache-hit != 'true'
52+
run: |
53+
docker build -t ${{ steps.image_tag.outputs.TAG }} .
54+
CACHE_DIR=$(dirname "${{ steps.image_tag.outputs.TAR_PATH }}")
55+
mkdir -p "$CACHE_DIR"
56+
docker save ${{ steps.image_tag.outputs.TAG }} > ${{ steps.image_tag.outputs.TAR_PATH }}
57+
58+
- name: Run tests in container
59+
run: |
60+
# Change owner of workspace to ubuntu user
61+
sudo chown -R 1000:1000 ${{ github.workspace }}
62+
docker run --rm -v ${{ github.workspace }}:/app -w /app ${{ steps.image_tag.outputs.TAG }} make test
63+
64+
deploy-acr:
65+
name: Build and deploy to Azure Container Registry
66+
runs-on: ubuntu-latest
67+
environment:
68+
name: azure container registry
69+
env:
70+
IMAGE_NAME: ${{ github.repository}}
71+
permissions:
72+
contents: read
73+
packages: write
74+
steps:
75+
- name: Extract Docker metadata
76+
id: meta
77+
uses: docker/metadata-action@v4
78+
with:
79+
images: ${{ secrets.ACR_ENDPOINT }}/${{ env.IMAGE_NAME }}
80+
- name: Checkout repository
81+
uses: actions/checkout@v4
82+
- name: Log into ACR
83+
uses: docker/login-action@v3
84+
with:
85+
registry: ${{ secrets.ACR_ENDPOINT }}
86+
username: ${{ secrets.ACR_USERNAME }}
87+
password: ${{ secrets.ACR_PASSWORD }}
88+
- name: Build and push
89+
uses: docker/build-push-action@v5
90+
with:
91+
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')}}
92+
context: .
93+
file: Dockerfile
94+
tags: ${{ steps.meta.outputs.tags }}
95+
labels: ${{ steps.meta.outputs.labels }}
96+
97+
deploy-github:
98+
name: Build and deploy to GitHub container registry
99+
runs-on: ubuntu-latest
100+
environment:
101+
name: github container registry
102+
env:
103+
REGISTRY: ghcr.io
104+
IMAGE_NAME: ${{ github.repository}}
105+
permissions:
106+
contents: write
107+
packages: write
108+
steps:
109+
- name: Checkout repository
110+
uses: actions/checkout@v4
111+
112+
- name: Log in to the Container registry
113+
uses: docker/login-action@v3
114+
with:
115+
registry: ${{ env.REGISTRY }}
116+
username: ${{ github.actor }}
117+
password: ${{ secrets.GITHUB_TOKEN }}
118+
119+
- name: Extract metadata (tags, labels) for Docker
120+
id: meta
121+
uses: docker/metadata-action@v5
122+
with:
123+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
124+
125+
- name: Build and push Docker image
126+
uses: docker/build-push-action@v6
127+
with:
128+
context: .
129+
file: Dockerfile
130+
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')}}
131+
tags: ${{ steps.meta.outputs.tags }}
132+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Use the GDAL image as the base
2+
FROM ghcr.io/osgeo/gdal:ubuntu-full-3.10.0
3+
4+
# Install necessary tools and Python packages
5+
RUN apt-get update && \
6+
apt-get install -y python3-pip pipenv gcc cmake libgeos-dev && \
7+
apt-get clean && \
8+
rm -rf /var/lib/apt/lists/*
9+
10+
WORKDIR /app
11+
12+
COPY requirements.txt .
13+
RUN pipenv --python 3 && pipenv install -r requirements.txt
14+
15+
COPY . .
16+
17+
RUN chmod +x cbsurge.sh
18+
19+
CMD [ "./cbsurge.sh", "--help"]

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
.PHONY: help test build down shell
2+
3+
help:
4+
@echo "Please use \`make <target>' where <target> is one of"
5+
@echo " shell to shell in dev mode"
6+
@echo " test to execute test cases"
7+
@echo " build to build docker image"
8+
@echo " down to destroy docker containers"
9+
10+
11+
shell:
12+
@echo
13+
@echo "------------------------------------------------------------------"
14+
@echo "Shelling in dev mode"
15+
@echo "------------------------------------------------------------------"
16+
docker compose -f docker-compose.yaml run cbsurge /bin/bash
17+
18+
19+
test:
20+
@echo
21+
@echo "------------------------------------------------------------------"
22+
@echo "Execute test cases"
23+
@echo "------------------------------------------------------------------"
24+
pipenv run python -m pytest cbsurge/stats
25+
26+
build:
27+
@echo
28+
@echo "------------------------------------------------------------------"
29+
@echo "Building Docker image"
30+
@echo "------------------------------------------------------------------"
31+
docker compose -f docker-compose.yaml build
32+
33+
down:
34+
@echo
35+
@echo "------------------------------------------------------------------"
36+
@echo "Destroy docker containers"
37+
@echo "------------------------------------------------------------------"
38+
docker compose -f docker-compose.yaml down
39+
40+

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ pipenv run python -m cbsurge.cli admin ocha --help
3636
Each `cbsurge`'s modules has its own test suite which can be ran independently
3737

3838
```shell
39-
# cbsurge.stats
40-
python -m pytest cbsurge/stats
39+
make test
40+
```
41+
42+
## Using docker
43+
44+
- build docker-image
45+
46+
```shell
47+
make build
48+
```
49+
50+
- run CLI
51+
52+
```shell
53+
./cbsurge.sh --help
54+
```
55+
56+
- enter to Docker container
57+
58+
```shell
59+
make shell
60+
./cbsurge.sh --help
4161
```

cbsurge.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
pipenv run python -m cbsurge.cli $@

docker-compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: '3'
2+
services:
3+
cbsurge:
4+
build:
5+
context: .
6+
dockerfile: ./Dockerfile
7+
# default command to show help menu
8+
command: "pipenv run python -m cbsurge.cli --help"
9+
volumes:
10+
- ./Makefile:/app/Makefile
11+
- ./cbsurge:/app/cbsurge # mount app folder to container

0 commit comments

Comments
 (0)