ci(root): use @traf/nx to get affected projects #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 | ||
snippets: | ||
Check failure on line 3 in .github/workflows/ci.yml GitHub Actions / buildInvalid workflow file
|
||
step-set-common-vars: &step-set-common-vars | ||
name: Set common variables | ||
id: commonVars | ||
run: | | ||
commit_id_8=${GITHUB_SHA::8} | ||
echo commit_id_8=$commit_id_8 >> $GITHUB_OUTPUT | ||
echo repo_link=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 | ||
docker_image=daotl/web-monorepo-starter-${proj} | ||
echo docker_image=$docker_image >> $GITHUB_OUTPUT | ||
echo commit_id_image=$docker_image:commit-${{ steps.commonVars.outputs.commid_id_8 }} | ||
if [[ ${{ needs.build.outputs.affectedProjs }} =~ (^|,)${proj}($|,) ]]; then | ||
affected = true | ||
else | ||
affected = | ||
fi | ||
echo affected=$affected >> $GITHUB_OUTPUT | ||
step-docker-setup-buildx: &step-docker-setup-buildx | ||
uses: docker/setup-buildx-action@v3 | ||
if: ${{ steps.projectVars.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.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.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.commonVars.outputs.docker_image }}:commit-${{ steps.commonVars.outputs.commit_id_8 }},${{ steps.commonVars.outputs.docker_image }}:${{ github.ref_name }},${{ steps.commonVars.outputs.docker_image }}:latest | ||
# cache-from: type=registry,ref=${{ steps.commonVars.outputs.docker_image }}:buildcache-${{ github.ref_name }} | ||
# cache-to: type=registry,ref=${{ steps.commonVars.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/*] | ||
pull_request: | ||
branches: [main] | ||
workflow_dispatch: {} | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
affectedProjs: ${{ steps.affected.outputs.apps }} | ||
# Workaround for tools (e.g., Node.js) not cached: | ||
# https://gitea.com/gitea/act_runner/issues/70#issuecomment-734824 | ||
env: | ||
RUNNER_TOOL_CACHE: /toolcache | ||
strategy: | ||
matrix: | ||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/ | ||
node-version: [18.x, 20.x] | ||
steps: | ||
- *step-set-common-vars | ||
# Needs to come first, `actions/setup-node` uses `pnpm-lock.yaml` | ||
- uses: actions/checkout@v3 | ||
with: | ||
# TO IMPROVE: We need to fetch all branches and commits so that Nx affected has a base to compare against. | ||
fetch-depth: 0 | ||
- name: Derive appropriate SHAs for base and head for `nx affected` commands | ||
id: setSHAs | ||
uses: nrwl/nx-set-shas@v4 | ||
# - run: | | ||
# echo "BASE: ${{ env.NX_BASE }}" or ${{ steps.setSHAs.outputs.base }}" | ||
# echo "HEAD: ${{ env.NX_HEAD }}" or ${{ steps.setSHAs.outputs.head }}" | ||
- name: install zx,@traf/nx @traf/nx | ||
run: pnpm add -g zx | ||
- name: Calculate affected projects | ||
id: affected | ||
run: | | ||
projs=$(npx @traf/nx affected --base=${NX_BASE} --json=true) | ||
projs=${projs#[} | ||
projs=${projs%]} | ||
echo Affected projects: ${projs} | ||
echo projs=${projs} >> $GITHUB_OUTPUT | ||
- uses: pnpm/action-setup@v2 | ||
with: | ||
version: 8 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
cache: pnpm | ||
- name: pnpm install | ||
# Fix: `pnpm install --filter` installing dependencies of all projects | ||
# https://github.com/pnpm/pnpm/issues/6300#issuecomment-1722120409 | ||
run: CYPRESS_INSTALL_BINARY=0 ./scripts/pnpm-install-filtered.js --frozen-lockfile ${{ steps.affected.outputs.projs }} | ||
- name: build & lint ("main" branch) | ||
if: github.ref == 'refs/heads/main' | ||
run: pnpm nx affected -t build lint | ||
- name: build & lint (not "main" branch) | ||
if: github.ref != 'refs/heads/main' | ||
run: | | ||
# `git fetch`: to workaround https://github.com/actions/checkout/issues/296#issuecomment-1348164731 | ||
# Not needed now for `fetch-depth: 0` | ||
# git fetch origin main:refs/remotes/origin/main | ||
# git reset --soft refs/remotes/origin/main | ||
pnpm add -g concurrently | ||
concurrently "pnpm nx affected -t build" "npx lefthook run pre-commit" | ||
docker-web: *job-docker | ||
docker-desktop: *job-docker |