From b059fc1b07cc1956c1df72124c576064161eafef Mon Sep 17 00:00:00 2001 From: Nimish <85357445+nimish-ks@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:12:58 +0530 Subject: [PATCH] Feat: improve CI and docker builds (#380) * feat: improve CI and docker builds --- .github/workflows/backend.yml | 46 ++++++++++++++ ...r_builds.yml => docker_builds.yml.disable} | 0 .github/workflows/frontend.yml | 61 +++++++++++++++++++ .github/workflows/main.yml | 56 +++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 .github/workflows/backend.yml rename .github/workflows/{docker_builds.yml => docker_builds.yml.disable} (100%) create mode 100644 .github/workflows/frontend.yml create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml new file mode 100644 index 00000000..2d8d49b0 --- /dev/null +++ b/.github/workflows/backend.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/docker_builds.yml b/.github/workflows/docker_builds.yml.disable similarity index 100% rename from .github/workflows/docker_builds.yml rename to .github/workflows/docker_builds.yml.disable diff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.yml new file mode 100644 index 00000000..b61e0eab --- /dev/null +++ b/.github/workflows/frontend.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..4de0cd75 --- /dev/null +++ b/.github/workflows/main.yml @@ -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 \ No newline at end of file