fix(ci): deploy to testnet #6
This file contains 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: Build and Deploy Docker Images | ||
on: | ||
push: | ||
branches: ['main'] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
service: | ||
[ | ||
api, | ||
app, | ||
app-lite, | ||
backend, | ||
indexer-balance, | ||
indexer-base, | ||
indexer-events, | ||
indexer-dex, | ||
explorer-selector, | ||
aggregates, | ||
] | ||
fail-fast: false | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v3 | ||
with: | ||
platforms: linux/amd64,linux/arm64 | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
- name: Docker Login | ||
uses: docker/[email protected] | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- name: Get short SHA | ||
id: slug | ||
run: echo "sha8=$(echo ${GITHUB_SHA} | cut -c1-8)" >> $GITHUB_OUTPUT | ||
- name: Build and push | ||
id: docker_build | ||
uses: docker/build-push-action@v5 | ||
with: | ||
context: . | ||
file: apps/${{ matrix.service }}/Dockerfile | ||
platforms: linux/amd64,linux/arm64 | ||
push: true | ||
tags: | | ||
ghcr.io/nearblocks/${{ matrix.service }}:latest | ||
ghcr.io/nearblocks/${{ matrix.service }}:${{ steps.slug.outputs.sha8 }} | ||
cache-from: type=registry,ref=ghcr.io/nearblocks/${{ matrix.service }}:latest | ||
cache-to: type=inline | ||
- name: Check for updates | ||
id: check_updates | ||
run: | | ||
if [[ "${{ steps.docker_build.outcome }}" == "success" ]]; then | ||
echo "${{ matrix.service }}=${{ steps.slug.outputs.sha8 }}" >> $GITHUB_OUTPUT | ||
else | ||
echo "${{ matrix.service }} build failed" | ||
fi | ||
deploy: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Set up kubectl | ||
uses: azure/setup-kubectl@v3 | ||
with: | ||
version: 'v1.28.0' | ||
- name: Configure kubectl | ||
run: | | ||
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > kubeconfig | ||
echo "KUBECONFIG=$(pwd)/kubeconfig" >> $GITHUB_ENV | ||
- name: Update Kubernetes deployments | ||
run: | | ||
Check failure on line 91 in .github/workflows/build-deploy.yaml GitHub Actions / Build and Deploy Docker ImagesInvalid workflow file
|
||
declare -A namespaces=(["testnet"]="nearblocks-testnet") | ||
services=(api app app-lite backend indexer-balance indexer-base indexer-events indexer-dex explorer-selector aggregates) | ||
for net in mainnet testnet; do | ||
echo "Switching to namespace ${namespaces[$net]}" | ||
kubectl config set-context --current --namespace=${namespaces[$net]} | ||
for service in "${services[@]}"; do | ||
if [[ -n "${{ needs.build.outputs[service] }}" ]]; then | ||
deployment_name="$net-$service" | ||
echo "Updating deployment for $deployment_name in ${namespaces[$net]}" | ||
kubectl set image deployment/$deployment_name $service=ghcr.io/nearblocks/$service:${{ needs.build.outputs[service] }} | ||
else | ||
echo "Skipping deployment for $net-$service (no updates)" | ||
fi | ||
done | ||
done | ||
- name: Verify rollout | ||
run: | | ||
declare -A namespaces=(["testnet"]="nearblocks-testnet") | ||
services=(api app app-lite backend indexer-balance indexer-base indexer-events indexer-dex explorer-selector aggregates) | ||
for net in mainnet testnet; do | ||
echo "Verifying rollouts in namespace ${namespaces[$net]}" | ||
kubectl config set-context --current --namespace=${namespaces[$net]} | ||
for service in "${services[@]}"; do | ||
if [[ -n "${{ needs.build.outputs[service] }}" ]]; then | ||
deployment_name="$net-$service" | ||
echo "Verifying rollout for $deployment_name in ${namespaces[$net]}" | ||
kubectl rollout status deployment/$deployment_name --timeout=300s | ||
fi | ||
done | ||
done | ||
- name: Rollback failed deployments | ||
if: failure() | ||
run: | | ||
declare -A namespaces=(["testnet"]="nearblocks-testnet") | ||
services=(api app app-lite backend indexer-balance indexer-base indexer-events indexer-dex explorer-selector aggregates) | ||
for net in mainnet testnet; do | ||
echo "Checking for failed deployments in namespace ${namespaces[$net]}" | ||
kubectl config set-context --current --namespace=${namespaces[$net]} | ||
for service in "${services[@]}"; do | ||
if [[ -n "${{ needs.build.outputs[service] }}" ]]; then | ||
deployment_name="$net-$service" | ||
echo "Attempting rollback for $deployment_name in ${namespaces[$net]}" | ||
kubectl rollout undo deployment/$deployment_name | ||
fi | ||
done | ||
done | ||
- name: Notify on failure | ||
if: failure() | ||
run: | | ||
echo "Deployment failed. Manual intervention may be required." |