Skip to content

Commit

Permalink
feat(): add test docker caching layer (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: muhammad-asghar-ali <[email protected]>
Co-authored-by: Yousuf Jawwad <[email protected]>
  • Loading branch information
3 people authored Feb 12, 2024
1 parent 2f1cbef commit d8a002c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 39 deletions.
102 changes: 70 additions & 32 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,44 @@ env:
GCR_SERVICE: quantm-api-live-v1

jobs:
release-artifact:
build:
if: github.event.pull_request.merged == true
runs-on: ubuntu-20.04
permissions:
contents: "read"
id-token: "write"
contents: 'read'
id-token: 'write'

steps:
- name: checkout
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v4

- name: Set variables
id: vars
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Set up QEMU (for cross-compilation support)
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Go Build Cache for Docker
uses: actions/cache@v4
with:
path: go-build-cache
key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }}

- name: Authenticate with GCP
id: auth
uses: "google-github-actions/auth@v1"
uses: 'google-github-actions/auth@v2'
with:
token_format: access_token
credentials_json: ${{ secrets.GCP_JSON_KEY }}
access_token_lifetime: 600s

- name: Login into Google Artifact Registry
uses: docker/login-action@v1
id: auth-gcp-artifact-registry
uses: docker/login-action@v3
with:
registry: ${{ env.GCP_ARTIFACT_REPOSITORY }}
username: oauth2accesstoken
Expand All @@ -52,48 +65,73 @@ jobs:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_CLUSTER_ZONE }}

- name: Build & Push docker image for API
run: |
docker build --target api -t quantm/api:latest .
docker tag quantm/api:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/api:latest
docker tag quantm/api:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/api:${{ steps.vars.outputs.short_sha }}
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/api:latest
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/api:${{ steps.vars.outputs.short_sha }}
- name: Inject go-build-cache into Docker
# v1 was composed of two actions: "inject" and "extract".
# v2 is unified to a single action.
uses: reproducible-containers/[email protected]
with:
cache-source: go-build-cache

- name: Build & Push docker image for migrate
run: |
docker build --target migrate -t quantm/migrate:latest .
docker tag quantm/migrate:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:latest
docker tag quantm/migrate:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:${{ steps.vars.outputs.short_sha }}
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:latest
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:${{ steps.vars.outputs.short_sha }}
- name: Build & Push Docker Image - API
id: build-api
uses: docker/build-push-action@v5
with:
context: .
target: api
cache-from: type=gha
cache-to: type=gha,mode=max
push: true
tags: |
${{ env.GCP_ARTIFACT_REPOSITORY }}/api:latest
${{ env.GCP_ARTIFACT_REPOSITORY }}/api:${{ steps.vars.outputs.short_sha }}
- name: Build & Push docker image for mothership
run: |
docker build --target mothership -t quantm/mothership:latest .
docker tag quantm/mothership:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:latest
docker tag quantm/mothership:latest ${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:${{ steps.vars.outputs.short_sha }}
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:latest
docker push ${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:${{ steps.vars.outputs.short_sha }}
- name: Build & Push Docker Image - Mothership
id: build-mothership
uses: docker/build-push-action@v5
with:
context: .
target: mothership
cache-from: type=gha
cache-to: type=gha,mode=max
push: true
tags: |
${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:latest
${{ env.GCP_ARTIFACT_REPOSITORY }}/mothership:${{ steps.vars.outputs.short_sha }}
- name: Build & Push Docker Image - Migrate
id: build-migrate
uses: docker/build-push-action@v5
with:
context: .
target: migrate
cache-from: type=gha
cache-to: type=gha,mode=max
push: true
tags: |
${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:latest
${{ env.GCP_ARTIFACT_REPOSITORY }}/migrate:${{ steps.vars.outputs.short_sha }}
- name: Get deployments
- name: Get Quantm Deployments in GKE
id: get-gke-deployments
run: |
kubectl get -n quantm deploy
- name: Deploy mothership docker image to the GKE cluster
id: deploy-mothership
run: |
kubectl patch -n quantm deployment mothership -p \
'{"spec":{"template":{"spec":{"containers":[{"name":"mothership","image":"us-docker.pkg.dev/quantm-live/default-live-v1/mothership:${{ steps.vars.outputs.short_sha }}"}]}}}}'
- name: Deploy API to Cloud Run
id: deployAPI
id: deploy-api
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ env.GCR_SERVICE }}
image: ${{ env.GCP_ARTIFACT_REPOSITORY }}/api:${{ steps.vars.outputs.short_sha }}

- name: Use output
run: curl ${{ steps.deployAPI.outputs.url }}
- name: Healthcheck for API
id: healthcheck-api
run: curl ${{ steps.deploy-api.outputs.url }}

- name: docker ls
run: docker image ls
19 changes: 12 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
FROM cgr.dev/chainguard/go:latest as src
FROM cgr.dev/chainguard/go:latest as base
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/root/.cache/go-build go mod download

FROM base as src

WORKDIR /src
COPY . .

# migrate

# migrate
FROM src as build-migrate
RUN go build -o ./build/migrate ./cmd/jobs/migrate
RUN --mount=type=cache,target=/root/.cache/go-build go build -o ./build/migrate ./cmd/jobs/migrate

FROM cgr.dev/chainguard/glibc-dynamic:latest as migrate

COPY --from=build-migrate /src/build/migrate /bin/migrate
CMD ["/bin/migrate"]

# mothership

# mothership
FROM src as build-mothership
RUN go build -o ./build/mothership ./cmd/workers/mothership
RUN --mount=type=cache,target=/root/.cache/go-build go build -o ./build/mothership ./cmd/workers/mothership

FROM cgr.dev/chainguard/glibc-dynamic:latest as mothership

COPY --from=build-mothership /src/build/mothership /bin/mothership
CMD ["/bin/mothership"]

# API

# api
FROM src as build-api
RUN go build -o ./build/api ./cmd/api
RUN --mount=type=cache,target=/root/.cache/go-build go build -o ./build/api ./cmd/api

FROM cgr.dev/chainguard/glibc-dynamic:latest as api

Expand Down

0 comments on commit d8a002c

Please sign in to comment.