Skip to content

Commit

Permalink
Feat: improve CI and docker builds (#380)
Browse files Browse the repository at this point in the history
* feat: improve CI and docker builds
  • Loading branch information
nimish-ks authored Oct 17, 2024
1 parent 8ca9b19 commit b059fc1
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Backend CI/CD

on:
workflow_call:
inputs:
version:
required: true
type: string
is_pr:
required: true
type: string

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
context: ./backend
push: true
platforms: ${{ inputs.is_pr == 'true' && 'linux/amd64' || 'linux/amd64,linux/arm64' }}
tags: |
${{ inputs.is_pr == 'true' && 'phasehq/backend-staging' || 'phasehq/backend' }}:${{ inputs.version }}
ghcr.io/${{ github.repository }}/${{ inputs.is_pr == 'true' && 'backend-staging' || 'backend' }}:${{ inputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
File renamed without changes.
61 changes: 61 additions & 0 deletions .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Frontend CI/CD

on:
workflow_call:
inputs:
version:
required: true
type: string
is_pr:
required: true
type: string

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

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

- name: Install dependencies
run: |
cd frontend
yarn install
- name: Run tests
run: |
cd frontend
yarn test
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
context: ./frontend
push: true
platforms: ${{ inputs.is_pr == 'true' && 'linux/amd64' || 'linux/amd64,linux/arm64' }}
tags: |
${{ inputs.is_pr == 'true' && 'phasehq/frontend-staging' || 'phasehq/frontend' }}:${{ inputs.version }}
ghcr.io/${{ github.repository }}/${{ inputs.is_pr == 'true' && 'frontend-staging' || 'frontend' }}:${{ inputs.version }}
cache-from: type=gha
cache-to: type=gha,mode=max
56 changes: 56 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: CI/CD Pipeline

on:
push:
branches:
- main
pull_request:
branches: [main]
release:
types: [published]

permissions:
contents: write
packages: write

jobs:
determine_version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.set_version.outputs.version }}
is_pr: ${{ steps.set_version.outputs.is_pr }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set version and PR flag
id: set_version
run: |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo "version=$(git rev-parse --short ${{ github.event.pull_request.head.sha }} | cut -c 1-7)" >> $GITHUB_OUTPUT
echo "is_pr=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" ]]; then
echo "version=latest" >> $GITHUB_OUTPUT
echo "is_pr=false" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "release" ]]; then
echo "version=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
echo "is_pr=false" >> $GITHUB_OUTPUT
fi
frontend:
needs: determine_version
uses: ./.github/workflows/frontend.yml
with:
version: ${{ needs.determine_version.outputs.version }}
is_pr: ${{ needs.determine_version.outputs.is_pr }}
secrets: inherit

backend:
needs: determine_version
uses: ./.github/workflows/backend.yml
with:
version: ${{ needs.determine_version.outputs.version }}
is_pr: ${{ needs.determine_version.outputs.is_pr }}
secrets: inherit

0 comments on commit b059fc1

Please sign in to comment.