Add production Dockerfile and ci image upload workflow #382
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI | |
on: | |
workflow_dispatch: | |
inputs: | |
push-docker-image-to-harbor: | |
description: 'Push Docker Image to Harbor' | |
type: boolean | |
default: false | |
pull_request: | |
push: | |
branches: | |
- main | |
- develop | |
permissions: | |
contents: read | |
jobs: | |
linting: | |
name: Linting | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Set up Python | |
uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 | |
with: | |
python-version: "3.12" | |
cache: "pip" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
python -m pip install .[code-analysis] | |
- name: Run black | |
run: black --check --line-length 120 object_storage_api test | |
- name: Run pylint | |
run: pylint object_storage_api test | |
unit-tests: | |
name: Unit Tests | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Create logging configuration file | |
run: cp object_storage_api/logging.example.ini object_storage_api/logging.ini | |
- name: Run unit tests | |
run: | | |
docker build --file Dockerfile --target test --tag object-storage-api:test . | |
docker run \ | |
--name object-storage-api \ | |
object-storage-api:test \ | |
pytest --config-file test/pytest.ini --cov object_storage_api --cov-report xml test/unit -v | |
docker cp object-storage-api:/app/coverage.xml coverage.xml | |
- name: Upload coverage reports to Codecov | |
if: success() | |
uses: codecov/codecov-action@v5 | |
with: | |
token: ${{ secrets.CODECOV_TOKEN }} | |
e2e-tests: | |
needs: [unit-tests] | |
name: End-to-End Tests | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- name: Create logging configuration file | |
run: cp object_storage_api/logging.example.ini object_storage_api/logging.ini | |
# Sleep 10 seconds to give time for containers to start | |
- name: Start MongoDB and MinIO | |
run: | | |
docker compose up --detach mongo-db minio | |
sleep 10 | |
- name: Create MinIO buckets | |
run: | | |
docker compose up minio-create-buckets | |
- name: Run e2e tests | |
run: | | |
docker build --file Dockerfile --target test --tag object-storage-api:test . | |
docker run \ | |
--name object-storage-api \ | |
--add-host localhost:host-gateway \ | |
object-storage-api:test \ | |
pytest --config-file test/pytest.ini test/e2e -v | |
- name: Output docker logs (mongodb) | |
if: failure() | |
run: docker logs object-storage-api-mongodb | |
- name: Output docker logs (minio) | |
if: failure() | |
run: docker logs object-storage-minio | |
docker: | |
# This job triggers only if all the other jobs succeed. It builds the Docker image | |
# and if run manually from Github Actions, it pushes to Harbor. | |
needs: [linting, unit-tests, e2e-tests] | |
name: Docker | |
runs-on: ubuntu-latest | |
env: | |
PUSH_DOCKER_IMAGE_TO_HARBOR: ${{ inputs.push-docker-image-to-harbor != null && inputs.push-docker-image-to-harbor || 'false' }} | |
steps: | |
- name: Check out repo | |
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1 | |
- name: Login to Harbor | |
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 | |
with: | |
registry: ${{ secrets.HARBOR_URL }} | |
username: ${{ secrets.HARBOR_USERNAME }} | |
password: ${{ secrets.HARBOR_TOKEN }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1 | |
with: | |
images: ${{ secrets.HARBOR_URL }}/object-storage-api | |
- name: ${{ fromJSON(env.PUSH_DOCKER_IMAGE_TO_HARBOR) && 'Build and push Docker image to Harbor' || 'Build Docker image' }} | |
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0 | |
with: | |
context: . | |
push: ${{ fromJSON(env.PUSH_DOCKER_IMAGE_TO_HARBOR) }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
target: prod |