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

migrate to k8s #10

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
115 changes: 115 additions & 0 deletions .github/workflows/CI-CD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Build and Deploy

on:
workflow_dispatch:
push:
branches: ['master', 'main', 'staging']

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
ENVIRONMENT: ${{ (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && 'prod' || 'stg' }}
REGISTRY: ghcr.io/$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
KUBECTL_VERSION: v1.23.9
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
KUBE_NAMESPACE: telemetry
KUBE_DEPLOY_PREFIX: telemetry
KUBE_DEPLOY_TYPE: deployment # (statefulset or deployment)

jobs:
prepair:
runs-on: ubuntu-latest
outputs:
image_name: ${{ steps.image.outputs.image_name }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Get branch
id: branch
run: |
branch_name=${GITHUB_REF#refs/heads/}
echo "branch_name=$branch_name" >> $GITHUB_ENV

- name: Get short SHA
id: sha
run: |
sha_short=$(git rev-parse --short HEAD)
echo "sha_short=$sha_short" >> $GITHUB_ENV

- name: Set IMAGE_NAME
id: image
run: |
image_name=${{ env.REGISTRY }}-${{ env.KUBE_DEPLOY_PREFIX }}:${{ env.branch_name }}-${{ env.sha_short }}
echo "image_name=$image_name" >> $GITHUB_OUTPUT

build-and-push-image:
needs: [prepair]
runs-on: ubuntu-latest
environment: ${{ (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && 'prod' || 'stg' }}
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the github container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push image
uses: docker/build-push-action@v6
with:
file: docker/Dockerfile
context: .
push: true
tags: ${{ needs.prepair.outputs.image_name }}

deploy-to-k8s:
needs: [prepair, build-and-push-image]
runs-on: ubuntu-latest
environment: ${{ (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main') && 'prod' || 'stg' }}
steps:
- name: Setup kubectl
run: |
curl -LO "https://dl.k8s.io/release/${{ env.KUBECTL_VERSION }}/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
mkdir -p $HOME/.kube/
echo "${{ env.KUBE_CONFIG_DATA }}" | base64 -d > $HOME/.kube/config

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Get containes
id: containers
run: |
containers=$(kubectl get ${{ env.KUBE_DEPLOY_TYPE }}/${{ env.KUBE_DEPLOY_PREFIX }}-${{ env.ENVIRONMENT }} -n ${{ env.KUBE_NAMESPACE }} -o jsonpath="{.spec.template.spec.containers[*].name}")
echo "CONTAINERS=$containers" >> $GITHUB_ENV

- name: Update deployment image
run: |
for container in ${{ env.CONTAINERS }}; do
kubectl set image ${{ env.KUBE_DEPLOY_TYPE }}/${{ env.KUBE_DEPLOY_PREFIX }}-${{ env.ENVIRONMENT }} $container=${{ needs.prepair.outputs.image_name }} -n ${{ env.KUBE_NAMESPACE }};
done

- name: Restart deployment
run: |
kubectl rollout restart ${{ env.KUBE_DEPLOY_TYPE }}/${{ env.KUBE_DEPLOY_PREFIX }}-${{ env.ENVIRONMENT }} -n ${{ env.KUBE_NAMESPACE }}

- name: Check deployment
run: |
kubectl rollout status ${{ env.KUBE_DEPLOY_TYPE }}/${{ env.KUBE_DEPLOY_PREFIX }}-${{ env.ENVIRONMENT }} --timeout=300s -n ${{ env.KUBE_NAMESPACE }}
25 changes: 25 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM rust:1.82-slim as backend-builder
WORKDIR /tmp
RUN apt-get update && apt-get install -y git pkg-config libssl-dev build-essential
RUN git clone https://github.com/paritytech/substrate-telemetry.git
WORKDIR /tmp/substrate-telemetry/backend
RUN cargo build --release

FROM node:16-slim as frontend-builder
WORKDIR /tmp
COPY --from=backend-builder /tmp/substrate-telemetry /tmp/substrate-telemetry
COPY ./frontend/ /tmp/substrate-telemetry/frontend/
WORKDIR /tmp/substrate-telemetry/frontend
RUN find ./src -name "*.css" -type f -exec sed -ie 's/#e6007a/#00a87a/g' {} \;
RUN yarn install && yarn build

FROM nginx:1.27.3
WORKDIR /app
RUN rm -rf /etc/nginx/conf.d/*
COPY --from=backend-builder /tmp/substrate-telemetry/backend/target/release/telemetry_core /usr/local/bin/telemetry_core
COPY --from=backend-builder /tmp/substrate-telemetry/backend/target/release/telemetry_shard /usr/local/bin/telemetry_shard
COPY --from=frontend-builder /tmp/substrate-telemetry/frontend/build/ /app/
COPY ./docker/vhost.conf /etc/nginx/conf.d/vhost.conf
EXPOSE 80 8000 8001

CMD ["sh", "-c", "telemetry_core --max-third-party-nodes 1000 & sleep 5 && telemetry_shard & sleep 5 && nginx -g 'daemon off;' -c /etc/nginx/nginx.conf"]
25 changes: 25 additions & 0 deletions docker/vhost.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
server {
server_name _;
listen 80;
root /app/;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /feed {
proxy_pass http://127.0.0.1:8000/feed;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
}
location /submit {
proxy_pass http://127.0.0.1:8001/submit;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Real-IP $remote_addr;
}
}