Wednesday, 14 August 2024

Setup local git branch and push to origin

How to Setup a local Git Branch, then push to origin be it github, gitlab or else and synch branchs ..

This pipeline will be generic and applicable to a typical project, which could include building, testing, and deploying a project.

Let’s assume your project is a Node.js application that you want to test and deploy using GitHub Actions. You can adapt this example to fit other languages or deployment environments.

1. Directory Structure

First, ensure your project directory is structured as follows:

 

2. GitHub Actions Workflow Configuration

Create the .github/workflows/ci-cd-pipeline.yml file with the following content:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

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

      - name: Install Dependencies
        run: npm install

      - name: Lint Code
        run: npm run lint

      - name: Run Unit Tests
        run: npm test

      - name: Build Application
        run: npm run build

      - name: Upload Build Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: build-artifacts
          path: ./dist

  deploy:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Download Build Artifacts
        uses: actions/download-artifact@v3
        with:
          name: build-artifacts

      - name: Deploy to Production
        run: |
          # Assuming you're deploying to a server or a cloud provider
          scp -r ./dist user@yourserver:/path/to/deployment
          ssh user@yourserver "cd /path/to/deployment && pm2 restart your-app"


.....


 

3. Explanation of the Pipeline

Trigger

  • The pipeline is triggered on every push to the main branch and on pull_request targeting the main branch.

Jobs

  • Build Job: This job runs on an Ubuntu runner and does the following:

    • Checkout Code: Pulls your latest code from the repository.
    • Set up Node.js: Sets up Node.js using the specified version.
    • Install Dependencies: Installs the dependencies specified in package.json.
    • Lint Code: Runs your linter to ensure code quality.
    • Run Unit Tests: Runs the unit tests to ensure your application is working as expected.
    • Build Application: Builds your application (if it’s a frontend project, this typically means creating a production build).
    • Upload Build Artifacts: Saves the build output to be used in the deployment job.
  • Deploy Job: This job runs after the build job and only if the push is to the main branch.

    • Checkout Code: Pulls your latest code.
    • Download Build Artifacts: Downloads the build output from the build job.
    • Deploy to Production: Deploys the application to your production environment. This is a placeholder for whatever deployment method you use, such as SSH to a server, deployment to a cloud provider, etc.

4. Additional Steps

  • Secret Management: Ensure you store any sensitive information like server credentials, API keys, etc., in GitHub Secrets. You can reference these in the workflow using ${{ secrets.YOUR_SECRET_NAME }}.

  • Docker Integration: If your app is containerized, you can add steps to build and push Docker images in the deploy job.

5. Running Locally

You should also be able to run your CI/CD steps locally to catch any issues before committing:

And then deploy manually or via a script if needed.

Conclusion

This pipeline provides a robust starting point for CI/CD, ensuring that your application is linted, tested, built, and deployed automatically whenever changes are made to the main branch. You can extend or modify it to fit the specific needs of your project, such as adding more jobs for integration testing, security scanning, or different deployment strategies.

 

No comments:

Post a Comment

How to check for open ports on Linux

Checking for open ports is among the first steps to secure your device. Listening services may be the entrance for attackers who may exploit...