Skip to content

Commit

Permalink
Merge pull request #11 from tablelandnetwork/bcalza/k8s
Browse files Browse the repository at this point in the history
Adds k8s files and CD workflow
  • Loading branch information
brunocalza authored Oct 20, 2023
2 parents 409f220 + 24acf70 commit 87aba6d
Show file tree
Hide file tree
Showing 16 changed files with 8,263 additions and 0 deletions.
127 changes: 127 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
name: Deploy

concurrency:
group: deploy-${{github.ref}}
cancel-in-progress: true

on:
push:
branches:
- "**"
release:
types:
- created

env:
GKE_REGION: us-east1
GKE_CLUSTER: basin
CONTAINER_REGISTRY: us-west1-docker.pkg.dev

jobs:
deploy:
if: github.event_name == 'release' || github.ref == 'refs/heads/main' || contains(github.event.head_commit.message, '[staging]')
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"

- name: Checkout
uses: actions/checkout@v4

- name: Prepare
id: prep
run: |
if [[ ${{ github.event_name }} == 'release' ]]; then
echo "DEPLOYMENT=staging" >> "$GITHUB_ENV"
else
echo "DEPLOYMENT=staging" >> "$GITHUB_ENV"
fi
echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> "$GITHUB_ENV"
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set up cargo cache
uses: actions/cache@v3
continue-on-error: false
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Buildx patch
run: |
Dockfiles="$(find $1 -name 'Dockerfile')"
d=$(date +%s)
i=0
for file in $Dockfiles; do
i=$(( i + 1 ))
echo "patching timestamp for $file"
touch -d @$(( d + i )) "$file"
done
- name: Google Auth
uses: 'google-github-actions/auth@v1'
with:
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

- name: Install Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
version: 415.0.0
project_id: ${{ secrets.GCP_PROJECT }}

- name: Configure Docker
run: gcloud auth configure-docker $CONTAINER_REGISTRY

- name: Build and push worker
uses: docker/build-push-action@v5
with:
context: ./
file: ./Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: ${{ env.CONTAINER_REGISTRY }}/${{ secrets.GCP_PROJECT }}/textile/basin/basin_worker:sha-${{ env.SHA_SHORT}}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
build-args: CRATE=basin_worker

- name: Build and push exporter
uses: docker/build-push-action@v5
with:
context: ./
file: ./Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: ${{ env.CONTAINER_REGISTRY }}/${{ secrets.GCP_PROJECT }}/textile/basin/basin_exporter:sha-${{ env.SHA_SHORT }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
build-args: CRATE=basin_exporter

- name: Get GKE Credentials
uses: 'google-github-actions/get-gke-credentials@v1'
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_REGION }}

- name: Deploy to GKE
working-directory: ./k8s
run: |
DEPLOYMENT=$DEPLOYMENT IMAGE_BASIN_WORKER=$CONTAINER_REGISTRY/${{ secrets.GCP_PROJECT }}/textile/basin/basin_worker:sha-$SHA_SHORT IMAGE_BASIN_EXPORTER=$CONTAINER_REGISTRY/${{ secrets.GCP_PROJECT }}/textile/basin/basin_exporter:sha-$SHA_SHORT make deploy
7 changes: 7 additions & 0 deletions k8s/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

KUSTOMIZE=go run sigs.k8s.io/kustomize/kustomize/v5@latest

deploy:
cd ${DEPLOYMENT} && $(KUSTOMIZE) edit set image textile/basin_worker=${IMAGE_BASIN_WORKER} textile/basin_exporter=${IMAGE_BASIN_EXPORTER}
cd ${DEPLOYMENT} && $(KUSTOMIZE) build . | sed -e 's/\x27"3000"\x27/3000/g' | kubectl apply -f -
.PHONY: deploy
61 changes: 61 additions & 0 deletions k8s/base/exporter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: basin-exporter
labels:
app: basin-exporter
spec:
replicas: 1
selector:
matchLabels:
app: basin-exporter-v1
template:
metadata:
labels:
app: basin-exporter-v1
spec:
containers:
- name: basin-exporter
image: textile/basin_exporter
imagePullPolicy: Always
command: ["/bin/app"]
args: [
"--export-bucket", "$(EXPORT_BUCKET)",
"--database-url", "$(DATABASE_URL)",
"--export-credentials", "$(EXPORT_CREDENTIALS)",
"--bind-health-address", "$(BIND_HEALTH_ADDRESS)",
#"--export-schedule", "$(EXPORT_SCHEDULE)",
"-vvvvv"
]
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: basin-secret
key: database-url
- name: EXPORT_CREDENTIALS
valueFrom:
secretKeyRef:
name: basin-secret
key: export-credentials
envFrom:
- configMapRef:
name: configmap-basin-exporter
resources:
requests:
cpu: 250m
memory: 512Mi
readinessProbe:
httpGet:
path: /health
port: 3001
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
livenessProbe:
httpGet:
path: /health
port: 3001
initialDelaySeconds: 15
periodSeconds: 20
failureThreshold: 3
Loading

0 comments on commit 87aba6d

Please sign in to comment.