• Stars
    star
    141
  • Rank 251,210 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✨ A Github Action which sets the base and head SHAs required for `nx affected` commands in CI

Nx - Smart, Extensible Build Framework

Set SHAs Action

✨ A Github Action which sets the base and head SHAs required for the nx affected commands in CI

NOTE: This documentation is for version 2.x.x+ which now uses the GitHub API to track successful workflows. You can find documentation for version 1.x.x which used GIT tags here.

Example Usage

.github/workflows/ci.yml

# ... more CI config ...

jobs:
  myjob:
    runs-on: ubuntu-latest
    name: My Job
    steps:
      - uses: actions/checkout@v3
        with:
          # We need to fetch all branches and commits so that Nx affected has a base to compare against.
          fetch-depth: 0

      # In any subsequent steps within this job (myjob) we can reference the resolved SHAs
      # using either the step outputs or environment variables:

      # ===========================================================================
      # OPTION 1) Environment variables
      # ===========================================================================
      - name: Derive appropriate SHAs for base and head for `nx affected` commands
        uses: nrwl/nx-set-shas@v3
    
      - run: |
          echo "BASE: ${{ env.NX_BASE }}"
          echo "HEAD: ${{ env.NX_HEAD }}"

      # ===========================================================================
      # OPTION 2) Step outputs (in this case we must give the step an "id")
      # ===========================================================================
      - name: Derive appropriate SHAs for base and head for `nx affected` commands
        id: setSHAs
        uses: nrwl/nx-set-shas@v3
    
      - run: |
          echo "BASE: ${{ steps.setSHAs.outputs.base }}"
          echo "HEAD: ${{ steps.setSHAs.outputs.head }}"

      # ... more CI config ...

Configuration Options

- uses: nrwl/nx-set-shas@v3
  with:
    # The "main" branch of your repository (the base branch which you target with PRs).
    # Common names for this branch include main and master.
    #
    # Default: main
    main-branch-name: ''

    # Applies the derived SHAs for base and head as NX_BASE and NX_HEAD environment variables within the current Job.
    #
    # Default: true
    set-environment-variables-for-job: ''

    # By default, if no successful workflow run is found on the main branch to determine the SHA, we will log a warning and use HEAD~1. Enable this option to error and exit instead.
    #
    # Default: false
    error-on-no-successful-workflow: ''

    # The type of event to check for the last successful commit corresponding to that workflow-id, e.g. push, pull-request, release etc.
    #
    # Default: push
    last-successful-event: ''

    # The path where your repository is. This is only required for cases where the repository code is checked out or moved to a specific path.
    #
    # Default: .
    working-directory: ''

    # The ID of the github action workflow to check for successful run or the name of the file name containing the workflow. 
    # E.g. 'ci.yml'. If not provided, current workflow id will be used
    #
    workflow-id: ''

Permissions in v2+

This Action uses Github API to find the last successful workflow run. If your GITHUB_TOKEN has restrictions set please ensure you override them for the workflow to enable read access to actions and contents:

jobs:
  myjob:
    runs-on: ubuntu-latest
    name: My Job
    permissions:
      contents: 'read'
      actions: 'read'

Background

When we run the affected command on Nx, we can specify 2 git history positions - base and head, and it calculates which projects in your repository changed between those 2 commits. We can then run a set of tasks (like building or linting) only on those affected projects.

This makes it easy to set up a CI system that scales well with the continuous growth of your repository, as you add more and more projects.

Problem

Figuring out what these two git commits are might not be as simple as it seems.

On a CI system that runs on submitted PRs, we determine what commits to include in the affected calculation by comparing our HEAD-commit-of-PR-branch to the commit in the main branch (master or main usually) from which the PR branch originated. This will ensure the entirety of our PR is always being tested.

But what if we want to set up a continuous deployment system that, as changes get pushed to master, builds and deploys only the affected projects? What are the FROM and TO commits in that case?

Conceptually, what we want is to use the absolute latest commit on the master branch as the HEAD, and the previous successful commit on the master as the BASE. Note, we want the previous successful one because it is still possible for commits on the master branch to fail for a variety of reasons.

The commits therefore can't just be HEAD and HEAD~1. If a few deployments fail one after another, that means that we're accumulating a list of affected projects that are not getting deployed. Anytime we retry the deployment, we want to include every commit since the last time we deployed successfully. That way we ensure we don't accidentally skip deploying a project that has changed.

This action enables you to find:

  • Commit SHA from which PR originated (in the case of pull_request)
  • Commit SHA of the last successful CI run

License

MIT

Copyright (c) 2021-present Narwhal Technologies Inc.

More Repositories

1

nx

Smart Monorepos Β· Fast CI
TypeScript
22,163
star
2

nx-console

Nx Console is the user interface for Nx & Lerna.
TypeScript
1,273
star
3

nx-examples

Example repo for Nx workspace
TypeScript
843
star
4

precise-commits

✨ Painlessly apply Prettier by only formatting lines you have modified anyway!
TypeScript
470
star
5

webpack-plugin-critical

Webpack wrapper for @addyosmani's critical library.
TypeScript
295
star
6

monorepo.tools

Your defacto guide on monorepos, and in depth feature comparisons of tooling solutions.
TypeScript
271
star
7

nx-recipes

Common recipes to productively use Nx with various technologies and in different setups. Made with ❀️ by the Nx Team
TypeScript
213
star
8

nx-workshop

Companion labs for the workshop: "Develop at Scale with Nx Monorepos"
TypeScript
171
star
9

nx-labs

A collection of Nx plugins
TypeScript
128
star
10

nx-azure-build

Example of setting up distributed Azure build for Nx workspace
TypeScript
113
star
11

nx-react-native

86
star
12

react-module-federation

Example showing how to use Nx's new module federation support to speed up the builds of React apps
TypeScript
69
star
13

nx-incremental-large-repo

TypeScript
65
star
14

nx-jenkins-build

Example of setting up distributed Jenkins build for Nx workspace
TypeScript
63
star
15

ci

TypeScript
47
star
16

nx-react-workshop

TypeScript
43
star
17

last-successful-commit-action

GitHub action for identifying the last successful commit for a given workflow and branch.
JavaScript
43
star
18

node-microservices

TypeScript
42
star
19

nx-angular-and-react

TypeScript
42
star
20

blog-post-nx-production

TypeScript
36
star
21

nx-apollo-react-example

TypeScript
34
star
22

gatsby

Nx plugin for Gatsby
TypeScript
32
star
23

nx-bazel-example

Python
31
star
24

nx-distributed-cache-example

Nx workspace connected to Nx Cloud's distributed cache
TypeScript
31
star
25

ng-module-federation

Example showing to use new Nx's module federation support to speed up the builds of Angular apps
TypeScript
31
star
26

example-nx-fullstack

TypeScript
29
star
27

nx-example-multirepo

28
star
28

react-nx-example

Example for the blog post
TypeScript
27
star
29

nx-apollo-angular-example

TypeScript
26
star
30

ngrx-workshop-app

NgRx Workshop Example Application based on Shopping Cart from Angular.io docs
TypeScript
25
star
31

ngrx-example

Application Showing How to Use NgRx
TypeScript
22
star
32

add-nx

JavaScript
22
star
33

ts-aoc-starter

TypeScript
22
star
34

nx-cloud-helm

Nx Cloud Helm Charts
Smarty
21
star
35

playbook-angular-v6-elements-example

TypeScript
21
star
36

board-game-hoard

TypeScript
20
star
37

workshop-nx-labs

Lab exercises for the Nx Enterprise workshop(s)
20
star
38

workshop-nx-starter

Starter repo for the Nx Enterprise workshop(s).
TypeScript
20
star
39

nx-orb

✨ A CircleCI Orb which includes helpful commands for running Nx commands in the CI
JavaScript
19
star
40

nx-expo

Official Expo plugin for Nx (alpha)
19
star
41

ngrx_race_condition

TypeScript
18
star
42

angular-reactive-forms-course

TypeScript
18
star
43

bazel-demo-app

Bazel Demo Application for Blog Post
TypeScript
18
star
44

angular-vscode

Useful VSCode plugins for Angular Development
TypeScript
17
star
45

nx-apollo-example

TypeScript
15
star
46

nx-go-project-graph-plugin

ℹ️ Please take a look at the plugin https://github.com/nx-go/nx-go if you want to use Go and Nx together
Go
15
star
47

nx-tag-successful-ci-run

✨ A Github Action which tags the repo, intended to be used only when on the main branch of your repo at the end of an Nx powered CI run
13
star
48

full-stack-react-example-app

TypeScript
12
star
49

bazel-rollup

TypeScript
11
star
50

nxcloud-k8s-setup

Example of running Nx Cloud on K8s
10
star
51

cra-to-nx

Tool for converting CRA project to Nx workspace
TypeScript
9
star
52

advent-of-code-starter

TypeScript
9
star
53

nx-cypress-command-sharing

Example repo to showcase how to share Cypress commands within various e2e tests in a Nx monorepo
TypeScript
8
star
54

pokemon-supabase-example

TypeScript
8
star
55

nx-trpc-demo

TypeScript
7
star
56

nx-ecosystem-ci

TypeScript
7
star
57

upgrade-with-elements

TypeScript
7
star
58

nx-shops

TypeScript
6
star
59

nx-custom-layout

TypeScript
6
star
60

bazel-cli-build

JavaScript
6
star
61

nx-migrate-angularjs-example

An example repo demonstrating a migration of an AngularJS application into an Nx workspace
JavaScript
6
star
62

bazel-docker-example

Python
6
star
63

cra-to-nx-migration

TypeScript
6
star
64

tic-tac-toe-playwright

TypeScript
6
star
65

nx-devkit-schematics

An example library that uses Nx Devkit to author Angular Schematics
TypeScript
6
star
66

zack-live-stream

The code base being worked on by Zack's livestream
TypeScript
6
star
67

nxcloud-k8s

Nx Cloud Kubernetes Example
6
star
68

angular-react-node

TypeScript
6
star
69

nx-cloud-workflows

JavaScript
5
star
70

nx-plugin-community-example

TypeScript
5
star
71

trpc-livestream

TypeScript
5
star
72

stackblitz-nx-angular

StackBlitz template for an Nx monorepo with Angular
TypeScript
4
star
73

react-to-solid

TypeScript
4
star
74

large-ts-monorepo

TypeScript
4
star
75

simple-portal-example

TypeScript
4
star
76

workshop-ngconf-18-labs

Lab exercises for the ng-conf 2018 workshop
4
star
77

transfer-state

TypeScript
3
star
78

lazy-spinners

TypeScript
3
star
79

angular-console

3
star
80

nx-incremental

Example showing how to set up incremental builds with Nx
TypeScript
3
star
81

workshop-pwa-fishgoco

Demo project for PWA workshop
TypeScript
3
star
82

nx-example-workspace

TypeScript
3
star
83

count-contributors-sample

Sample Repo for Node CLI exercise
JavaScript
2
star
84

bug-repro-circular

TypeScript
2
star
85

nx-example-sharedlib

TypeScript
2
star
86

hybrid-sample

TypeScript
2
star
87

nrwl-coding-assignment

TypeScript
2
star
88

release-js-and-rust

Example of versioning and publishing JS and Rust code together using nx release
Rust
2
star
89

spring-boot-livestream

TypeScript
2
star
90

stackblitz-nx-react

StackBlitz template for an Nx monorepo with React
TypeScript
2
star
91

using-tsc-b-with-next

TypeScript
2
star
92

angular-module-federation-example

TypeScript
2
star
93

nx-coding-assignment

Coding assignment for JavaScript/TypeScript developers
TypeScript
2
star
94

workshop-upgrade-angular-project

Angular version of TuskDesk demo project for upgrade workshops
TypeScript
2
star
95

nrwl-coding-assignment-angular

TypeScript
2
star
96

named-outlets

TypeScript
2
star
97

workshop-upgrade-angularjs-starter

AngularJS version of TuskDesk demo project for workshops
JavaScript
2
star
98

gatsby-example

Example repository that shows Gatsby and Nx in action
TypeScript
2
star
99

nx-14-5-cypress-10-example

TypeScript
1
star
100

.github

Nrwl GitHub Org profile
1
star