Skip to content

Commit

Permalink
ci(all): do build Docker images, switch from docker/build-push-action…
Browse files Browse the repository at this point in the history
… to @nx-tools/nx-container
  • Loading branch information
NexZhu committed Dec 11, 2023
1 parent e8712e4 commit 1002fa3
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 69 deletions.
88 changes: 24 additions & 64 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,5 @@
name: build

snippets:
step-set-common-vars: &step-set-common-vars
name: Set common variables
id: commonVars
run: |
commit_id_8=${GITHUB_SHA::8}
CONTAINER_REGISTRY_DOMAIN=docker.io
echo commit_id_8=${commit_id_8} >> $GITHUB_OUTPUT
echo repo_url=https://github.com/${{ github.repository }} >> $GITHUB_OUTPUT
step-set-project-vars: &step-set-project-vars
name: Set project variables
id: projectVars
run: |
tmp=${{ github.job }}
proj=${tmp#docker-}
echo proj=$proj >> $GITHUB_OUTPUT
if echo '${{ needs.build.outputs.affectedProjs }}' | grep -qE "(^|,)${proj}($|,)"; then
affected=true
fi
echo affected=${affected} >> $GITHUB_OUTPUT
docker_image=${{ steps.commonVars.outputs.container_registry_domain }}/daotl/web-monorepo-starter-${proj}
echo docker_image=${docker_image} >> $GITHUB_OUTPUT
echo commit_id_image=${docker_image}:commit-${{ steps.commonVars.outputs.commit_id_8 }}
step-docker-setup-buildx: &step-docker-setup-buildx
uses: docker/setup-buildx-action@v3
if: steps.projectVars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'

step-docker-login: &step-docker-login
uses: docker/login-action@v3
if: steps.projectVars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

step-docker-build-push: &step-docker-build-push
uses: docker/build-push-action@v5
if: steps.projectVars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
with:
context: apps/${{ github.job }}
file: apps/${{github.job}}/docker/Dockerfile.ci
push: true
tags: ${{ steps.projectVars.outputs.docker_image }}:commit-${{ steps.commonVars.outputs.commit_id_8 }},${{ steps.projectVars.outputs.docker_image }}:${{ github.ref_name }},${{ steps.projectVars.outputs.docker_image }}:latest
# cache-from: type=registry,ref=${{ steps.projectVars.outputs.docker_image }}:buildcache-${{ github.ref_name }}
# cache-to: type=registry,ref=${{ steps.projectVars.outputs.docker_image }}:buildcache-${{ github.ref_name }},mode=max

job-docker: &job-docker
needs: [build]
runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /toolcache
steps:
- *step-set-common-vars
- *step-set-project-vars
- *step-docker-setup-buildx
- *step-docker-login
- *step-docker-build-push

on:
push:
branches: [main, single-page, feat/*, fix/*, chore/*, debug/*]
Expand All @@ -84,7 +25,13 @@ jobs:
node-version: [18.x, 20.x]

steps:
- *step-set-common-vars
- name: Set common variables
id: common-vars
run: |
commit_id_8=${GITHUB_SHA::8}
CONTAINER_REGISTRY_DOMAIN=docker.io
echo commit_id_8=${commit_id_8} >> $GITHUB_OUTPUT
echo repo_url=https://github.com/${{ github.repository }} >> $GITHUB_OUTPUT
# Needs to come first, `actions/setup-node` uses `pnpm-lock.yaml`
- uses: actions/checkout@v3
Expand Down Expand Up @@ -126,9 +73,22 @@ jobs:
# https://github.com/pnpm/pnpm/issues/6300#issuecomment-1722120409
run: CYPRESS_INSTALL_BINARY=0 ./scripts/pnpm-install-filtered.js --frozen-lockfile=true ${{ steps.affected.outputs.projs }}

- name: build & lint
run: pnpm nx affected -t build lint
- run: pnpm nx affected -t build lint

docker-web: *job-docker
# Build Docker images

docker-desktop: *job-docker
- uses: docker/setup-buildx-action@v3
id: docker-setup-buildx
if: steps.project-vars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'

- uses: docker/login-action@v3
if: steps.project-vars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker images
if: steps.project-vars.outputs.affected == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
INPUT_BUILDER: steps.docker-setup-buildx.outputs.name
run: pnpm nx affected -t container -c ci
25 changes: 24 additions & 1 deletion apps/desktop/project.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
{
"name": "desktop",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/desktop/src",
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"cwd": "./apps/desktop",
"command": "mkdir dist && echo 'app' > dist/index.js"
"command": "mkdir -p dist && echo 'app' > dist/index.js"
}
},
"lint": {
"command": "echo lint",
"options": {}
},
"container": {
"executor": "@nx-tools/nx-container:build",
"options": {
"engine": "docker",
"context": "apps/desktop",
"file": "apps/desktop/docker/Dockerfile.ci",
"tags": [
"daotl/web-monorepo-starter-desktop:dev"
],
"metadata": {
"images": [
"daotl/web-monorepo-starter-desktop"
]
}
},
"configurations": {
"ci": {
// "cache-from": "type=registry,ref=daotl/web-monorepo-starter-desktop:buildcache-${{ github.ref_name }}",
// "cache-to": "type=registry,ref=daotl/web-monorepo-starter-desktop:buildcache-${{ github.ref_name }},mode=max"
}
}
}
}
}
25 changes: 24 additions & 1 deletion apps/web/project.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
{
"name": "web",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/web/src",
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"cwd": "./apps/web",
"command": "mkdir dist && echo 'app' > dist/index.js"
"command": "mkdir -p dist && echo 'app' > dist/index.js"
}
},
"lint": {
"command": "echo lint",
"options": {}
},
"container": {
"executor": "@nx-tools/nx-container:build",
"options": {
"engine": "docker",
"context": "apps/web",
"file": "apps/web/docker/Dockerfile.ci",
"tags": [
"daotl/web-monorepo-starter-web:dev"
],
"metadata": {
"images": [
"daotl/web-monorepo-starter-web"
]
}
},
"configurations": {
"ci": {
// "cache-from": "type=registry,ref=daotl/web-monorepo-starter-web:buildcache-${{ github.ref_name }}",
// "cache-to": "type=registry,ref=daotl/web-monorepo-starter-web:buildcache-${{ github.ref_name }},mode=max"
}
}
}
}
}
22 changes: 22 additions & 0 deletions nx.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ const config = {
cache: true,
},

container: {
dependsOn: ['build'],
cache: true,
options: {
load: true,
platforms: 'linux/amd64,linux/arm64',
},
configurations: {
ci: {
load: false,
push: true,
metadata: {
tags: [
'type=match,pattern=v(.*),group=1',
'type=edge,branch=main',
'type=sha,enabled={{is_default_branch}},prefix=sha-',
],
},
},
},
},

deploy: {
dependsOn: ['build', 'test', 'lint'],
cache: false,
Expand Down
26 changes: 25 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
}
],
"default": [
"sharedGlobals",
"{projectRoot}/**/*",
"sharedGlobals"
"!{projectRoot}/**/?(*.)+spec.ts"
]
},
"targetDefaults": {
Expand Down Expand Up @@ -51,6 +52,29 @@
"input": [],
"cache": true
},
"container": {
"dependsOn": [
"build"
],
"cache": true,
"options": {
"load": true,
"platforms": "linux/amd64,linux/arm64"
},
"configurations": {
"ci": {
"load": false,
"push": true,
"metadata": {
"tags": [
"type=match,pattern=v(.*),group=1",
"type=edge,branch=main",
"type=sha,enabled={{is_default_branch}},prefix=sha-"
]
}
}
}
},
"deploy": {
"dependsOn": [
"build",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@daotl/biome-config": "0.0.4",
"@daotl/tsconfig": "^0.1.2",
"@manypkg/cli": "npm:@daotl/manypkg-cli@^0.21.1",
"@nx-tools/container-metadata": "^5.1.0",
"@nx-tools/nx-container": "^5.1.0",
"@traf/nx": "^0.0.16",
"commitiquette": "^1.2.1",
"commitizen": "^4.3.0",
Expand Down
Loading

0 comments on commit 1002fa3

Please sign in to comment.