Skip to content

Commit

Permalink
Creatng CICD for Dev and Prod env
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikszewczyk committed Oct 10, 2024
1 parent a91f471 commit 852377e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: CI/CD - Development

on:
push:
branches:
- feature/**
- bugfix/**
- hotfix/**

jobs:
build:
runs-on: ubuntu-latest

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

- name: Get branch name
id: get_branch
run: |
branch_name=$(echo "${GITHUB_REF#refs/heads/}" | sed 's/\//-/g')
echo "branch_name=$branch_name" >> $GITHUB_ENV
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Install dependencies
run: npm install

- name: Build app
run: BRANCH_NAME=$branch_name npm run build

- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'

- name: List SSH directory
run: ls -l ~/.ssh

- name: Deploy to Dev Server
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa
ssh-keyscan -p ${{ secrets.HOSTING_SSH_PORT }} "${{ secrets.HOSTING_URL }}" >> ~/.ssh/known_hosts
rsync -avz --delete -e "ssh -p ${{ secrets.HOSTING_SSH_PORT }}" dist/ ${{ secrets.HOSTING_SSH_USER }}:${{ secrets.DEPLOY_PATH }}-dev/${{ env.branch_name }}
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
64 changes: 64 additions & 0 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CI/CD - Production

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

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

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '18'

- name: Install dependencies
run: npm install

- name: Build app
run: npm run build

- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'

- name: List SSH directory
run: ls -l ~/.ssh

- name: Deploy to Prod Server
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa
ssh-keyscan -p ${{ secrets.HOSTING_SSH_PORT }} "${{ secrets.HOSTING_URL }}" >> ~/.ssh/known_hosts
rsync -avz --delete -e "ssh -p ${{ secrets.HOSTING_SSH_PORT }}" dist/ ${{ secrets.HOSTING_SSH_USER }}:${{ secrets.DEPLOY_PATH }}
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

- name: Generate unique tag based on date and time
id: tag
run: |
new_tag=$(date +"%Y%m%d-%H%M")
echo "New tag: $new_tag"
echo "TAG=$new_tag" >> $GITHUB_ENV
- name: Create and push new tag
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ env.TAG }}
release_name: false
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.CICD_DEV_TOKEN }}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.github/workflows/secret.txt
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
base: process.env.NODE_ENV === 'production' ? `/${process.env.BRANCH_NAME}/` : '/'
})

0 comments on commit 852377e

Please sign in to comment.