|
| 1 | + |
| 2 | +# Docker CI build |
| 3 | + |
| 4 | +This repository contains reusable GitHub Actions located in `./global/docker-golden-image-workflows` that implement a docker build system with pr test image. |
| 5 | + |
| 6 | +## 🏗️ Architecture |
| 7 | + |
| 8 | +The workflows follow a strict promotion workflow: |
| 9 | + |
| 10 | +``` |
| 11 | +PR Build → MA (Main) |
| 12 | + ↓ ↓ |
| 13 | +pr-* ma-* |
| 14 | +``` |
| 15 | + |
| 16 | +## 📦 Available Composite Actions |
| 17 | + |
| 18 | +These actions are designed to be used as steps within your own jobs. |
| 19 | + |
| 20 | +1. **build-pr** - Builds Docker images for pull requests. |
| 21 | +2. **build-and-promote-ma** - Builds golden images on main merge. |
| 22 | + |
| 23 | +## 🚀 Usage in Your Repository (using Composite Actions) |
| 24 | + |
| 25 | +Composite actions are more flexible as they allow you to define your own jobs and runners. |
| 26 | + |
| 27 | +### Example: `.github/workflows/ci-cd.yml` |
| 28 | + |
| 29 | +```yaml |
| 30 | +jobs: |
| 31 | + pr-build: |
| 32 | + if: github.event_name == 'pull_request' |
| 33 | + runs-on: ubuntu-latest |
| 34 | + steps: |
| 35 | + - uses: YOUR-ORG/REPO-NAME/global/docker-golden-image-workflows/build-pr@main |
| 36 | + with: |
| 37 | + registry_password: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + |
| 39 | + main-build: |
| 40 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 41 | + runs-on: ubuntu-latest |
| 42 | + steps: |
| 43 | + - uses: YOUR-ORG/REPO-NAME/global/docker-golden-image-workflows/build-and-promote-ma@main |
| 44 | + with: |
| 45 | + registry_password: ${{ secrets.GITHUB_TOKEN }} |
| 46 | +``` |
| 47 | +
|
| 48 | +
|
| 49 | +
|
| 50 | +
|
| 51 | +### Step 2: Configure Repository Settings |
| 52 | +
|
| 53 | +1. **Enable GitHub Packages** |
| 54 | + - Go to repository Settings → Packages |
| 55 | + - Ensure package visibility is set appropriately |
| 56 | +
|
| 57 | +2. **Set Up Production Approval** (Optional) |
| 58 | + - Go to Settings → Environments |
| 59 | + - Create environment: `production-approval` |
| 60 | + - Enable "Required reviewers" |
| 61 | + - Add reviewers who can approve production deployments |
| 62 | + |
| 63 | +3. **Configure Secrets** (if using private registry) |
| 64 | + - Go to Settings → Secrets and variables → Actions |
| 65 | + - Add `REGISTRY_USERNAME` and `REGISTRY_PASSWORD` if not using GHCR |
| 66 | + |
| 67 | +## 🔧 Configuration Options |
| 68 | + |
| 69 | +### Common Inputs |
| 70 | + |
| 71 | +All actions support these inputs: |
| 72 | + |
| 73 | +| Input | Description | Default | Required | |
| 74 | +|-------|-------------|---------|----------| |
| 75 | +| `dockerfile_path` | Path to Dockerfile | `./Dockerfile` | No | |
| 76 | +| `docker_context` | Docker build context | `.` | No | |
| 77 | +| `registry` | Container registry URL | `ghcr.io` | No | |
| 78 | +| `image_name` | Image name | Repository name | No | |
| 79 | +| `build_args` | Build arguments (comma-separated) | `''` | No | |
| 80 | +| `platforms` | Target platforms | `linux/amd64` | No | |
| 81 | + |
| 82 | +### Build Arguments Example |
| 83 | + |
| 84 | +```yaml |
| 85 | +with: |
| 86 | + build_args: 'NODE_ENV=production,API_VERSION=v1,BUILD_DATE=2024-01-15' |
| 87 | +``` |
| 88 | + |
| 89 | +### Multi-Platform Builds |
| 90 | + |
| 91 | +```yaml |
| 92 | +with: |
| 93 | + platforms: 'linux/amd64,linux/arm64' |
| 94 | +``` |
| 95 | + |
| 96 | +### Custom Registry |
| 97 | + |
| 98 | +```yaml |
| 99 | +with: |
| 100 | + registry: 'docker.io' |
| 101 | + image_name: 'myorg/myapp' |
| 102 | +secrets: |
| 103 | + registry_username: ${{ secrets.DOCKER_USERNAME }} |
| 104 | + registry_password: ${{ secrets.DOCKER_PASSWORD }} |
| 105 | +``` |
| 106 | + |
| 107 | +## 🏷️ Tagging Strategy |
| 108 | + |
| 109 | +### PR Images |
| 110 | +- `pr-{number}` - PR number reference |
| 111 | +- `pr-{branch}-{sha}` - Branch and commit reference |
| 112 | + |
| 113 | +### MA (Main) Images |
| 114 | +- `v{version}` - Semantic version (e.g., v0.1.2) [Primary] |
| 115 | +- `latest` - Latest build on main |
| 116 | +- `ma-latest` - Latest MA build (alias) |
| 117 | +- `ma-{sha}` - Commit SHA reference |
| 118 | +- `ma-{timestamp}` - Timestamped version (YYYYMMDD-HHmmss) |
| 119 | + |
| 120 | +## 📊 Action Outputs |
| 121 | + |
| 122 | +### build-and-promote-ma |
| 123 | +- `image-digest` - SHA256 digest of the built image |
| 124 | +- `image-tag` - Primary tag applied to the image |
| 125 | +- `full-image` - Complete image reference with digest |
| 126 | +- `version` - Version tag (e.g., v0.1.2) used for GitHub Release |
| 127 | + |
| 128 | +## 🔐 Security Best Practices |
| 129 | + |
| 130 | +1. **Use GitHub Environments** for approval gates |
| 131 | +2. **Restrict workflow permissions** to minimum required |
| 132 | +3. **Enable branch protection** on main branch |
| 133 | +4. **Use CODEOWNERS** for workflow file changes |
| 134 | +5. **Audit promotion manifests** regularly |
| 135 | +6. **Use image digests** for production deployments |
| 136 | + |
| 137 | +## 🎯 Example: Complete Deployment Flow |
| 138 | + |
| 139 | +```bash |
| 140 | +# 1. Developer creates PR |
| 141 | +git checkout -b feature/new-feature |
| 142 | +git push origin feature/new-feature |
| 143 | +# → PR workflow builds pr-123 image |
| 144 | +
|
| 145 | +# 2. PR merged to main |
| 146 | +git checkout main |
| 147 | +git merge feature/new-feature |
| 148 | +git push origin main |
| 149 | +# → Main workflow builds v0.1.2 and latest |
| 150 | +``` |
| 151 | + |
| 152 | +## 🆘 Troubleshooting |
| 153 | + |
| 154 | +### Image not found |
| 155 | +- Verify the tag exists in GitHub Container Registry |
| 156 | +- Check workflow logs for build failures |
| 157 | +- Ensure GITHUB_TOKEN has package write permissions |
| 158 | + |
| 159 | +### Approval not working |
| 160 | +- Verify environment is configured correctly |
| 161 | +- Check required reviewers are set |
| 162 | +- Ensure reviewers have appropriate permissions |
| 163 | + |
| 164 | +## 📝 Versioning |
| 165 | + |
| 166 | +This actions repository follows semantic versioning. Use specific versions in production: |
| 167 | + |
| 168 | +```yaml |
| 169 | +uses: YOUR-ORG/REPO-NAME/global/docker-golden-image-workflows/build-and-promote-ma@v1.0.0 |
| 170 | +``` |
0 commit comments