Automating Environments with Trunk-Based Development


Bicycle

Introduction

Trunk-Based Development (TBD) means everyone integrates to a single branch: usually main or trunk. There are almost no long-lived branches or repo forks. Instead, environments are derived dynamically through deployment pipelines and configuration.

This approach is at the heart of elite DevOps and “Continuous Deployment” teams.


Trunk-Based Development Recap

A pipeline that supports TBD should prioritize:

  • Fast feedback (unit tests, linting, builds)
  • All work goes directly into main.
  • Deployments to all environments flow from main via automated pipelines.
  • Protection rules for main (e.g. require PRs, status checks)
  • Optional feature flag deployment
  • Automation for merging short-lived branches


Pipeline Principles

  • Deploy every commit on main to an integration/dev environment.
  • Staging and Production deployments use approvals, tagging, or commit selection — but always from main.
  • Feature flags control what features are exposed where.

Example: GitHub Actions

 1# .github/workflows/deploy.yml
 2# .github/workflows/ci.yml
 3name: CI Pipeline
 4
 5on:
 6  push:
 7    branches: [main]
 8  pull_request:
 9    branches: [main]
10
11jobs:
12  lint:
13    ...
14
15  test:
16    ...
17
18  build:
19    ...
20
21  deploy-preview:
22    runs-on: ubuntu-latest
23    if: github.event_name == 'pull_request'
24    needs: build
25    steps:
26      - name: Deploy Preview
27        run: echo "Deploying PR preview environment"
28
29  deploy-production:
30    runs-on: ubuntu-latest
31    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
32    needs: build
33    steps:
34      - name: Deploy to Production
35        run: echo "Deploying to production"
  • Pull Request to main always create a preview deployment
  • Pushes to main always deploy to production.

Example: GitLab CI/CD

 1stages:
 2  - lint
 3  - test
 4  - build
 5  - deploy
 6
 7variables:
 8  NODE_ENV: test
 9
10
11lint:
12  stage: lint
13    ...
14
15test:
16  stage: test
17  needs: [lint]
18    ...
19
20build:
21  stage: build
22  needs: [test]
23    ...
24
25deploy_preview:
26  stage: deploy
27  needs: [build]
28  only:
29    - merge_requests
30  ...
31
32deploy_production:
33  stage: deploy
34  only:
35    - main
36  needs: [build]
37  ...

Best Practices

  • Invest in feature flags: They’re essential; unfinished features don’t block main, and can be selectively enabled.
  • Automate tests and rollbacks: CI must be fast and reliable for trunk-based success.
  • Keep main deployable at all times: Culture shift—code review, automated checks, frequent integration.
  • Correlate deployments/releases with tags if necessary for tracing.

Conclusion

Trunk-Based Development is ideal for high-frequency delivery and low-overhead pipeline automation. It requires robust automated testing, continuous integration, and feature flags—but unlocks rapid feedback and streamlined releases.

Ready to evolve your environments? Trunk-based may be your next step!

Go Back explore our courses

We are here for you

You are interested in our courses or you simply have a question that needs answering? You can contact us at anytime! We will do our best to answer all your questions.

Contact us