279 lines
12 KiB
YAML
279 lines
12 KiB
YAML
name: Build, Render and Deploy (lab)
|
|
|
|
# Dispatch only while the lab path is proven. Three jobs for the dispatched commit:
|
|
# build the image
|
|
# render score-lab.yaml with the platform's provisioners, committed to deploy/lab for Argo CD
|
|
# deploy score-lab.yaml to the orchestrator, which keeps the Argo CD Application and the
|
|
# public route in place
|
|
on:
|
|
workflow_dispatch: {}
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.ref }}-lab
|
|
cancel-in-progress: true
|
|
|
|
env:
|
|
AZURE_FEDERATED_TOKEN_FILE: /var/run/secrets/azure/tokens/azure-identity-token
|
|
ACR_LOGIN_SERVER: cruciblecoredevacr.azurecr.io
|
|
# Platform provisioners release. Upgrading is a change to this pin.
|
|
PROVISIONERS_VERSION: v0.1.2
|
|
SCORE_K8S_VERSION: 0.19.0
|
|
SCORE_K8S_SHA256: 7c62f3585f1864cad7c4032557e183cb65fef22a7a15f3cbe22365849ca95e2c
|
|
ORAS_VERSION: 1.2.3
|
|
ORAS_SHA256: b4efc97a91f471f323f193ea4b4d63d8ff443ca3aab514151a30751330852827
|
|
|
|
jobs:
|
|
build:
|
|
name: Build and Push
|
|
runs-on: ubuntu-latest
|
|
if: github.event_name == 'workflow_dispatch'
|
|
permissions:
|
|
contents: read
|
|
id-token: write
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Install Azure CLI
|
|
run: |
|
|
command -v az &>/dev/null || curl -sL https://aka.ms/InstallAzureCLIDeb | bash
|
|
|
|
- name: Install Docker CLI
|
|
run: |
|
|
command -v docker &>/dev/null || (apt-get update -qq && apt-get install -y docker.io)
|
|
docker --version
|
|
|
|
- name: Azure login (OIDC)
|
|
run: |
|
|
az login \
|
|
--service-principal \
|
|
--username "$AZURE_CLIENT_ID" \
|
|
--tenant "$AZURE_TENANT_ID" \
|
|
--federated-token "$(cat $AZURE_FEDERATED_TOKEN_FILE)"
|
|
echo "✓ Azure login successful"
|
|
|
|
- name: Get ACR details
|
|
run: |
|
|
# Pinned: `az acr list` returns whichever registry the identity sees first.
|
|
ACR_NAME=cruciblecoredevacr
|
|
echo "ACR_NAME=$ACR_NAME" >> $GITHUB_ENV
|
|
echo "ACR_LOGIN_SERVER=${ACR_NAME}.azurecr.io" >> $GITHUB_ENV
|
|
echo "✓ Using ACR: ${ACR_NAME}.azurecr.io"
|
|
|
|
- name: ACR Login
|
|
run: |
|
|
ACR_TOKEN=$(az acr login --name "$ACR_NAME" --expose-token --output tsv --query accessToken)
|
|
docker login "$ACR_LOGIN_SERVER" \
|
|
--username 00000000-0000-0000-0000-000000000000 \
|
|
--password "$ACR_TOKEN"
|
|
echo "✓ ACR login successful"
|
|
|
|
- name: Build and Push Docker image
|
|
run: |
|
|
IMAGE_TAG="${{ gitea.sha }}"
|
|
IMAGE_FULL="${ACR_LOGIN_SERVER}/demo-vivek-11:${IMAGE_TAG}"
|
|
IMAGE_LATEST="${ACR_LOGIN_SERVER}/demo-vivek-11:latest"
|
|
docker build -t "$IMAGE_FULL" -t "$IMAGE_LATEST" .
|
|
docker push "$IMAGE_FULL"
|
|
docker push "$IMAGE_LATEST"
|
|
echo "IMAGE_FULL=$IMAGE_FULL" >> $GITHUB_ENV
|
|
echo "✓ Pushed: $IMAGE_FULL"
|
|
|
|
- name: Build Summary
|
|
run: |
|
|
echo "### ✅ Build Successful" >> $GITHUB_STEP_SUMMARY
|
|
echo "| | |" >> $GITHUB_STEP_SUMMARY
|
|
echo "|---|---|" >> $GITHUB_STEP_SUMMARY
|
|
echo "| **Service** | demo-vivek-11 |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| **Commit** | ${{ gitea.sha }} |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| **Image** | $IMAGE_FULL |" >> $GITHUB_STEP_SUMMARY
|
|
|
|
render:
|
|
name: Render manifests
|
|
needs: build
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: write
|
|
id-token: write
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Install tools
|
|
run: |
|
|
command -v az &>/dev/null || curl -sL https://aka.ms/InstallAzureCLIDeb | bash
|
|
python3 -c 'import yaml' 2>/dev/null || (apt-get update -qq && apt-get install -y python3-yaml)
|
|
mkdir -p "$HOME/bin" && cd "$(mktemp -d)"
|
|
curl -fsSL -o score-k8s.tgz "https://github.com/score-spec/score-k8s/releases/download/${SCORE_K8S_VERSION}/score-k8s_${SCORE_K8S_VERSION}_linux_amd64.tar.gz"
|
|
curl -fsSL -o oras.tgz "https://github.com/oras-project/oras/releases/download/v${ORAS_VERSION}/oras_${ORAS_VERSION}_linux_amd64.tar.gz"
|
|
echo "${SCORE_K8S_SHA256} score-k8s.tgz" | sha256sum -c -
|
|
echo "${ORAS_SHA256} oras.tgz" | sha256sum -c -
|
|
tar -xzf score-k8s.tgz score-k8s && tar -xzf oras.tgz oras
|
|
install -m 0755 score-k8s oras "$HOME/bin/"
|
|
echo "$HOME/bin" >> $GITHUB_PATH
|
|
|
|
- name: Azure login (OIDC)
|
|
run: |
|
|
az login \
|
|
--service-principal \
|
|
--username "$AZURE_CLIENT_ID" \
|
|
--tenant "$AZURE_TENANT_ID" \
|
|
--federated-token "$(cat $AZURE_FEDERATED_TOKEN_FILE)"
|
|
|
|
- name: Pull platform provisioners
|
|
run: |
|
|
az acr login --name "${ACR_LOGIN_SERVER%%.*}" --expose-token --output tsv --query accessToken \
|
|
| oras login "$ACR_LOGIN_SERVER" --username 00000000-0000-0000-0000-000000000000 --password-stdin
|
|
oras pull "${ACR_LOGIN_SERVER}/score-provisioners:${PROVISIONERS_VERSION}" -o "$HOME/provisioners"
|
|
ls -1 "$HOME/provisioners"
|
|
|
|
- name: Render score-lab.yaml
|
|
run: |
|
|
P="$HOME/provisioners"
|
|
OUT="$PWD/deploy/lab"
|
|
WORK="$(mktemp -d)"
|
|
cp score-lab.yaml "$WORK/score.yaml"
|
|
cd "$WORK"
|
|
score-k8s init --no-sample --quiet \
|
|
$(for f in "$P"/*.provisioners.yaml; do printf -- '--provisioners %s ' "$f"; done) \
|
|
$(for f in "$P"/*.tpl.yaml; do printf -- '--patch-templates %s ' "$f"; done)
|
|
# Platform rule: only the platform's provisioners, so an unsupported resource type fails.
|
|
rm -f .score-k8s/zz-default.provisioners.yaml
|
|
CRUCIBLE_ENV=lab score-k8s generate score.yaml --quiet \
|
|
--image "${ACR_LOGIN_SERVER}/demo-vivek-11:${{ gitea.sha }}" \
|
|
--namespace demo-vivek-11 \
|
|
-o manifests.yaml
|
|
python3 "$P/policy-check.py" manifests.yaml
|
|
python3 "$P/split.py" manifests.yaml "$OUT"
|
|
|
|
- name: Commit rendered manifests
|
|
run: |
|
|
git config user.name "gitea-actions"
|
|
git config user.email "gitea-actions@noreply.gitea.kyndemo.live"
|
|
git add deploy/lab
|
|
if git diff --cached --quiet; then
|
|
echo "deploy/lab is already up to date"
|
|
exit 0
|
|
fi
|
|
SHA="${{ gitea.sha }}"
|
|
git commit -m "deploy(lab): render ${SHA:0:12} [skip ci]"
|
|
# No rebase: gradlew.bat and mvnw.cmd always show as modified in a checkout (line endings),
|
|
# and git will not rebase a dirty tree. If the branch moved during the run, run it again.
|
|
git push origin "HEAD:${{ gitea.ref_name }}" || {
|
|
echo "::error::${{ gitea.ref_name }} moved while this job ran; run the workflow again"
|
|
exit 1
|
|
}
|
|
|
|
deploy:
|
|
name: Deploy to Orchestrator
|
|
needs: render
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
PO_API_URL: https://api.dev.orchestrator.crucible.kyndemo.live
|
|
PO_ORG_ID: crucible
|
|
PO_AUTH_TOKEN: ${{ secrets.PO_AUTH_TOKEN }}
|
|
# One orchestrator project per application, bound to the lab HUB's runner: what the
|
|
# orchestrator creates is the Argo CD Application and the public route, and both live on the
|
|
# hub. Argo CD delivers the workload to the spoke from deploy/lab.
|
|
PROJECT_ID: demo-vivek-11-lab
|
|
PO_RUNNER_ID: crucible-orchestrator-dev-platform-lab-runner
|
|
OCTL_VERSION: 1.0.0
|
|
IMAGE: cruciblecoredevacr.azurecr.io/demo-vivek-11
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Install octl
|
|
run: |
|
|
set -euo pipefail
|
|
curl -fsSLo /tmp/octl.tar.gz \
|
|
"https://github.com/stellwerk-labs/platform-orchestrator-cli/releases/download/v${OCTL_VERSION}/platform-orchestrator-cli_${OCTL_VERSION}_linux_amd64.tar.gz"
|
|
tar xzf /tmp/octl.tar.gz -C /tmp
|
|
install -m 755 /tmp/octl /usr/local/bin/octl
|
|
octl --version
|
|
|
|
- name: Set environment
|
|
run: |
|
|
# One environment, of type `lab`. The type is what selects the Argo CD implementations:
|
|
# the workload becomes an Application and workload-ingress becomes the hub route.
|
|
echo "ENV_ID=lab" >> $GITHUB_ENV
|
|
|
|
- name: Ensure the project and its runner binding exist
|
|
run: |
|
|
set -euo pipefail
|
|
# Created on first deploy rather than by a pull request against config/projects.tf.
|
|
# None of this needs a repository change: project, runner rule and environment are
|
|
# all runtime objects.
|
|
#
|
|
# Neither create is idempotent, so both fall through to a read on the second run.
|
|
octl create project "$PROJECT_ID" \
|
|
--set display_name='demo-vivek-11 (lab)' || \
|
|
octl get project "$PROJECT_ID"
|
|
|
|
# The runner rule is what routes this project's deployments to the apps cluster.
|
|
# Creating it twice would leave two rules matching the same project, so it is
|
|
# created only when absent -- `create` would happily add a duplicate.
|
|
if octl get runner-rules -o json 2>/dev/null | grep -q "\"project_id\": *\"$PROJECT_ID\""; then
|
|
echo "Runner rule for '$PROJECT_ID' already exists."
|
|
else
|
|
octl create runner-rule \
|
|
--set project_id="$PROJECT_ID" \
|
|
--set runner_id="$PO_RUNNER_ID" \
|
|
--no-prompt
|
|
fi
|
|
|
|
- name: Ensure the environment exists
|
|
run: |
|
|
set -euo pipefail
|
|
octl create environment "$PROJECT_ID" "$ENV_ID" \
|
|
--set env_type_id=lab \
|
|
--set display_name="Lab" || \
|
|
octl get environment "$PROJECT_ID" "$ENV_ID"
|
|
|
|
- name: Deploy the Score workload
|
|
run: |
|
|
set -euo pipefail
|
|
# `octl score deploy` is ADDITIVE — it adds or updates a workload in the manifest and
|
|
# never removes one. That is the opposite of `octl deploy`, where omission is
|
|
# deletion. Do not substitute one for the other.
|
|
# No --show-logs: octl 1.0.0 has no such flag and exits 1 with `unknown flag`
|
|
# BEFORE contacting the orchestrator, so the whole deploy dies on an argument
|
|
# typo. Its nearest relatives are --runner-logs-level (default `info`, already
|
|
# what we want) and --skip-logs (which suppresses storage). Neither streams the
|
|
# runner's logs into this job, so there is nothing to substitute -- the runner
|
|
# logs are read from the orchestrator, not from here.
|
|
# The tag must be the commit the BUILD built, and it must be the WHOLE sha.
|
|
#
|
|
# build-push.yml tags with `` -- all 40 characters -- so the
|
|
# 7-character `${GITHUB_SHA:0:7}` this used to pass named a tag that has never
|
|
# existed in the registry.
|
|
#
|
|
# And on a workflow_run, GITHUB_SHA is the DEFAULT branch's head, while the build
|
|
# that produced the image ran on `dev`. They coincide only while the branches are
|
|
# level. `workflow_run.head_sha` is the triggering run's own commit, which is by
|
|
# definition the one that was built; `github.sha` covers the workflow_dispatch case,
|
|
# where there is no triggering run.
|
|
IMAGE_TAG="${{ gitea.sha }}"
|
|
echo "Deploying ${IMAGE}:${IMAGE_TAG}"
|
|
|
|
octl score deploy "$PROJECT_ID" "$ENV_ID" score-lab.yaml \
|
|
--default-image "${IMAGE}:${IMAGE_TAG}" \
|
|
--no-prompt
|
|
|
|
- name: Deployment summary
|
|
if: always()
|
|
run: |
|
|
# Same commit the deploy step resolved, abbreviated for reading only -- the
|
|
# deployed tag is the full sha.
|
|
DEPLOYED_SHA="${{ gitea.sha }}"
|
|
SHORT_SHA="${DEPLOYED_SHA:0:7}"
|
|
echo "## Deployment Result" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY
|
|
echo "|---|---|" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Project | \`$PROJECT_ID\` |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Environment | \`$ENV_ID\` |" >> $GITHUB_STEP_SUMMARY
|
|
echo "| Commit | \`$SHORT_SHA\` |" >> $GITHUB_STEP_SUMMARY
|
|
echo "[View in Orchestrator Console](https://console.dev.orchestrator.crucible.kyndemo.live/orgs/$PO_ORG_ID/projects/$PROJECT_ID/environments/$ENV_ID)" >> $GITHUB_STEP_SUMMARY
|