Skip to main content
DeployHub tracks Git branches and commits to enable automatic redeployments when your code changes. Learn how to configure branch selection and monitor commit history.

Branch Configuration

Specify which branch to deploy from using the branchname field:
{
  "branchname": "main"
}
Common branch names:
  • main (default)
  • master
  • develop
  • staging
  • production

Validation

The branchname field is required for all deployments.
From the validation schema:
body('branchname')
  .notEmpty()
  .withMessage('branch name is required')

Database Storage

Branch settings are stored in the project model:
settings: {
  repoBranchName: {
    type: String,
    default: "main"
  }
}

Commit Tracking

DeployHub automatically tracks the commit SHA of each deployment for change detection.

Build Model

Commits are tracked in the Build model:
const buildSchema = new mongoose.Schema({
  project: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Project",
    required: true,
    index: true
  },
  commitSha: String,
  status: {
    type: String,
    enum: ['pending', 'success', 'failed'],
    default: 'pending'
  },
  startedAt: Date,
  finishedAt: Date,
  dockerImage: String,
  logUrl: String
}, { timestamps: true });

Fetching Commit SHA

During deployment, DeployHub fetches the latest commit SHA from GitHub:
const owner = parts[3];
const repo = parts[4].replace(/\.git$/, '');

let headers = {
  "Accept": "application/vnd.github.v3+json"
};

if (user.githubAccessToken) {
  headers.Authorization = `token ${user.githubAccessToken}`;
}

const response = await fetch(
  `https://api.github.com/repos/${owner}/${repo}/git/ref/heads/${branchname}`,
  { headers }
);

const data = await response.json();
commitSha = data?.object?.sha || null;

Storing Commit SHA

The commit SHA is stored with each build:
const newBuild = new Model.Build({
  project: newProject._id,
  commitSha: commitSha,
});

Deployment Request with Branch

1

Specify branch in deployment request

{
  "projectId": "507f1f77bcf86cd799439011",
  "name": "my-app",
  "codeLink": "https://github.com/username/repo.git",
  "projectType": "static",
  "buildCommand": "npm run build",
  "publishDir": "dist",
  "branchname": "develop",
  "isFolder": false
}
2

DeployHub fetches latest commit

The commit SHA is automatically fetched from the specified branch.
3

Build is created with commit tracking

{
  "project": "507f1f77bcf86cd799439011",
  "commitSha": "a3f2b1c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0",
  "status": "pending",
  "dockerImage": "username/projectid:buildid"
}

Git Clone with Branch

DeployHub clones the specified branch during build:
const branchname = projectData.settings.repoBranchName;
const isFolder = projectData.settings.folder.enabled;
const folderName = projectData.settings.folder?.name;

let repoUrlWithAuth;
if (usergithubAccessToken) {
  repoUrlWithAuth = `https://${usergithubAccessToken}@github.com/${owner}/${repo}.git`;
} else {
  repoUrlWithAuth = `https://github.com/${owner}/${repo}.git`;
}

if (isFolder === true) {
  execSync(
    `git clone -b ${branchname} --filter=blob:none --sparse ${repoUrlWithAuth} ${buildFilePath}`,
    { stdio: "inherit" },
  );
} else {
  execSync(`git clone -b ${branchname} ${repoUrlWithAuth} ${buildFilePath}`, {
    stdio: "inherit",
  });
}

Automatic Redeployment

DeployHub intelligently redeployes based on commit changes.

Change Detection

The redeploy worker compares commit SHAs:
const projectData = bindingData.project;
const buildData = projectData.buildId;

// Fetch latest commit SHA
let commitSha = null;
const response = await fetch(
  `https://api.github.com/repos/${owner}/${repo}/git/ref/heads/${projectData.settings.repoBranchName}`,
  { headers }
);

const data = await response.json();
commitSha = data?.object?.sha || null;

// Check if rebuild is needed
const isBuildFailed = buildData.status === "failed";
const isNewCommit = commitSha && buildData.commitSha !== commitSha;

if (isBuildFailed || isNewCommit) {
  // Rebuild from source
  const newBuild = new Model.Build({
    project: projectData._id,
    commitSha: commitSha,
    status: 'pending',
    startedAt: new Date(),
  });
  // ... build process
}

Rebuild Triggers

A rebuild is triggered when:
  1. Previous build failed: buildData.status === "failed"
  2. New commit detected: buildData.commitSha !== commitSha

Redeployment Without Rebuild

If the commit hasn’t changed and the build succeeded, DeployHub redeploys the existing Docker image:
if (isBuildFailed || isNewCommit) {
  // Rebuild from source
} else {
  // Use existing image
  const imageName = buildData.dockerImage;
  
  // Pull image if not available locally
  const images = await docker.listImages();
  const imageExists = images.some(img => 
    img.RepoTags && img.RepoTags.includes(imageName)
  );
  
  if (!imageExists) {
    await docker.pull(imageName);
  }
  
  // Deploy existing image
}

GitHub Webhook Integration

After successful deployment, DeployHub sets up a webhook for automatic redeployments:
if (usergithubAccessToken) {
  const result = await fetch(
    `https://api.github.com/repos/${owner}/${repo}/hooks`,
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${usergithubAccessToken}`,
        Accept: "application/vnd.github+json"
      },
      body: JSON.stringify({
        name: "web",
        active: true,
        events: ["push"],
        config: {
          url: "https://api.deployhub.cloud/github-webhook",
          content_type: "json",
          secret: process.env.GITHUB_WEBHOOK_SECRET
        }
      })
    }
  );
}
Webhooks are only created if the user has provided a GitHub access token.

Webhook Events

The webhook triggers on:
  • Push events: Commits pushed to the configured branch
  • Automatic redeployment: DeployHub detects the new commit and redeploys

Updating Branch Settings

Change the branch via the settings API:
curl -X PATCH https://api.deployhub.cloud/api/projects/:id/settings/general \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "repoBranchName": "staging"
  }'
Changing the branch requires a redeployment to build from the new branch.

Manual Redeployment

Trigger a manual redeployment:
curl -X POST https://api.deployhub.cloud/api/deployment/redeploy/:projectId \
  -H "Authorization: Bearer YOUR_TOKEN"
This will:
  1. Check for new commits on the configured branch
  2. Rebuild if new commits are detected
  3. Redeploy the application

Branch Strategy Examples

Production Deployment

{
  "branchname": "main",
  "name": "prod-app"
}

Staging Deployment

{
  "branchname": "staging",
  "name": "staging-app"
}

Development Deployment

{
  "branchname": "develop",
  "name": "dev-app"
}

Feature Branch Deployment

{
  "branchname": "feature/new-ui",
  "name": "feature-new-ui"
}

Multi-Environment Setup

Deploy multiple environments from different branches:
{
  "projectId": "prod-id",
  "name": "app-production",
  "branchname": "main",
  "env": {
    "NODE_ENV": "production",
    "API_URL": "https://api.example.com"
  }
}

Project Settings Response

When fetching project settings, branch info is included:
project: {
  name: "my-app",
  projectType: "static",
  settings: {
    repoBranchName: "main",
    folder: {
      enabled: false,
      name: ""
    }
  },
  buildCommand: "npm run build",
  publishDir: "dist",
  env: {}
}

Troubleshooting

Branch Not Found

Ensure the branch exists in your repository before deploying.
# List all branches
git branch -r

# Check if branch exists
git ls-remote --heads origin branchname

Commit SHA Not Fetched

If commit tracking fails:
  • Check that the repository is public or GitHub token is provided
  • Verify branch name is correct
  • Ensure GitHub API is accessible

Webhook Not Triggering

  • Verify webhook is created in GitHub repository settings
  • Check webhook secret matches
  • Ensure webhook URL is accessible
  • Review webhook delivery logs in GitHub

Rebuild Not Triggered

If changes aren’t detected:
  • Verify commits are pushed to the correct branch
  • Check that the webhook is firing
  • Manually trigger redeployment to force a check

Best Practices

Protected Branches

Use protected branches for production deployments

Branch Naming

Use consistent branch naming conventions across projects

Commit Messages

Write clear commit messages for deployment tracking

Testing

Test on staging branches before deploying to production

Next Steps