|
| 1 | +# Lock Branch Action |
| 2 | + |
| 3 | +This GitHub Action locks or unlocks a branch by modifying the `lock_branch` setting in branch protection rules. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +The action modifies branch protection settings to: |
| 8 | +- **Freeze (lock)**: Prevent all pushes and merges to the branch |
| 9 | +- **Unfreeze (unlock)**: Allow normal operations on the branch |
| 10 | + |
| 11 | +This is useful for temporarily locking a branch during release processes. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- Branch protection must be enabled on the target branch (or will be created with minimal settings) |
| 16 | +- GitHub token with `administration:write` permissions |
| 17 | + |
| 18 | +## Inputs |
| 19 | + |
| 20 | +| Input | Description | Required | Default | |
| 21 | +|-------|-------------|----------|---------| |
| 22 | +| `branch` | The branch name to lock/unlock | Yes | - | |
| 23 | +| `freeze` | Set to `true` to lock (freeze), `false` to unlock (unfreeze) | Yes | - | |
| 24 | +| `slack-channel` | Slack channel to notify about state changes | No | - | |
| 25 | +| `github-token` | GitHub token with admin permissions | No | From Vault | |
| 26 | +| `slack-token` | Slack token for notifications | No | From Vault | |
| 27 | + |
| 28 | +## Outputs |
| 29 | + |
| 30 | +| Output | Description | |
| 31 | +|--------|-------------| |
| 32 | +| `previous-state` | The previous lock state (`true`/`false`) | |
| 33 | +| `current-state` | The current lock state (`true`/`false`) | |
| 34 | +| `branch` | The branch that was modified | |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### Lock (freeze) a branch |
| 39 | + |
| 40 | +```yaml |
| 41 | +- name: Freeze master branch |
| 42 | + uses: SonarSource/release-github-actions/lock-branch@v1 |
| 43 | + with: |
| 44 | + branch: master |
| 45 | + freeze: true |
| 46 | + slack-channel: '#releases' |
| 47 | +``` |
| 48 | +
|
| 49 | +### Unlock (unfreeze) a branch |
| 50 | +
|
| 51 | +```yaml |
| 52 | +- name: Unfreeze master branch |
| 53 | + uses: SonarSource/release-github-actions/lock-branch@v1 |
| 54 | + with: |
| 55 | + branch: master |
| 56 | + freeze: false |
| 57 | + slack-channel: '#releases' |
| 58 | +``` |
| 59 | +
|
| 60 | +### Use in automated release workflow |
| 61 | +
|
| 62 | +```yaml |
| 63 | +jobs: |
| 64 | + freeze-branch: |
| 65 | + runs-on: ubuntu-latest |
| 66 | + permissions: |
| 67 | + id-token: write |
| 68 | + steps: |
| 69 | + - uses: SonarSource/release-github-actions/lock-branch@v1 |
| 70 | + with: |
| 71 | + branch: ${{ inputs.branch }} |
| 72 | + freeze: true |
| 73 | + slack-channel: ${{ inputs.slack-channel }} |
| 74 | + |
| 75 | + # ... release steps ... |
| 76 | + |
| 77 | + unfreeze-branch: |
| 78 | + needs: [release-steps] |
| 79 | + if: always() |
| 80 | + runs-on: ubuntu-latest |
| 81 | + permissions: |
| 82 | + id-token: write |
| 83 | + steps: |
| 84 | + - uses: SonarSource/release-github-actions/lock-branch@v1 |
| 85 | + with: |
| 86 | + branch: ${{ inputs.branch }} |
| 87 | + freeze: false |
| 88 | + slack-channel: ${{ inputs.slack-channel }} |
| 89 | +``` |
| 90 | +
|
| 91 | +## Behavior |
| 92 | +
|
| 93 | +### Locking a branch |
| 94 | +- Sets `lock_branch: true` in branch protection settings |
| 95 | +- Prevents all pushes and merges to the branch |
| 96 | +- Preserves all other existing branch protection settings |
| 97 | + |
| 98 | +### Unlocking a branch |
| 99 | +- Sets `lock_branch: false` in branch protection settings |
| 100 | +- Allows normal push and merge operations |
| 101 | +- Preserves all other existing branch protection settings |
| 102 | + |
| 103 | +### Idempotent operation |
| 104 | +- If the branch is already in the requested state, no changes are made |
| 105 | +- The action will succeed and report the current state |
| 106 | + |
| 107 | +### No existing protection |
| 108 | +- If the branch has no existing protection, minimal protection is created with just the lock setting |
| 109 | +- Existing protection settings are always preserved when updating |
| 110 | + |
| 111 | +## Slack Notifications |
| 112 | + |
| 113 | +When `slack-channel` is provided, the action sends a notification with: |
| 114 | +- Lock/unlock icon indicating the action taken |
| 115 | +- Branch name and repository |
| 116 | +- Link to the workflow run |
| 117 | + |
| 118 | +## Error Handling |
| 119 | + |
| 120 | +The action will fail if: |
| 121 | +- GitHub authentication fails |
| 122 | +- Branch protection cannot be updated (e.g., insufficient permissions) |
| 123 | +- API request fails |
| 124 | + |
| 125 | +The action will succeed with a warning if: |
| 126 | +- No branch protection exists (will create minimal protection with lock) |
| 127 | +- Branch is already in the requested state |
0 commit comments