178 lines
6.6 KiB
YAML
178 lines
6.6 KiB
YAML
name: Validate and deploy phpPgAdmin
|
|
|
|
on:
|
|
push:
|
|
pull_request:
|
|
|
|
env:
|
|
IMAGE: registry.brunner.ninja/feedc0de/phppgadmin
|
|
KUBECONFORM_VERSION: v0.7.0
|
|
KUBECTL_VERSION: v1.36.3
|
|
KUBERNETES_API: https://host.containers.internal:6443
|
|
KUBERNETES_TLS_SERVER_NAME: 192.168.0.2
|
|
NAMESPACE: default
|
|
|
|
jobs:
|
|
image:
|
|
name: Build, test and publish image
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 30
|
|
steps:
|
|
- name: Check out source
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Log in to Quay
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: registry.brunner.ninja
|
|
username: ${{ secrets.QUAY_USERNAME }}
|
|
password: ${{ secrets.QUAY_TOKEN }}
|
|
|
|
- name: Build image
|
|
env:
|
|
COMMIT_SHA: ${{ gitea.sha }}
|
|
run: |
|
|
set -euo pipefail
|
|
docker build \
|
|
--pull \
|
|
--load \
|
|
--label "org.opencontainers.image.base.name=registry.brunner.ninja/feedc0de/php-apache:f76304f1014f15354ec05bba7eaca376993afe72" \
|
|
--label "org.opencontainers.image.revision=${COMMIT_SHA}" \
|
|
--label "org.opencontainers.image.source=https://code.brunner.ninja/feedc0de/phppgadmin-docker" \
|
|
--tag "${IMAGE}:${COMMIT_SHA}" \
|
|
.
|
|
|
|
- name: Smoke-test image
|
|
env:
|
|
COMMIT_SHA: ${{ gitea.sha }}
|
|
run: |
|
|
set -euo pipefail
|
|
docker run --rm --entrypoint httpd "${IMAGE}:${COMMIT_SHA}" -t
|
|
docker run --rm --entrypoint php "${IMAGE}:${COMMIT_SHA}" -r '
|
|
foreach (["pgsql", "pdo_pgsql", "mbstring", "Zend OPcache"] as $extension) {
|
|
if (!extension_loaded($extension)) {
|
|
fwrite(STDERR, "Missing PHP extension: {$extension}\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
'
|
|
container_id="$(docker run --detach --publish 127.0.0.1::80 "${IMAGE}:${COMMIT_SHA}")"
|
|
trap 'docker rm --force "${container_id}" >/dev/null 2>&1 || true' EXIT
|
|
port="$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}' "${container_id}")"
|
|
for attempt in {1..30}; do
|
|
if curl --fail --silent --show-error \
|
|
--output "${RUNNER_TEMP}/phppgadmin.html" \
|
|
"http://127.0.0.1:${port}/phppgadmin/" \
|
|
&& grep -Fq 'phpPgAdmin' "${RUNNER_TEMP}/phppgadmin.html"; then
|
|
exit 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
docker logs "${container_id}"
|
|
exit 1
|
|
|
|
- name: Push image
|
|
if: gitea.ref == 'refs/heads/main'
|
|
env:
|
|
COMMIT_SHA: ${{ gitea.sha }}
|
|
run: |
|
|
set -euo pipefail
|
|
docker push "${IMAGE}:${COMMIT_SHA}"
|
|
docker tag "${IMAGE}:${COMMIT_SHA}" "${IMAGE}:latest"
|
|
docker push "${IMAGE}:latest"
|
|
|
|
validate:
|
|
name: Validate manifests
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Check out source
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Validate scripts and Kubernetes resources
|
|
run: |
|
|
sh -n install.sh
|
|
bash -n create-ci-kubeconfig.sh
|
|
bash -n test.sh
|
|
docker run --rm --interactive \
|
|
"ghcr.io/yannh/kubeconform:${KUBECONFORM_VERSION}" \
|
|
-strict -ignore-missing-schemas -summary < phppgadmin.yaml
|
|
docker run --rm --interactive \
|
|
"ghcr.io/yannh/kubeconform:${KUBECONFORM_VERSION}" \
|
|
-strict -summary < ci-deployer.yaml
|
|
|
|
deploy:
|
|
name: Deploy to Kubernetes
|
|
if: gitea.ref == 'refs/heads/main'
|
|
needs:
|
|
- image
|
|
- validate
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Check out source
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Install kubectl
|
|
run: |
|
|
curl --fail --silent --show-error --location \
|
|
--output "${RUNNER_TEMP}/kubectl" \
|
|
"https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"
|
|
chmod 0700 "${RUNNER_TEMP}/kubectl"
|
|
|
|
- name: Configure Kubernetes access
|
|
env:
|
|
KUBE_CONFIG_BASE64: ${{ secrets.KUBE_CONFIG_BASE64 }}
|
|
run: |
|
|
test -n "${KUBE_CONFIG_BASE64}"
|
|
printf '%s' "${KUBE_CONFIG_BASE64}" | base64 --decode > "${RUNNER_TEMP}/kubeconfig"
|
|
chmod 0600 "${RUNNER_TEMP}/kubeconfig"
|
|
export KUBECONFIG="${RUNNER_TEMP}/kubeconfig"
|
|
"${RUNNER_TEMP}/kubectl" config set-cluster cluster \
|
|
--server="${KUBERNETES_API}" \
|
|
--tls-server-name="${KUBERNETES_TLS_SERVER_NAME}"
|
|
|
|
- name: Apply and verify
|
|
env:
|
|
COMMIT_SHA: ${{ gitea.sha }}
|
|
run: |
|
|
export KUBECONFIG="${RUNNER_TEMP}/kubeconfig"
|
|
sed \
|
|
"s|deployment.brunner.ninja/revision: manual|deployment.brunner.ninja/revision: ${COMMIT_SHA}|" \
|
|
phppgadmin.yaml \
|
|
| sed \
|
|
"s|registry.brunner.ninja/feedc0de/phppgadmin:latest|${IMAGE}:${COMMIT_SHA}|" \
|
|
> "${RUNNER_TEMP}/phppgadmin.yaml"
|
|
grep -Fq "deployment.brunner.ninja/revision: ${COMMIT_SHA}" \
|
|
"${RUNNER_TEMP}/phppgadmin.yaml"
|
|
grep -Fq "image: ${IMAGE}:${COMMIT_SHA}" \
|
|
"${RUNNER_TEMP}/phppgadmin.yaml"
|
|
"${RUNNER_TEMP}/kubectl" apply \
|
|
--dry-run=server \
|
|
--validate=false \
|
|
--filename "${RUNNER_TEMP}/phppgadmin.yaml"
|
|
"${RUNNER_TEMP}/kubectl" apply \
|
|
--validate=false \
|
|
--filename "${RUNNER_TEMP}/phppgadmin.yaml"
|
|
|
|
for attempt in {1..120}; do
|
|
IFS='|' read -r generation observed desired updated ready available unavailable image <<< "$(
|
|
"${RUNNER_TEMP}/kubectl" --namespace "${NAMESPACE}" \
|
|
get deployment/phppgadmin \
|
|
--output=jsonpath='{.metadata.generation}|{.status.observedGeneration}|{.spec.replicas}|{.status.updatedReplicas}|{.status.readyReplicas}|{.status.availableReplicas}|{.status.unavailableReplicas}|{.spec.template.spec.containers[0].image}'
|
|
)"
|
|
|
|
echo "Rollout ${attempt}/120: generation ${observed}/${generation}, replicas ${updated}/${desired} updated, ${ready}/${desired} ready, ${available}/${desired} available, image ${image}"
|
|
if [[ "${observed}" == "${generation}" \
|
|
&& "${updated}" == "${desired}" \
|
|
&& "${ready}" == "${desired}" \
|
|
&& "${available}" == "${desired}" \
|
|
&& ( -z "${unavailable}" || "${unavailable}" == "0" ) ]]; then
|
|
echo "phpPgAdmin deployment successfully rolled out"
|
|
exit 0
|
|
fi
|
|
|
|
sleep 5
|
|
done
|
|
|
|
echo "phpPgAdmin rollout did not complete within ten minutes" >&2
|
|
exit 1
|