Skip to content

bebug envs

bebug envs #16

name: Build and Deploy digit-recognition/frontend
on:
workflow_dispatch:
push:
branches: ["*"]
paths:
- "digit-recognition/frontend/**"
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: ${{ secrets.KUBECTL_VERSION }}
KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
KUBE_NAMESPACE: gear-exe-dapps
KUBE_DEPLOY_PREFIX: digit-recognition-frontend
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:
context: digit-recognition/frontend/
push: true
build-args: |
NODE_ENV=${{ env.NODE_ENV }}
VITE_WALLET_CONNECT_PROJECT_ID=${{ secrets.VITE_WALLET_CONNECT_PROJECT_ID }}
VITE_GEAR_API_NODE=${{ secrets.VITE_GEAR_API_NODE }}
VITE_ETH_NODE_ADDRESS=${{ secrets.VITE_ETH_NODE_ADDRESS }}
VITE_CONTRACT_ADDRESS_DIGIT_RECOGNITION=${{ secrets.VITE_CONTRACT_ADDRESS_DIGIT_RECOGNITION }}
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 }}