From 4d08478664996bf34d2db1c931a48e3889036173 Mon Sep 17 00:00:00 2001 From: Joosep Alviste Date: Sat, 6 Jan 2024 10:31:26 +0200 Subject: [PATCH] ci: build images with caching with docker buildx Caching should work for this for GitHub Actions. Also, we're only building the images for projects that have actually changed (with the help of `nx`). https://docs.docker.com/build/cache/backends/gha/ Related to #83 --- .github/workflows/default.yml | 10 +++++----- apps/api/package.json | 1 + apps/webapp/package.json | 1 + nx.json | 4 ++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 9373ff1..7f7031e 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -80,6 +80,9 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Expose GitHub Runtime + uses: crazy-max/ghaction-github-runtime@v3 + - name: Setup run: | cp .env.example .env @@ -87,11 +90,8 @@ jobs: - name: Build containers run: | echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin - sed -i.bak -e "s/VITE_API_HOST=.*/VITE_API_HOST=api.serieslist.joosep.xyz/" .env - sed -i.bak -e "s/VITE_API_URL=.*/VITE_API_URL=https:\/\/\$VITE_API_HOST/" .env - docker compose -f docker-compose.production.yml pull webapp api || true - docker compose -f docker-compose.production.yml build webapp api - docker compose -f docker-compose.production.yml push + sed -i.bak -e "s/VITE_API_URL=.*/VITE_API_URL=https:\/\/api.serieslist.joosep.xyz/" .env + pnpm exec nx affected -t build:docker - name: Install SSH Key uses: shimataro/ssh-key-action@v2 diff --git a/apps/api/package.json b/apps/api/package.json index c8882a8..4a7bb21 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -4,6 +4,7 @@ "type": "module", "scripts": { "build": "NODE_ENV=production rimraf dist && tsx bin/build.ts", + "build:docker": "(cd ../.. && docker buildx build --push --cache-to type=gha,mode=max,scope=webapp --cache-from type=gha,scope=webapp -t ghcr.io/joosepalviste/serieslist-api:latest -f apps/api/Dockerfile --target production .)", "clear-test-db": "dotenv -e ../../.env.test tsx bin/clearDatabase.ts", "codegen": "DOTENV_CONFIG_PATH='../../.env' graphql-codegen-esm -r dotenv/config", "codegen:db": "tsx bin/kyselyCodegen.ts && eslint --fix src/generated/db.ts", diff --git a/apps/webapp/package.json b/apps/webapp/package.json index 1f3bbeb..a119354 100644 --- a/apps/webapp/package.json +++ b/apps/webapp/package.json @@ -4,6 +4,7 @@ "type": "module", "scripts": { "build": "rimraf dist && vite build && pnpm build:server", + "build:docker": "(cd ../.. && docker buildx build --push --cache-to type=gha,mode=max,scope=webapp --cache-from type=gha,scope=webapp -t ghcr.io/joosepalviste/serieslist-webapp:latest -f apps/webapp/Dockerfile --target production --build-arg=\"VITE_APP_PORT=$VITE_APP_PORT\" --build-arg=\"VITE_API_URL=$VITE_API_URL\" --build-arg=\"VITE_API_INTERNAL_URL=$VITE_API_INTERNAL_URL\" .)", "build:server": "tsx bin/build.ts && resolve-tspaths --out dist/prodServer", "codegen": "graphql-codegen", "lint": "eslint src && madge --circular --extensions ts,tsx ./src", diff --git a/nx.json b/nx.json index 5dbaacb..896286d 100644 --- a/nx.json +++ b/nx.json @@ -15,6 +15,10 @@ "cache": true, "inputs": ["production", "^production"] }, + "build:docker": { + "cache": true, + "defaultConfiguration": "docker" + }, "tsc": { "cache": true },