Skip to content

Commit

Permalink
fix!: always resolve SHA against provided main branch (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Sep 8, 2022
1 parent 5a9542b commit 8b116da
Show file tree
Hide file tree
Showing 8 changed files with 2,141 additions and 24 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/integration-test-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: "Integration test workflow"

on:
workflow_call:
inputs:
main-branch-name:
required: false
type: string
default: main
runs-on:
required: false
type: string
default: ubuntu-latest
working-directory:
required: false
type: string

jobs:
main:
runs-on: ${{ inputs.runs-on }}
name: Run
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v2
name: Checkout [Pull Request]
if: ${{ github.event_name == 'pull_request' }}
with:
# By default, PRs will be checked-out based on the Merge Commit, but we want the actual branch HEAD.
ref: ${{ github.event.pull_request.head.sha }}
# We need to fetch all branches and commits so that Nx affected has a base to compare against.
fetch-depth: 0

- uses: actions/checkout@v2
name: Checkout [Default Branch]
if: ${{ github.event_name != 'pull_request' }}
with:
# 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
uses: ./
with:
main-branch-name: ${{ inputs.main-branch-name }}

- name: Verify default PR Workflow
if: ${{ github.event_name == 'pull_request' }}
run: |
BASE_SHA=$(echo $(git merge-base origin/${{github.base_ref}} HEAD))
HEAD_SHA=$(git rev-parse HEAD)
node -e "if(process.env.NX_BASE == '${BASE_SHA}') console.log('Base set correctly'); else { throw new Error('Base not set correctly!');}"
node -e "if(process.env.NX_HEAD == '${HEAD_SHA}') console.log('Head set correctly'); else { throw new Error('Head not set correctly!');}"
- name: Verify default Push Workflow
if: ${{ github.event_name != 'pull_request' }}
run: |
BASE_SHA=$(echo $(git rev-parse HEAD~1))
HEAD_SHA=$(echo $(git rev-parse HEAD))
node -e "if(process.env.NX_BASE == '${BASE_SHA}') console.log('Base set correctly'); else { throw new Error('Base not set correctly!');}"
node -e "if(process.env.NX_HEAD == '${HEAD_SHA}') console.log('Head set correctly'); else { throw new Error('Head not set correctly!');}"
26 changes: 26 additions & 0 deletions .github/workflows/test-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: "Test Integration"

on:
push:
paths-ignore:
- "**.md"
pull_request:
paths-ignore:
- "**.md"

concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
cancel-in-progress: true

jobs:
test:
name: Test workflow integration
strategy:
matrix:
runs-on: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: false
uses: ./.github/workflows/integration-test-workflow.yml
with:
working-directory: integration-test
main-branch-name: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref_name }}
runs-on: ${{ matrix.runs-on }}
74 changes: 74 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: "Test"

on:
push:
paths-ignore:
- "**.md"
pull_request:
paths-ignore:
- "**.md"

concurrency:
group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
cancel-in-progress: true

jobs:
test:
name: Test
strategy:
matrix:
runs-on: [ubuntu-latest, macos-latest, windows-latest]
fail-fast: false
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v2
name: Checkout [Pull Request]
if: ${{ github.event_name == 'pull_request' }}
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- uses: actions/checkout@v2
name: Checkout [Push]
if: ${{ github.event_name != 'pull_request' }}
with:
fetch-depth: 0

- name: Set Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install
run: npm ci

- name: Compile
run: npm run build

- name: Test default PR Workflow
if: github.event_name == 'pull_request'
uses: ./
with:
main-branch-name: ${{ github.base_ref }}
- name: Verify default PR Workflow
if: github.event_name == 'pull_request'
shell: bash
run: |
BASE_SHA=$(echo $(git merge-base origin/${{github.base_ref}} HEAD))
HEAD_SHA=$(git rev-parse HEAD)
node -e "if(process.env.NX_BASE == '${BASE_SHA}') console.log('Base set correctly'); else { throw new Error('Base not set correctly!');}"
node -e "if(process.env.NX_HEAD == '${HEAD_SHA}') console.log('Head set correctly'); else { throw new Error('Head not set correctly!');}"
- name: Test default Push Workflow
if: github.event_name != 'pull_request'
uses: ./
with:
main-branch-name: ${{ github.ref_name }}
- name: Verify default Push Workflow
if: github.event_name != 'pull_request'
shell: bash
run: |
BASE_SHA=$(echo $(git rev-parse HEAD~1))
HEAD_SHA=$(echo $(git rev-parse HEAD))
node -e "if(process.env.NX_BASE == '${BASE_SHA}') console.log('Base set correctly'); else { throw new Error('Base not set correctly!');}"
node -e "if(process.env.NX_HEAD == '${HEAD_SHA}') console.log('Head set correctly'); else { throw new Error('Head not set correctly!');}"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ width="100%" alt="Nx - Smart, Extensible Build Framework"></p>
- [Background](#background)
- [License](#license)

**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](https://github.com/nrwl/nx-set-shas/blob/v1/README.md).
**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](https://github.com/nrwl/nx-set-shas/blob/v1/README.md).

## Example Usage

Expand All @@ -37,7 +37,7 @@ jobs:
# OPTION 1) Environment variables
# ===========================================================================
- name: Derive appropriate SHAs for base and head for `nx affected` commands
uses: nrwl/nx-set-shas@v2
uses: nrwl/nx-set-shas@v3

- run: |
echo "BASE: ${{ env.NX_BASE }}"
Expand All @@ -48,7 +48,7 @@ jobs:
# ===========================================================================
- name: Derive appropriate SHAs for base and head for `nx affected` commands
id: setSHAs
uses: nrwl/nx-set-shas@v2
uses: nrwl/nx-set-shas@v3

- run: |
echo "BASE: ${{ steps.setSHAs.outputs.base }}"
Expand All @@ -62,7 +62,7 @@ jobs:

<!-- start configuration-options -->
```yaml
- uses: nrwl/nx-set-shas@v2
- 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.
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
```
<!-- end configuration-options -->
## Permissions in v2
## 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`:

Expand Down
Loading

0 comments on commit 8b116da

Please sign in to comment.