feat(actions): add brencher reusable GitHub Action #33

Open
hermes wants to merge 1 commits from hermes/brencher:feature/reusable-brencher-action into main_corrupted
2 changed files with 131 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
# brencher action
Reusable GitHub Action that builds a Docker image, pushes it to a registry, and
deploys a Docker Compose stack to a Swarm cluster.
## Usage
```yaml
- uses: admin/brencher/.github/actions/brencher@main
with:
registry: registry.example.com
image_name: admin/my-stack
stack_name: my_stack
compose_path: docker-compose.yml
deploy_branch: main
registry_username: ${{ secrets.REGISTRY_USERNAME }}
registry_password: ${{ secrets.REGISTRY_PASSWORD }}
swarm_manager_host: ${{ secrets.SWARM_MANAGER_HOST }}
swarm_ssh_user: ${{ secrets.SWARM_SSH_USER }}
swarm_ssh_key: ${{ secrets.SWARM_SSH_KEY }}
```
## Inputs
| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `registry` | yes | – | Docker registry hostname |
| `image_name` | yes | – | Image name (e.g. `admin/reverseproxy`) |
| `stack_name` | yes | – | Docker stack name for `docker stack deploy` |
| `compose_path` | no | `docker-compose.yml` | Path to compose file relative to repo root |
| `deploy_branch` | no | `main` | Branch containing the merged-deploy commit on the swarm manager |
| `registry_username` | yes | – | Registry username |
| `registry_password` | yes | – | Registry password |
| `swarm_manager_host` | yes | – | Swarm manager SSH host |
| `swarm_ssh_user` | yes | – | Swarm manager SSH user |
| `swarm_ssh_key` | yes | – | Swarm manager SSH private key |
## How it works
1. Resolves the image tag from the current commit SHA: `auto-<sha>`.
2. Logs in to the registry.
3. Builds the image with BuildKit, pushes it to the registry with the
`auto-<sha>` and `latest` tags, using registry-based cache.
4. SSHes to the swarm manager, fetches the deploy branch, then runs
`docker stack deploy --prune --resolve-image-always` against the compose
file in the repo.
The deploy branch is fetched and reset to its tip on the swarm manager, so the
compose file the manager sees is always the latest version from the deploy
branch.
+81
View File
@@ -0,0 +1,81 @@
name: brencher
description: Build, push, and deploy a Docker Compose stack from a watched repo
inputs:
registry:
description: Docker registry hostname
required: true
image_name:
description: Image name (e.g. admin/reverseproxy)
required: true
stack_name:
description: Docker stack name for `docker stack deploy`
required: true
compose_path:
description: Path to docker-compose.yml relative to repo root
required: false
default: docker-compose.yml
deploy_branch:
description: Branch containing the merged-deploy commit
required: false
default: main
registry_username:
description: Registry username
required: true
registry_password:
description: Registry password
required: true
swarm_manager_host:
description: Swarm manager SSH host
required: true
swarm_ssh_user:
description: Swarm manager SSH user
required: true
swarm_ssh_key:
description: Swarm manager SSH private key
required: true
runs:
using: composite
steps:
- name: Set IMAGE_TAG
id: tag
shell: bash
run: |
if [ -n "${{ github.event.pull_request.head.sha }}" ]; then
echo "value=auto-${{ github.event.pull_request.head.sha }}" >> "$GITHUB_OUTPUT"
else
echo "value=auto-${{ github.sha }}" >> "$GITHUB_OUTPUT"
fi
- name: Login to registry
shell: bash
run: |
echo "${{ inputs.registry_password }}" | docker login "${{ inputs.registry }}" -u "${{ inputs.registry_username }}" --password-stdin
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
${{ inputs.registry }}/${{ inputs.image_name }}:${{ steps.tag.outputs.value }}
${{ inputs.registry }}/${{ inputs.image_name }}:latest
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.image_name }}:buildcache
cache-to: type=registry,ref=${{ inputs.registry }}/${{ inputs.image_name }}:buildcache,mode=max
- name: Deploy to swarm
uses: appleboy/drone-ssh-action@v1
with:
host: ${{ inputs.swarm_manager_host }}
username: ${{ inputs.swarm_ssh_user }}
key: ${{ inputs.swarm_ssh_key }}
script: |
set -e
export IMAGE_TAG=${{ steps.tag.outputs.value }}
cd /opt/brencher-stacks/${{ inputs.stack_name }}
git fetch origin ${{ inputs.deploy_branch }}
git reset --hard origin/${{ inputs.deploy_branch }}
docker stack deploy --prune --resolve-image-always -c ${{ inputs.compose_path }} ${{ inputs.stack_name }}