Git Product home page Git Product logo

topics's Introduction

Topics Project

This project will demo the usage of Github Actions with a monorepo. The system consists on two applications, web and platform(./plaform) the front-end and back-end respectively.

Common

At the top level of the project we will find the following files:

  • README.md - This
  • .gitignore - Telling git which files to ignore
  • vercel.json - Vercel config file
  • .github - Folder with Github Actions

Web

Web is a React based application. Currently it only contains the starting project created by create-react-app. For instructions on usage refer to create-react-app docs.

Platform

Platform is a Python based back-end powered by FastAPI. It contains a few example APIs and it requires the usage of a PostgreSQL database.

Infra

Infrastructure. Code is hosted in Github, on commit it runs separete workflow for web and platform. After the build is done, it will deploy to Vercel for web, and to Heroku for platform.

CI/CD

The .github folder

The .github folder contains our Github Actions Workflows. Each workflow is defined is it's own .yml file.

Job Overview

Let's take a into the workflow definition for web. Following we will define the name of the workflow, when it should run, and on which branch and folder.

name: Build and Deploy Web App

on:
  # when it should run 
  push: 
    # on which branches
    branches:
      - main  
    # which folder trigger changes
    paths:
      - "web/**"

As we are running a monorepo changes will be made to several projects, but we only want to deploy changes when the specific project changes, so in order to get that behaviour we set the paths variable. The paths variable will make the Workflow only run when changes are made to that specific path.

Now let's define the build job. This will require us to define which OS we need to use, our build steps, if any services are required, among others.

# set some default values for all the jobs
defaults:
  run:
    working-directory: ./web

# jobs definition
jobs:

  # name of the job
  build:
  
    # selected OS
    runs-on: ubuntu-latest

    # interpreter versions, will run once for each version 
    # mostly useful for libraries
    strategy:
      matrix:
        node-version: [16.x]

    # our job steps definition
    steps:
      # checkout code with predifined action
      - uses: actions/checkout@v2

      # add interpreter to enviorment with predefined action
      # uses version in matrix
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      # Project build steps
      - name: Install dependencies
        run: npm install

      - name: Check code format - Prettier
        run: npx prettier --check src

      - name: Run tests
        run: npm test

      - name: Execute build
        run: npm run build

If everything with the job is okay, we can move forward with the deployment. For the web project, we will be using Vercel. We will only deploy on build success so we will say that this job named deploy will need the job build. This means that deploy will run after build is finished and is successful.

In order to deploy to Vercel we will use Vercel provided deploy action. In order to do this we will need to set a few secrets to the actions. We can pass secrets by using the ${{ secret_name }} notation. Secrets are defined in the settings tab for the project in Github.

# define job name
deploy:

  # add dependencies
  needs: build

  # select os
  runs-on: ubuntu-latest

  # define job steps
  steps:
    - uses: actions/checkout@v2
    - uses: amondnet/vercel-action@v20
      with:
        # pass secrets
        vercel-token: ${{ secrets.VERCEL_TOKEN }}
        vercel-org-id: ${{ secrets.ORG_ID}}
        vercel-project-id: ${{ secrets.PROJECT_ID}}
        working-directory: ./web

Optionally if needed we can pass services to a job definition, like we do in platform workflow. Here we will use PostgreSQL while running the project build. We are setting username as password so the service is created with that credentials. Also do some port forwarding the PostgreSQL will be listening in port 5432, which will connect to our CI/CD container in the port 5432. This means we can expect the DB to be available at port 5432.

services:
  postgres:
    image: postgres
      env:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: cantguessthis
      ports:
        - 5432:5432

Services are basically just Docker containers. So you should be able to use any container you want.

topics's People

Contributors

npgiorgi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.