ci
This commit is contained in:
parent
9d64499c2a
commit
f5061c501e
6 changed files with 221 additions and 1 deletions
155
.forgejo/workflows/deploy.yml
Normal file
155
.forgejo/workflows/deploy.yml
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
name: Reusable Deploy
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
inputs:
|
||||||
|
env:
|
||||||
|
required: true
|
||||||
|
type: string # dev | prod
|
||||||
|
secrets:
|
||||||
|
MANIFESTS_TOKEN:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: docker
|
||||||
|
steps:
|
||||||
|
- name: Checkout app repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Checkout ci-workflows
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
repository: sys/ci-workflows
|
||||||
|
ref: master
|
||||||
|
path: .ci-workflows
|
||||||
|
token: ${{ secrets.MANIFESTS_TOKEN }}
|
||||||
|
|
||||||
|
- name: Check authorization and read config
|
||||||
|
id: config
|
||||||
|
env:
|
||||||
|
REPO: ${{ github.repository }}
|
||||||
|
ENV: ${{ inputs.env }}
|
||||||
|
SHA: ${{ github.sha }}
|
||||||
|
run: |
|
||||||
|
python3 << 'PYEOF'
|
||||||
|
import yaml, os, sys
|
||||||
|
|
||||||
|
repo = os.environ['REPO']
|
||||||
|
env = os.environ['ENV']
|
||||||
|
sha = os.environ['SHA'][:7]
|
||||||
|
|
||||||
|
with open('.ci-workflows/allowed-repos.yaml') as f:
|
||||||
|
allowed = yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
namespace = next(
|
||||||
|
(ns for ns, repos in allowed.items() if repo in (repos or [])),
|
||||||
|
None
|
||||||
|
)
|
||||||
|
if not namespace:
|
||||||
|
print(f'::error::Repository {repo} is not in allowed-repos.yaml', flush=True)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
with open('deploy.yaml') as f:
|
||||||
|
cfg = yaml.safe_load(f)
|
||||||
|
|
||||||
|
app_name = cfg['app']['name']
|
||||||
|
port = str(cfg['app']['port'])
|
||||||
|
ingress_host = cfg['ingress'][env]
|
||||||
|
replicas = str(cfg['replicas'][env])
|
||||||
|
cpu_req = cfg['resources']['requests']['cpu']
|
||||||
|
mem_req = cfg['resources']['requests']['memory']
|
||||||
|
cpu_lim = cfg['resources']['limits']['cpu']
|
||||||
|
mem_lim = cfg['resources']['limits']['memory']
|
||||||
|
|
||||||
|
sha_prefix = 'dev-sha-' if env == 'dev' else 'sha-'
|
||||||
|
image = f"git.vodyanoysh.ru/{repo}:{sha_prefix}{sha}"
|
||||||
|
resource_name = f"{env}-{app_name}"
|
||||||
|
|
||||||
|
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
|
||||||
|
for k, v in {
|
||||||
|
'namespace': namespace,
|
||||||
|
'app_name': app_name,
|
||||||
|
'resource_name': resource_name,
|
||||||
|
'port': port,
|
||||||
|
'ingress_host': ingress_host,
|
||||||
|
'replicas': replicas,
|
||||||
|
'cpu_request': cpu_req,
|
||||||
|
'memory_request': mem_req,
|
||||||
|
'cpu_limit': cpu_lim,
|
||||||
|
'memory_limit': mem_lim,
|
||||||
|
'image': image,
|
||||||
|
}.items():
|
||||||
|
f.write(f'{k}={v}\n')
|
||||||
|
PYEOF
|
||||||
|
|
||||||
|
- name: Render manifests
|
||||||
|
env:
|
||||||
|
APP_NAME: ${{ steps.config.outputs.app_name }}
|
||||||
|
RESOURCE_NAME: ${{ steps.config.outputs.resource_name }}
|
||||||
|
PORT: ${{ steps.config.outputs.port }}
|
||||||
|
INGRESS_HOST: ${{ steps.config.outputs.ingress_host }}
|
||||||
|
REPLICAS: ${{ steps.config.outputs.replicas }}
|
||||||
|
CPU_REQUEST: ${{ steps.config.outputs.cpu_request }}
|
||||||
|
MEMORY_REQUEST: ${{ steps.config.outputs.memory_request }}
|
||||||
|
CPU_LIMIT: ${{ steps.config.outputs.cpu_limit }}
|
||||||
|
MEMORY_LIMIT: ${{ steps.config.outputs.memory_limit }}
|
||||||
|
IMAGE: ${{ steps.config.outputs.image }}
|
||||||
|
run: |
|
||||||
|
VARS='${APP_NAME} ${RESOURCE_NAME} ${PORT} ${INGRESS_HOST} ${REPLICAS} ${CPU_REQUEST} ${MEMORY_REQUEST} ${CPU_LIMIT} ${MEMORY_LIMIT} ${IMAGE}'
|
||||||
|
mkdir -p /tmp/rendered
|
||||||
|
> /tmp/rendered/all.yaml
|
||||||
|
for tpl in \
|
||||||
|
.ci-workflows/templates/deployment.yaml \
|
||||||
|
.ci-workflows/templates/service.yaml \
|
||||||
|
.ci-workflows/templates/ingress.yaml; do
|
||||||
|
envsubst "$VARS" < "$tpl" >> /tmp/rendered/all.yaml
|
||||||
|
printf '\n---\n' >> /tmp/rendered/all.yaml
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Commit to manifests repo
|
||||||
|
env:
|
||||||
|
TOKEN: ${{ secrets.MANIFESTS_TOKEN }}
|
||||||
|
NAMESPACE: ${{ steps.config.outputs.namespace }}
|
||||||
|
RESOURCE_NAME: ${{ steps.config.outputs.resource_name }}
|
||||||
|
run: |
|
||||||
|
git clone https://x-token:${TOKEN}@git.vodyanoysh.ru/vodyanoy/manifests.git /tmp/manifests
|
||||||
|
|
||||||
|
python3 << 'PYEOF'
|
||||||
|
import yaml, os, shutil
|
||||||
|
|
||||||
|
namespace = os.environ['NAMESPACE']
|
||||||
|
resource_name = os.environ['RESOURCE_NAME']
|
||||||
|
base = f"/tmp/manifests/cluster/namespaces/{namespace}"
|
||||||
|
dest = f"{base}/{resource_name}"
|
||||||
|
|
||||||
|
os.makedirs(dest, exist_ok=True)
|
||||||
|
shutil.copy('/tmp/rendered/all.yaml', f'{dest}/all.yaml')
|
||||||
|
|
||||||
|
with open(f'{dest}/kustomization.yaml', 'w') as f:
|
||||||
|
yaml.dump(
|
||||||
|
{'apiVersion': 'kustomize.config.k8s.io/v1beta1',
|
||||||
|
'kind': 'Kustomization',
|
||||||
|
'resources': ['all.yaml']},
|
||||||
|
f, default_flow_style=False, sort_keys=False
|
||||||
|
)
|
||||||
|
|
||||||
|
ns_kust_path = f'{base}/kustomization.yaml'
|
||||||
|
with open(ns_kust_path) as f:
|
||||||
|
ns_kust = yaml.safe_load(f)
|
||||||
|
|
||||||
|
entry = f'{resource_name}/'
|
||||||
|
resources = ns_kust.get('resources') or []
|
||||||
|
if entry not in resources:
|
||||||
|
resources.append(entry)
|
||||||
|
ns_kust['resources'] = resources
|
||||||
|
with open(ns_kust_path, 'w') as f:
|
||||||
|
yaml.dump(ns_kust, f, default_flow_style=False, sort_keys=False)
|
||||||
|
PYEOF
|
||||||
|
|
||||||
|
cd /tmp/manifests
|
||||||
|
git config user.email "ci@vodyanoysh.ru"
|
||||||
|
git config user.name "Forgejo CI"
|
||||||
|
git add .
|
||||||
|
git diff --cached --quiet || git commit -m "deploy ${RESOURCE_NAME} → ${NAMESPACE}"
|
||||||
|
git push
|
||||||
|
|
@ -14,6 +14,10 @@ on:
|
||||||
required: false
|
required: false
|
||||||
type: string
|
type: string
|
||||||
default: "./Dockerfile"
|
default: "./Dockerfile"
|
||||||
|
sha_prefix:
|
||||||
|
required: false
|
||||||
|
type: string
|
||||||
|
default: "sha-"
|
||||||
secrets:
|
secrets:
|
||||||
REGISTRY_AUTH:
|
REGISTRY_AUTH:
|
||||||
required: true
|
required: true
|
||||||
|
|
@ -49,7 +53,7 @@ jobs:
|
||||||
type=ref,event=branch
|
type=ref,event=branch
|
||||||
type=ref,event=pr
|
type=ref,event=pr
|
||||||
type=semver,pattern={{version}}
|
type=semver,pattern={{version}}
|
||||||
type=sha,prefix=sha-
|
type=sha,prefix=${{ inputs.sha_prefix }}
|
||||||
|
|
||||||
- name: Build and push
|
- name: Build and push
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
|
|
|
||||||
2
allowed-repos.yaml
Normal file
2
allowed-repos.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
apps:
|
||||||
|
- vodyanoy/test-ci
|
||||||
28
templates/deployment.yaml
Normal file
28
templates/deployment.yaml
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: ${RESOURCE_NAME}
|
||||||
|
spec:
|
||||||
|
replicas: ${REPLICAS}
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: ${RESOURCE_NAME}
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: ${RESOURCE_NAME}
|
||||||
|
spec:
|
||||||
|
imagePullSecrets:
|
||||||
|
- name: forgejo-registry
|
||||||
|
containers:
|
||||||
|
- name: ${APP_NAME}
|
||||||
|
image: ${IMAGE}
|
||||||
|
ports:
|
||||||
|
- containerPort: ${PORT}
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: ${CPU_REQUEST}
|
||||||
|
memory: ${MEMORY_REQUEST}
|
||||||
|
limits:
|
||||||
|
cpu: ${CPU_LIMIT}
|
||||||
|
memory: ${MEMORY_LIMIT}
|
||||||
21
templates/ingress.yaml
Normal file
21
templates/ingress.yaml
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
apiVersion: networking.k8s.io/v1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
name: ${RESOURCE_NAME}
|
||||||
|
annotations:
|
||||||
|
traefik.ingress.kubernetes.io/router.entrypoints: websecure
|
||||||
|
traefik.ingress.kubernetes.io/router.tls: "true"
|
||||||
|
traefik.ingress.kubernetes.io/router.tls.certresolver: letsencrypt
|
||||||
|
spec:
|
||||||
|
ingressClassName: traefik
|
||||||
|
rules:
|
||||||
|
- host: ${INGRESS_HOST}
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- path: /
|
||||||
|
pathType: Prefix
|
||||||
|
backend:
|
||||||
|
service:
|
||||||
|
name: ${RESOURCE_NAME}
|
||||||
|
port:
|
||||||
|
number: 80
|
||||||
10
templates/service.yaml
Normal file
10
templates/service.yaml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: ${RESOURCE_NAME}
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: ${RESOURCE_NAME}
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
targetPort: ${PORT}
|
||||||
Loading…
Reference in a new issue