[CI] Deploy preview environments #30
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "[UI] Build and push Docker image" | |
on: | |
push: | |
paths: | |
- "opendata.swiss/ui/**" # Only trigger if files in the ui directory change | |
- ".github/workflows/docker-ods-ui.yaml" # Trigger also if this workflow file is changed | |
pull_request: | |
paths: | |
- "opendata.swiss/ui/**" # Only trigger if files in the ui directory change | |
- ".github/workflows/docker-ods-ui.yaml" # Trigger also if this workflow file is changed | |
jobs: | |
docker: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
id-token: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v5 | |
- name: Set up kubectl | |
if: github.event_name == 'pull_request' | |
uses: azure/setup-kubectl@v4 | |
with: | |
version: v1.33.0 | |
- name: Set up Kustomize | |
if: github.event_name == 'pull_request' | |
uses: imranismail/setup-kustomize@v2 | |
- name: Install Cosign | |
if: github.event_name != 'pull_request' | |
uses: sigstore/cosign-installer@v3.9.2 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Configure some variables | |
id: vars | |
run: | | |
echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}" >> $GITHUB_OUTPUT | |
- name: Docker meta | |
id: docker_meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: | | |
ghcr.io/opendata-swiss/ods-ui | |
tags: | | |
type=ref,event=branch | |
type=sha | |
type=raw,value={{branch}}-${{ github.run_id }} | |
type=raw,value=latest,enable={{is_default_branch}} | |
- name: Build and push Docker images | |
id: docker_build | |
if: github.event_name != 'pull_request' | |
uses: docker/build-push-action@v6 | |
with: | |
context: ./opendata.swiss/ui | |
file: ./opendata.swiss/ui/Dockerfile | |
push: true | |
tags: ${{ steps.docker_meta.outputs.tags }} | |
labels: ${{ steps.docker_meta.outputs.labels }} | |
platforms: | | |
linux/amd64 | |
- name: Sign the images with GitHub OIDC Token | |
if: github.event_name != 'pull_request' | |
env: | |
DIGEST: ${{ steps.docker_build.outputs.digest }} | |
TAGS: ${{ steps.docker_meta.outputs.tags }} | |
run: | | |
images="" | |
for tag in ${TAGS}; do | |
images+="${tag}@${DIGEST} " | |
done | |
cosign sign --yes ${images} | |
- name: Configure cluster context | |
if: github.event_name == 'pull_request' | |
run: | | |
kubectl config set-cluster default "--server=${K8S_API_URL}" --insecure-skip-tls-verify=true | |
kubectl config set-credentials default "--token=${K8S_TOKEN}" | |
kubectl config set-context default --cluster=default --namespace=piveau-previews --user=default | |
kubectl config use-context default | |
- name: Generate manifests | |
if: github.event_name == 'pull_request' | |
working-directory: ./opendata.swiss/ui/k8s | |
run: | | |
kustomize edit set nameprefix "${{ steps.vars.outputs.branch }}-" | |
kustomize edit add patch --patch "$(cat <<EOF | |
apiVersion: networking.k8s.io/v1 | |
kind: Ingress | |
metadata: | |
name: piveau-ui | |
spec: | |
tls: | |
- secretName: ${{ steps.vars.outputs.branch }}-prev-tls | |
hosts: | |
- ${{ steps.vars.outputs.branch }}.piveau-ln-preview.zazukoians.org | |
EOF | |
)" | |
kustomize edit set image "ghcr.io/opendata-swiss/ods-ui=ghcr.io/opendata-swiss/ods-ui:sha-${{ steps.vars.outputs.short_sha }}" | |
kustomize edit set label "app.kubernetes.io/instance:${{ steps.vars.outputs.branch }}" | |
kustomize build | tee manifest.yaml |