Files
ci-templates/.gitea/workflows/container-cd.yml
T
TJWater CI 33c2618671
Runner Canary / runner-canary (push) Successful in 1s
fix(ci): use Gitea commit context for image tags
2026-08-07 17:27:41 +08:00

86 lines
3.0 KiB
YAML

name: Generic Container CI/CD
on:
workflow_call:
inputs:
image_name:
description: Fully qualified Gitea Registry image repository without a tag
required: true
type: string
dockerfile:
required: false
default: Dockerfile
type: string
build_context:
required: false
default: .
type: string
test_command:
description: Optional source-level test command
required: false
default: ""
type: string
deploy_service:
description: Dev service to candidate-test and promote
required: false
default: ""
type: string
deploy_host:
description: Restricted Dev deployment host
required: false
default: ""
type: string
secrets:
REGISTRY_USERNAME:
required: true
REGISTRY_PASSWORD:
required: true
DEV_DEPLOY_SSH_KEY:
required: false
jobs:
test-build-publish:
runs-on: ubuntu-22.04
steps:
- uses: https://gitea.waternetwork.cn/actions/checkout@v4
with:
fetch-depth: 1
- name: Source tests
if: ${{ inputs.test_command != '' }}
run: ${{ inputs.test_command }}
- name: Build immutable candidate image
run: |
set -eu
docker build --pull=false -f "${{ inputs.dockerfile }}" \
-t "${{ inputs.image_name }}:sha-${{ gitea.sha }}" "${{ inputs.build_context }}"
- name: Login and publish candidate image
env:
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
set -eu
registry="$(printf '%s' '${{ inputs.image_name }}' | cut -d/ -f1)"
printf '%s' "$REGISTRY_PASSWORD" | docker login "$registry" -u "$REGISTRY_USERNAME" --password-stdin
docker push "${{ inputs.image_name }}:sha-${{ gitea.sha }}"
- name: Candidate pull on Dev
if: ${{ inputs.deploy_service != '' && inputs.deploy_host != '' }}
env:
DEV_DEPLOY_SSH_KEY: ${{ secrets.DEV_DEPLOY_SSH_KEY }}
run: |
set -eu
test -n "$DEV_DEPLOY_SSH_KEY"
install -d -m 700 ~/.ssh
printf '%s\n' "$DEV_DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "${{ inputs.deploy_host }}" >> ~/.ssh/known_hosts
ssh -i ~/.ssh/id_ed25519 cicd-deploy@"${{ inputs.deploy_host }}" \
"candidate ${{ inputs.deploy_service }} ${{ inputs.image_name }}:sha-${{ gitea.sha }}"
- name: Promote tagged release after candidate check
if: ${{ startsWith(gitea.ref, 'refs/tags/v') && inputs.deploy_service != '' && inputs.deploy_host != '' }}
env:
DEV_DEPLOY_SSH_KEY: ${{ secrets.DEV_DEPLOY_SSH_KEY }}
run: |
set -eu
ssh -i ~/.ssh/id_ed25519 cicd-deploy@"${{ inputs.deploy_host }}" \
"promote ${{ inputs.deploy_service }} ${{ inputs.image_name }}:sha-${{ gitea.sha }}"