diff --git a/.circleci/test-deploy.yml b/.circleci/test-deploy.yml index 0d7ba95..8d6dd47 100644 --- a/.circleci/test-deploy.yml +++ b/.circleci/test-deploy.yml @@ -17,18 +17,18 @@ jobs: shell: "/bin/bash" command: | HEAD_SHA=$(git rev-parse HEAD) - if [[ -z "$CIRCLE_PULL_REQUEST" ]]; then - BASE_SHA=$(git merge-base origin/main HEAD) - else + if [[ $CIRCLE_BRANCH == "main" ]]; then BASE_SHA=$(git rev-parse origin/main~1) + else + BASE_SHA=$(git merge-base origin/main HEAD) fi echo "Comparing head: $NX_HEAD to $HEAD_SHA" echo "Comparing base: $NX_BASE to $BASE_SHA" if [[ $NX_HEAD == $HEAD_SHA && $NX_BASE == $BASE_SHA ]]; then echo "Test conditions met" else - echo "Exiting Job" - circleci-agent step halt + echo "Test conditions NOT met" + exit 5 fi workflows: diff --git a/CHANGELOG.md b/CHANGELOG.md index 97c300c..bde9c41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## [1.6.0] - 2022-08-09 - **[fix]**: Use explicitly main branch's previous commit when last successful sha was not found - **[fix]**: Use CircleCI internal env var to check if run is pull request - - **[feat]**: Optionally disable filtering the past workflow runs by branch + - **[feat]**: Optionally disable filtering the past workflow runs by branch ## [1.5.1] - 2022-06-27 - **[fix]**: Build url regex broken for pipeline ids < 10 diff --git a/README.md b/README.md index c2cd992..0cd8f5a 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ width="100%" alt="Nx - Smart, Extensible Build Framework">

version: 2.1 orbs: - nx: nrwl/nx@1.5.1 + nx: nrwl/nx@1.6.0 jobs: checks: diff --git a/src/commands/set-shas.yml b/src/commands/set-shas.yml index f82efe4..ce2ae0a 100755 --- a/src/commands/set-shas.yml +++ b/src/commands/set-shas.yml @@ -25,6 +25,12 @@ parameters: description: | By default, only workflows with CircleCI status of "success" will be detected for the main branch. Enable this option to also detect workflows with CircleCI status of "on_hold" in case your workflow requires manual approvals. + skip-branch-filter: + type: boolean + default: false + description: | + By default, the workflow runs will be filtered by `main` branch. This works fine with standard `push` event. If you + want to use the orb for non-push events (e.g. tag, label etc.) you need to disable branch filtering. steps: - run: @@ -33,6 +39,7 @@ steps: PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW: <> PARAM_WORKFLOW_NAME: <> PARAM_ALLOW_ON_HOLD: <> + PARAM_SKIP_BRANCH_FILTER: <> PARAM_SCRIPT: <> name: Derives SHAs for base and head for use in `nx affected` commands shell: "/bin/bash" diff --git a/src/examples/custom.yml b/src/examples/custom.yml index ad9cbb8..b7bc36e 100644 --- a/src/examples/custom.yml +++ b/src/examples/custom.yml @@ -8,7 +8,7 @@ description: > usage: version: 2.1 orbs: - nx: nrwl/nx@1.5.0 + nx: nrwl/nx@1.6.0 jobs: build: docker: diff --git a/src/examples/default.yml b/src/examples/default.yml index 89fcc46..372f7d6 100755 --- a/src/examples/default.yml +++ b/src/examples/default.yml @@ -3,7 +3,7 @@ description: > usage: version: 2.1 orbs: - nx: nrwl/nx@1.5.0 + nx: nrwl/nx@1.6.0 jobs: build: docker: diff --git a/src/examples/private.yml b/src/examples/private.yml index 6df1cf6..446bda7 100644 --- a/src/examples/private.yml +++ b/src/examples/private.yml @@ -7,4 +7,4 @@ description: > usage: version: 2.1 orbs: - nx: nrwl/nx@1.5.0 + nx: nrwl/nx@1.6.0 diff --git a/src/examples/tags.yml b/src/examples/tags.yml new file mode 100755 index 0000000..4813bd7 --- /dev/null +++ b/src/examples/tags.yml @@ -0,0 +1,32 @@ +description: > + You can use `set-shas` also for non-push events, but you need to skip the branch check in that case. +usage: + version: 2.1 + orbs: + nx: nrwl/nx@1.6.0 + jobs: + build: + docker: + - image: cimg/node:14.17-browsers + steps: + - checkout + - run: + name: Install dependencies + command: yarn install --frozen-lockfile + - nx/set-shas: + skip-branch-filter: true + - run: + name: Run Builds + command: yarn nx affected --target=build --base=$NX_BASE --parallel --max-parallel=3 + - run: + name: Run Unit Tests + command: yarn nx affected --target=test --base=$NX_BASE --parallel --max-parallel=2 + workflows: + my-workflow: + jobs: + - build: + filters: + branches: + ignore: /.*/ # ignore any commit on any branch by default + tags: + only: /^v[0-9]+(\.[0-9]+)*$/ # only act on version tags diff --git a/src/scripts/find-successful-workflow.js b/src/scripts/find-successful-workflow.js index e0d526b..e21ff7d 100644 --- a/src/scripts/find-successful-workflow.js +++ b/src/scripts/find-successful-workflow.js @@ -7,7 +7,8 @@ const branchName = process.argv[3]; const mainBranchName = process.env.MAIN_BRANCH_NAME || process.argv[4]; const errorOnNoSuccessfulWorkflow = process.argv[5] === '1'; const allowOnHoldWorkflow = process.argv[6] === '1'; -const workflowName = process.argv[7]; +const skipBranchFilter = process.argv[7] === '1'; +const workflowName = process.argv[8]; const circleToken = process.env.CIRCLE_API_TOKEN; const [, host, project] = buildUrl.match(/https?:\/\/([^\/]+)\/(.*)\/\d+/); @@ -18,7 +19,7 @@ let BASE_SHA; BASE_SHA = execSync(`git merge-base origin/${mainBranchName} HEAD`, { encoding: 'utf-8' }); } else { try { - BASE_SHA = await findSuccessfulCommit(mainBranchName, workflowName); + BASE_SHA = await findSuccessfulCommit(skipBranchFilter ? undefined : mainBranchName, workflowName); } catch (e) { process.stderr.write(e.message); process.exit(1); @@ -48,17 +49,18 @@ Found the last successful workflow run on 'origin/${mainBranchName}'.\n\n`); } } - process.stdout.write(`Commit: ${BASE_SHA}\n`); + process.stdout.write(`Commit: ${BASE_SHA}\n\n`); })(); async function findSuccessfulCommit(branch, workflowName) { - const url = `https://${host}/api/v2/project/${project}/pipeline?branch=${branch}`; + const url = `https://${host}/api/v2/project/${project}/pipeline?`; + const params = branch ? [`branch=${branch}`] : []; let nextPage; let foundSHA; do { - const fullUrl = nextPage ? `${url}&page-token=${nextPage}` : url; - const { next_page_token, sha } = await getJson(fullUrl) + const fullParams = params.concat(nextPage ? [`page=${nextPage}`] : []).join('&'); + const { next_page_token, sha } = await getJson(`${url}${fullParams}`) .then(async ({ next_page_token, items }) => { const pipeline = await findSuccessfulPipeline(items, workflowName); return { diff --git a/src/scripts/set-shas.sh b/src/scripts/set-shas.sh index c16850c..221b1ad 100644 --- a/src/scripts/set-shas.sh +++ b/src/scripts/set-shas.sh @@ -1,6 +1,6 @@ #!/bin/bash echo "$PARAM_SCRIPT" >>"index.js" -RESPONSE=$(node index.js $CIRCLE_BUILD_URL $CIRCLE_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_WORKFLOW_NAME) +RESPONSE=$(node index.js $CIRCLE_BUILD_URL $CIRCLE_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_SKIP_BRANCH_FILTER $PARAM_WORKFLOW_NAME) echo "$RESPONSE" BASE_SHA=$(echo "$RESPONSE" | grep 'Commit:' | sed 's/.*Commit: //') HEAD_SHA=$(git rev-parse HEAD)