name: "Deploy to development" on: push: branches: - "development" jobs: build-deployment-images: name: "Build deployment images" runs-on: "ubuntu-latest" steps: - name: "Checkout the code" uses: "actions/checkout@v2" - name: "Get build options" id: "get-build-options" uses: "./.gitea/actions/get-build-options" - name: "Get `backend` version" id: "get-backend-version" uses: "./.gitea/actions/get-service-version" with: service: "backend" - name: "Import Secrets" id: "import-secrets" uses: "hashicorp/vault-action@v2" with: url: "https://vault.bthlabs.pl/" method: "approle" roleId: "${{ secrets.VAULT_ROLE_ID }}" secretId: "${{ secrets.VAULT_SECRET_ID }}" secrets: | gitea/data/docker-hosted.nexus.bthlabs.pl username | DOCKER_USERNAME ; gitea/data/docker-hosted.nexus.bthlabs.pl password | DOCKER_PASSWORD - name: "Set up Docker Buildx" id: "setup-docker-buildx" uses: "docker/setup-buildx-action@v3" with: driver: "remote" endpoint: "tcp://builder-01.bthlab:2375" platforms: "linux/amd64" append: | - endpoint: "tcp://builder-mac-01.bthlab:2375" platforms: "linux/arm64" - name: "Login to Docket Registry" uses: "docker/login-action@v3" with: registry: "docker-hosted.nexus.bthlabs.pl" username: "${{ steps.import-secrets.outputs.DOCKER_USERNAME }}" password: "${{ steps.import-secrets.outputs.DOCKER_PASSWORD }}" - name: "Build `backend-deployment` image" env: SHORT_SHA: "${{ steps.get-build-options.outputs.short-sha }}" VERSION: "${{ steps.get-backend-version.outputs.version }}" BUILD: "${{ steps.get-backend-version.outputs.build-number }}" run: | set -x docker buildx build \ --cache-from "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket" \ --cache-to "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max" \ --push \ --platform linux/amd64,linux/arm64 \ --build-arg IMAGE_ID="deployment.${SHORT_SHA}" \ -f services/backend/Dockerfile \ --target deployment \ -t "docker-hosted.nexus.bthlabs.pl/hotpocket/backend:deployment-${VERSION}-${BUILD}" \ services/ - name: "Build `backend-aio` image" env: SHORT_SHA: "${{ steps.get-build-options.outputs.short-sha }}" VERSION: "${{ steps.get-backend-version.outputs.version }}" BUILD: "${{ steps.get-backend-version.outputs.build-number }}" run: | set -x docker buildx build \ --cache-from "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket" \ --cache-to "type=registry,ref=nexus.bthlab.bthlabs.net:8001/hotpocket,mode=max" \ --push \ --platform linux/amd64,linux/arm64 \ --build-arg IMAGE_ID="aio.${SHORT_SHA}" \ -f services/backend/Dockerfile \ --target aio \ -t "docker-hosted.nexus.bthlabs.pl/hotpocket/backend:aio-${VERSION}-${BUILD}" \ services/ deploy: name: "Deploy" runs-on: "ubuntu-latest" needs: - "build-deployment-images" env: KUBERNETES_NAMESPACE: "hotpocket-development" KUBERNETES_CLUSTER: "k8s.bthlab" steps: - name: "Checkout the code" uses: "actions/checkout@v2" - name: "Get run info" id: "get-run-info" uses: "./.gitea/actions/get-run-info" with: compose-project-base: "${{ vars.COMPOSE_PROJECT_BASE }}" - name: "Get build options" id: "get-build-options" uses: "./.gitea/actions/get-build-options" - name: "Get `backend` version" id: "get-backend-version" uses: "./.gitea/actions/get-service-version" with: service: "backend" - name: "Setup k8s" uses: "./.gitea/actions/setup-k8s" with: arch: "${{ steps.get-build-options.outputs.build-arch }}" - name: "Import Secrets" id: "import-secrets" uses: "hashicorp/vault-action@v2" with: url: "https://vault.bthlabs.pl/" method: "approle" roleId: "${{ secrets.VAULT_ROLE_ID }}" secretId: "${{ secrets.VAULT_SECRET_ID }}" secrets: | gitea/data/k8s.bthlab config | KUBECONFIG_PAYLOAD - name: "Set up kubeconfig" env: COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}" KUBECONFIG_PAYLOAD: "${{ steps.import-secrets.outputs.KUBECONFIG_PAYLOAD }}" run: | set -x echo ${KUBECONFIG_PAYLOAD} | base64 -d >"/opt/k8s/etc/kubeconfig" export KUBECONFIG="/opt/k8s/etc/kubeconfig" /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER} /opt/k8s/bin/kubectl get node - name: "Run `backend` Django migrations" env: COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}" BACKEND_TAG: "deployment-${{ steps.get-backend-version.outputs.version }}-${{ steps.get-backend-version.outputs.build-number }}" run: | set -x ( cd deployment/hotpocket.bthlab ; export KUBECONFIG="/opt/k8s/etc/kubeconfig" ; /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER} ; /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} set image cronjobs/backend-job-migrations migrations=docker-hosted.nexus.bthlabs.pl/hotpocket/backend:${BACKEND_TAG} ; /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} delete jobs --ignore-not-found=true backend-job-migrations ; /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} create job backend-job-migrations --from=cronjob/backend-job-migrations ; /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} wait --for=condition=complete --timeout=300s job/backend-job-migrations ) - name: "Deploy" env: COMPOSE_PROJECT: "${{ steps.get-run-info.outputs.compose-project }}" BACKEND_TAG: "deployment-${{ steps.get-backend-version.outputs.version }}-${{ steps.get-backend-version.outputs.build-number }}" run: | set -x ( cd deployment/hotpocket.bthlab ; export KUBECONFIG="/opt/k8s/etc/kubeconfig" ; /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER} ; /opt/k8s/bin/kustomize edit set image hotpocket-backend=docker-hosted.nexus.bthlabs.pl/hotpocket/backend:${BACKEND_TAG} ; /opt/k8s/bin/kustomize build . | /opt/k8s/bin/kubectl apply -f - )