Skip to content

Commit

Permalink
CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
dyakovri committed Feb 19, 2024
1 parent 35f068a commit 36ba0b9
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 51 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
123 changes: 72 additions & 51 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,76 @@
name: CI



on:
push:
branches: ["main"]

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

# Sets the GITHUB_TOKEN permissions to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow one concurrent deployment
concurrency:
group: 'pages'
cancel-in-progress: true
push:
tags:
- 'v*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Build
# We need to pass the repository name directly to the Vite build command
# as the "base" parameter.
# This is necessary because it aligns with how GitHub Pages functions.
# Here's how you can achieve this:
run: npm run build -- -- --base /${{ github.event.repository.name }}
- name: Create 404.html
run: cp ./app/dist/index.html ./app/dist/404.html
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload dist repository
path: './app/dist'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
build-prod-image:
name: Собираем production Docker
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

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

- name: Define production metadata
id: meta-prod
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=tag,enable=true
type=raw,value=prod,enable=true
type=raw,value=latest,enable=true
- name: Build and push production Docker
uses: docker/build-push-action@v4
with:
file: ./deployment/Dockerfile
context: .
push: true
build-args: |
BUILD_MODE=production
LAUNCH_MODE=production
APP_VERSION=${{ github.ref_name }}
tags: ${{ steps.meta-prod.outputs.tags }}
labels: ${{ steps.meta-prod.outputs.labels }}

deploy-production:
name: Раскатываем production среду
runs-on: [self-hosted, Linux]
needs: [build-prod-image]
environment:
name: Production
url: https://vu.profcomff.com/
env:
CONTAINER_NAME: com_profcomff_app
permissions:
packages: read
steps:
- name: Run docker container
run: |
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:prod
docker stop ${{ env.CONTAINER_NAME }} || true && docker rm ${{ env.CONTAINER_NAME }} || true
docker run \
--detach \
--restart always \
--network=web \
--name ${{ env.CONTAINER_NAME }} \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:prod
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
1. Склонировать себе `git clone https://github.com/profcomff/viribus-unitis-webapp`
2. Установить зависимости `npm install`
3. Запустить `npm run dev`

Очень маловероятно, что нужно будет редактировать файлы, кроме этого:

https://github.com/profcomff/viribus-unitis-webapp/blob/main/app/src/config.ts
14 changes: 14 additions & 0 deletions deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:18 AS build

WORKDIR /app
ADD . /app
RUN npm install && npm run build


FROM nginx:1.21

ADD ./deployment/nginx.conf /etc/nginx/conf.d/default.conf
ADD ./deployment/docker_entrypoint.sh /docker_entrypoint.sh
COPY --from=build /app/app/dist /usr/share/nginx/html

RUN chmod +x /docker_entrypoint.sh && /docker_entrypoint.sh
11 changes: 11 additions & 0 deletions deployment/docker_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export APPJS_NAME=$(cd /usr/share/nginx/html && ls js/app.*.js)
if [ ! -z ${APPJS_NAME} ]
then
echo 'app.js is ${APPJS_NAME}'
sed -i 's|APPJS_NAME|'${APPJS_NAME}'|g' /etc/nginx/conf.d/default.conf
sed -i 's|# ||g' /etc/nginx/conf.d/default.conf
else
echo 'app.js is app.js, no changes'
fi
31 changes: 31 additions & 0 deletions deployment/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
log_format json_combined escape=json
'{'
'"timestamp":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent"'
'}';

server {
access_log /dev/stdout json_combined;

listen 80;
listen [::]:80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

0 comments on commit 36ba0b9

Please sign in to comment.