Skip to content

Commit

Permalink
initial commit with service files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Petrucci committed Dec 12, 2023
1 parent f8f1c2d commit 739d5dc
Show file tree
Hide file tree
Showing 22 changed files with 1,107 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Log level
LOG_LEVEL=debug

# Environment
ENVIRONMENT=development

# The engine URLs
ENGINE_URLS=["http://localhost:8080"]

# The Service URL
SERVICE_URL="http://localhost:9292"

# The maximum of tasks the service can process
MAX_TASKS=50

# The number of times the service tries to announce itself to the engine
ENGINE_ANNOUNCE_RETRIES=5

# The number of seconds between each retry
ENGINE_ANNOUNCE_RETRY_DELAY=3
55 changes: 55 additions & 0 deletions .github/actions/build-and-push-docker-image-to-github/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Documentation: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
name: build_and_push_docker_image_to_github
description: Build and push Docker image to GitHub

inputs:
docker-registry-url:
description: Docker registry URL
required: true
default: ghcr.io
docker-registry-username:
description: Docker registry username
required: true
docker-registry-password:
description: Docker registry password
required: true
docker-image-name:
description: Docker image name
required: true
docker-image-context:
description: Docker image context
required: true
default: .

outputs:
docker-image-tags:
description: "Docker image tags"
value: ${{ steps.meta.outputs.tags }}

runs:
using: composite
steps:
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ inputs.docker-registry-url }}
username: ${{ inputs.docker-registry-username }}
password: ${{ inputs.docker-registry-password }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v2
with:
images: |
${{ inputs.docker-registry-url }}/${{ inputs.docker-image-name }}
tags: |
type=raw,value=latest
type=sha
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: ${{ inputs.docker-image-context }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
62 changes: 62 additions & 0 deletions .github/actions/execute-command-on-kubernetes-cluster/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Documentation: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
name: execute_command_on_kubernetes_cluster
description: Execute a command on a Kubernetes cluster

inputs:
kubectl-binary-url:
description: kubectl binary URL
required: true
default: https://dl.k8s.io/release/v1.25.3/bin/linux/amd64/kubectl
kubectl-binary-sha-url:
description: kubectl binary SHA URL
required: true
default: https://dl.k8s.io/v1.25.3/bin/linux/amd64/kubectl.sha256
kube-config:
description: kubectl config file
required: true
kube-namespace:
description: kubectl namespace
required: true
kubectl-context:
description: kubectl context
required: true
default: .
kubectl-args:
description: kubectl arguments
required: true

runs:
using: composite
steps:
- name: Get filenames from URLs
shell: bash
run: |
echo "KUBECTL_BINARY_NAME=$(basename "${{ inputs.kubectl-binary-url }}")" >> $GITHUB_ENV
echo "KUBECTL_BINARY_SHA_NAME=$(basename "${{ inputs.kubectl-binary-sha-url }}")" >> $GITHUB_ENV
- name: Download kubectl binary
shell: bash
run: curl -LO -s "${{ inputs.kubectl-binary-url }}"

- name: Download kubectl binary SHA
shell: bash
run: curl -LO -s "${{ inputs.kubectl-binary-sha-url }}"

- name: Verify kubectl binary with SHA checksum
shell: bash
run: echo "$(cat $KUBECTL_BINARY_SHA_NAME) kubectl" | sha256sum --check

- name: Install kubectl binary
shell: bash
run: sudo install -o root -g root -m 0755 $KUBECTL_BINARY_NAME /usr/local/bin/kubectl

- name: Create kubectl config file
shell: bash
run: |
mkdir --parent ~/.kube
echo "${{ inputs.kube-config }}" > ~/.kube/config
- name: Execute kubectl command
shell: bash
working-directory: ${{ inputs.kubectl-context }}
run: kubectl --namespace "${{ inputs.kube-namespace }}" ${{ inputs.kubectl-args }}
28 changes: 28 additions & 0 deletions .github/actions/lint-python-app/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Documentation: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
name: lint_python_app
description: Lint a Python app

inputs:
python-app-path:
description: Python app path
required: true
default: .

runs:
using: composite
steps:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'

- name: Install app dependencies
shell: bash
working-directory: ${{ inputs.python-app-path }}
run: pip3 install -r requirements.txt -r requirements-all.txt

- name: Lint Python code
shell: bash
working-directory: ${{ inputs.python-app-path }}
run: flake8 .
41 changes: 41 additions & 0 deletions .github/actions/test-python-app/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Documentation: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
name: test_python_app
description: Test a Python app

inputs:
python-app-path:
description: Python app path
required: true
default: .
token:
description: GitHub token
required: true
default: ""

runs:
using: composite
steps:
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'

- name: Install app dependencies
shell: bash
working-directory: ${{ inputs.python-app-path }}
run: pip3 install -r requirements.txt -r requirements-all.txt

- name: Run Python tests
shell: bash
working-directory: ${{ inputs.python-app-path }}
run: pytest

- name: Coverage report
uses: orgoro/[email protected]
if: github.event_name == 'pull_request'
with:
coverageFile: ${{ inputs.python-app-path }}/coverage.xml
token: ${{ inputs.token }}


166 changes: 166 additions & 0 deletions .github/workflows/image-resize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Documentation: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses
name: image-resize_workflow
run-name: image-resize workflow

# Allow one concurrent deployment
concurrency:
group: "image-resize"
cancel-in-progress: true

on:
push:
branches:
- main
- prod
paths:
- .github/actions/build-and-push-docker-image-to-github/action.yml
- .github/actions/execute-command-on-kubernetes-cluster/action.yml
- .github/actions/lint-python-app/action.yml
- .github/actions/test-python-app/action.yml
- .github/workflows/image-resize.yml
- common-code/**/*
- services/image-resize/**/*

pull_request:
paths:
- .github/actions/build-and-push-docker-image-to-github/action.yml
- .github/actions/execute-command-on-kubernetes-cluster/action.yml
- .github/actions/lint-python-app/action.yml
- .github/actions/test-python-app/action.yml
- .github/workflows/image-resize.yml
- common-code/**/*
- services/image-resize/**/*

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
review:
runs-on: ubuntu-latest
steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Lint Python app
uses: ./.github/actions/lint-python-app
with:
python-app-path: ./services/image-resize

test:
needs: review
runs-on: ubuntu-latest
steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Test Python app
uses: ./.github/actions/test-python-app
with:
python-app-path: ./services/image-resize
token: ${{ secrets.GITHUB_TOKEN }}

release:
needs: test
runs-on: ubuntu-latest
# Only run on main
if: success() && github.ref == 'refs/heads/main'
steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Build and push Docker image to GitHub
id: build-and-push-docker-image-to-github
uses: ./.github/actions/build-and-push-docker-image-to-github
with:
docker-registry-username: ${{ github.actor }}
docker-registry-password: ${{ secrets.GITHUB_TOKEN }}
docker-image-name: swiss-ai-center/image-resize
docker-image-context: ./services/image-resize
outputs:
docker-image-tags: ${{ steps.build-and-push-docker-image-to-github.outputs.docker-image-tags }}

deploy-dev:
needs: release
runs-on: ubuntu-latest
# Only run on main
if: success() && github.ref == 'refs/heads/main'
steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Prepare configuration files
shell: bash
working-directory: services/image-resize/kubernetes
env:
ENVIRONMENT: production
LOG_LEVEL: info
ENGINE_URLS: "'[\"https://core-engine-swiss-ai-center.kube.isc.heia-fr.ch\"]'"
SERVICE_URL: https://image-resize-swiss-ai-center.kube.isc.heia-fr.ch
run: |
# Set image-resize version
docker_image_tags=(${{ needs.release.outputs.docker-image-tags }})
docker_image_sha_tag="${docker_image_tags[1]}"
yq ".spec.template.spec.containers[0].image = \"$docker_image_sha_tag\"" image-resize.stateful.yml > new-image-resize.stateful.yml && mv new-image-resize.stateful.yml image-resize.stateful.yml
# Set image-resize configuration (ConfigMap)
yq '.data = (.data | to_entries | map({"key": .key, "value": "${" + .key + "}"}) | from_entries)' image-resize.config-map.yml | envsubst > new-image-resize.config-map.yml && mv new-image-resize.config-map.yml image-resize.config-map.yml
# Set image-resize configuration (Ingress)
yq ".spec.rules[0].host = \"${SERVICE_URL#*://}\"" image-resize.ingress.yml > new-image-resize.ingress.yml && mv new-image-resize.ingress.yml image-resize.ingress.yml
yq ".spec.tls[0].hosts[0] = \"${SERVICE_URL#*://}\"" image-resize.ingress.yml > new-image-resize.ingress.yml && mv new-image-resize.ingress.yml image-resize.ingress.yml
- name: Deploy image-resize on the Kubernetes cluster
uses: ./.github/actions/execute-command-on-kubernetes-cluster
with:
kube-config: ${{ secrets.KUBE_CONFIG_DEV }}
kube-namespace: swiss-ai-center-dev
kubectl-context: ./services/image-resize/kubernetes
kubectl-args: |
apply \
-f image-resize.config-map.yml \
-f image-resize.stateful.yml \
-f image-resize.service.yml \
-f image-resize.ingress.yml
deploy-prod:
needs: release
runs-on: ubuntu-latest
# Only run on prod
if: success() && github.ref == 'refs/heads/prod'
steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Prepare configuration files
shell: bash
working-directory: services/image-resize/kubernetes
env:
ENVIRONMENT: production
LOG_LEVEL: info
ENGINE_URLS: "'[\"https://core-engine-swiss-ai-center.kube.isc.heia-fr.ch\"]'"
SERVICE_URL: https://image-resize-swiss-ai-center.kube.isc.heia-fr.ch
run: |
# Set image-resize version
docker_image_tags=(${{ needs.release.outputs.docker-image-tags }})
docker_image_sha_tag="${docker_image_tags[1]}"
yq ".spec.template.spec.containers[0].image = \"$docker_image_sha_tag\"" image-resize.stateful.yml > new-image-resize.stateful.yml && mv new-image-resize.stateful.yml image-resize.stateful.yml
# Set image-resize configuration (ConfigMap)
yq '.data = (.data | to_entries | map({"key": .key, "value": "${" + .key + "}"}) | from_entries)' image-resize.config-map.yml | envsubst > new-image-resize.config-map.yml && mv new-image-resize.config-map.yml image-resize.config-map.yml
# Set image-resize configuration (Ingress)
yq ".spec.rules[0].host = \"${SERVICE_URL#*://}\"" image-resize.ingress.yml > new-image-resize.ingress.yml && mv new-image-resize.ingress.yml image-resize.ingress.yml
yq ".spec.tls[0].hosts[0] = \"${SERVICE_URL#*://}\"" image-resize.ingress.yml > new-image-resize.ingress.yml && mv new-image-resize.ingress.yml image-resize.ingress.yml
- name: Deploy image-resize on the Kubernetes cluster
uses: ./.github/actions/execute-command-on-kubernetes-cluster
with:
kube-config: ${{ secrets.KUBE_CONFIG_PROD }}
kube-namespace: swiss-ai-center-prod
kubectl-context: ./services/image-resize/kubernetes
kubectl-args: |
apply \
-f image-resize.config-map.yml \
-f image-resize.stateful.yml \
-f image-resize.service.yml \
-f image-resize.ingress.yml
Loading

0 comments on commit 739d5dc

Please sign in to comment.