diff --git a/.github/workflows/approve-deploy.yml b/.github/workflows/approve-deploy.yml new file mode 100644 index 0000000..fd713f2 --- /dev/null +++ b/.github/workflows/approve-deploy.yml @@ -0,0 +1,46 @@ +name: approve-deploy + +on: + pull_request_review: + types: [submitted] + workflow_dispatch: + inputs: + ref: + description: PR head SHA or branch to deploy + required: true + +permissions: + contents: write + +jobs: + approve-deploy: + runs-on: ubuntu-latest + if: | + github.event_name == 'workflow_dispatch' || + (github.event_name == 'pull_request_review' && + github.event.review.state == 'approved' && + github.event.review.author_association == 'OWNER') + steps: + - name: Deploy via build-and-deploy + uses: ./.github/workflows/build-and-deploy.yml + with: + ref: ${{ github.event.pull_request.head.sha || inputs.ref || github.sha }} + image_name: admin/reverseproxy + stack_name: torrserv_proxy + compose_path: docker-compose.yml + base_ref: main + registry: gitea.rudolf.keenetic.link + 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 }} + + - name: Comment on PR + if: github.event_name == 'pull_request_review' && github.event.review.state == 'approved' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: brencher-deploy + message: | + ✅ Deployed `torrserv_proxy` at `${{ github.event.pull_request.head.sha || inputs.ref || github.sha }}` + - triggered by: @${{ github.event.review.user.login }} diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..17139c9 --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -0,0 +1,113 @@ +name: build-and-deploy + +on: + workflow_call: + inputs: + ref: + description: Git ref to build and deploy + required: true + type: string + image_name: + description: Image name (e.g. admin/reverseproxy) + required: true + type: string + stack_name: + description: Docker stack name for `docker stack deploy` + required: true + type: string + compose_path: + description: Path to docker-compose.yml relative to the merged working dir + required: false + type: string + default: docker-compose.yml + base_ref: + description: Base ref to merge on top of (e.g. main) + required: false + type: string + default: main + registry: + description: Docker registry hostname + required: false + type: string + default: gitea.rudolf.keenetic.link + registry_username: + description: Registry username + required: true + type: string + registry_password: + description: Registry password + required: true + type: string + swarm_manager_host: + description: Swarm manager SSH host + required: true + type: string + swarm_ssh_user: + description: Swarm manager SSH user + required: true + type: string + swarm_ssh_key: + description: Swarm manager SSH private key + required: true + type: string + workflow_dispatch: + inputs: + ref: + description: Git ref to build and deploy + required: true + +permissions: + contents: read + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout base + uses: actions/checkout@v4 + with: + ref: ${{ inputs.base_ref }} + fetch-depth: 0 + + - name: Merge PR ref into working dir (no push) + run: | + if [ -n "${{ inputs.ref }}" ] && [ "${{ inputs.ref }}" != "${{ inputs.base_ref }}" ]; then + echo "Merging ${{ inputs.ref }} into ${{ inputs.base_ref }}..." + git fetch origin "${{ inputs.ref }}" + git merge --no-ff "origin/${{ inputs.ref }}" -m "auto-deploy: merge ${{ inputs.ref }} into ${{ inputs.base_ref }}" + else + echo "Using base ref directly: ${{ inputs.base_ref }}" + fi + + - name: Resolve image tag + id: tag + run: echo "value=auto-${{ inputs.ref }}" >> "$GITHUB_OUTPUT" + + - name: Login to registry + 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 }} + docker stack deploy --prune --resolve-image-always -c ${{ inputs.compose_path }} ${{ inputs.stack_name }}