chore(repo): switch to using nx orb on circleci workflows (#6505)

* chore(repo): switch to using nx orb on circleci workflows

* chore(repo): set proper name of the main branch
This commit is contained in:
Miroslav Jonaš 2021-07-26 15:11:17 +02:00 committed by GitHub
parent 9e8252406c
commit 1c3397f033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 185 deletions

View File

@ -11,6 +11,9 @@ dependencies:
cache_directories: cache_directories:
- ~/.cache/yarn - ~/.cache/yarn
orbs:
nx: nrwl/nx@1.0.0
executors: executors:
linux: linux:
working_directory: *working_directory working_directory: *working_directory
@ -126,27 +129,24 @@ jobs:
- run: - run:
name: Check Package dependencies name: Check Package dependencies
command: yarn depcheck command: yarn depcheck
- run: - nx/set-shas:
name: Get Last Sucessful Workflow Run main-branch-name: 'master'
command: |
source ./scripts/circleci/get-successful-run.sh << pipeline.project.type >> $CIRCLE_PROJECT_USERNAME $CIRCLE_PROJECT_REPONAME $CIRCLE_BRANCH master false
echo "export BASE_SHA=\"$BASE_SHA\";" >> $BASH_ENV
- run: - run:
name: Run Builds name: Run Builds
command: | command: |
npx nx affected --target=build --base=$BASE_SHA --parallel --max-parallel=3 npx nx affected --target=build --base=$NX_BASE --parallel --max-parallel=3
- run: - run:
name: Run Unit Tests name: Run Unit Tests
command: | command: |
npx nx affected --target=test --base=$BASE_SHA --parallel --max-parallel=2 npx nx affected --target=test --base=$NX_BASE --parallel --max-parallel=2
- run: - run:
name: Run Linting name: Run Linting
command: | command: |
npx nx affected --target=lint --base=$BASE_SHA --parallel --max-parallel=4 npx nx affected --target=lint --base=$NX_BASE --parallel --max-parallel=4
- run: - run:
name: Run E2E Tests name: Run E2E Tests
command: | command: |
npx nx affected --target=e2e --base=$BASE_SHA npx nx affected --target=e2e --base=$NX_BASE
no_output_timeout: 45m no_output_timeout: 45m
- run: - run:
name: Stop All Running Agents for This CI Run name: Stop All Running Agents for This CI Run
@ -171,23 +171,20 @@ jobs:
steps: steps:
- setup: - setup:
os: << parameters.os >> os: << parameters.os >>
- run: - nx/set-shas:
name: Get Last Sucessful Workflow Run main-branch-name: 'master'
command: |
source ./scripts/circleci/get-successful-run.sh << pipeline.project.type >> $CIRCLE_PROJECT_USERNAME $CIRCLE_PROJECT_REPONAME $CIRCLE_BRANCH master false
echo "export BASE_SHA=\"$BASE_SHA\";" >> $BASH_ENV
- run: - run:
name: Run Builds name: Run Builds
command: npx nx affected --target=build --base=$BASE_SHA --parallel --max-parallel=3 command: npx nx affected --target=build --base=$NX_BASE --parallel --max-parallel=3
- run: - run:
name: Run Unit Tests name: Run Unit Tests
command: npx nx affected --target=test --base=$BASE_SHA --parallel --max-parallel=2 command: npx nx affected --target=test --base=$NX_BASE --parallel --max-parallel=2
- run: - run:
name: Run Linting name: Run Linting
command: npx nx affected --target=lint --base=$BASE_SHA --parallel --max-parallel=4 command: npx nx affected --target=lint --base=$NX_BASE --parallel --max-parallel=4
- run: - run:
name: Run E2E Tests name: Run E2E Tests
command: npx nx affected --target=e2e --base=$BASE_SHA command: npx nx affected --target=e2e --base=$NX_BASE
no_output_timeout: 45m no_output_timeout: 45m
- run: - run:
name: Stop All Running Agents for This CI Run name: Stop All Running Agents for This CI Run

View File

@ -1,115 +0,0 @@
#!/usr/bin/env node
const { execSync } = require('child_process');
const https = require('https');
// first two argument params are node and script
const INPUTS_MAIN_BRANCH_NAME = process.argv[2];
const PROJECT_SLUG = process.argv[3];
const URL = `https://circleci.com/api/v2/project/${PROJECT_SLUG}/pipeline?branch=${INPUTS_MAIN_BRANCH_NAME}`;
/**
* Main
* Cycle through pipelines until first successful run is found
* or we reach the end of the pipeline history
*
* If found, log it to the parent process
*/
(async () => {
let nextPage;
let foundSHA;
do {
const { next_page_token, sha } = await findSha(nextPage);
foundSHA = sha;
nextPage = next_page_token;
} while (!foundSHA && nextPage);
if (foundSHA) {
// send it to parent process
process.stdout.write(foundSHA);
}
})();
/**
* Finds the last successful commit and/or token for the next page
* @param {string} pageToken
* @returns { next_page_token?: string, sha?: string }
*/
async function findSha(pageToken) {
return getJson(pageToken ? `${URL}&page-token=${pageToken}` : URL).then(
async ({ next_page_token, items }) => {
const pipeline = await findSuccessfulPipeline(items);
return {
next_page_token,
sha: pipeline ? pipeline.vcs.revision : void 0,
};
}
);
}
/**
* Get successful pipeline run if any
* @param {Object[]} pipelines
* @returns
*/
async function findSuccessfulPipeline(pipelines) {
for (const pipeline of pipelines) {
if (
!pipeline.errors.length &&
commitExists(pipeline.vcs.revision) &&
(await isWorkflowSuccessful(pipeline.id))
) {
return pipeline;
}
}
return undefined;
}
/**
* Check if given commit still exists
* @param {string} commitSha
* @returns
*/
function commitExists(commitSha) {
try {
execSync(`git cat-file -e ${commitSha} 2> /dev/null`);
return true;
} catch {
return false;
}
}
/**
* Check if every workflow in the pipeline is successful
* @param {string} pipelineId
* @returns {boolean}
*/
async function isWorkflowSuccessful(pipelineId) {
return getJson(
`https://circleci.com/api/v2/pipeline/${pipelineId}/workflow`
).then(({ items }) => items.every((item) => item.status === 'success'));
}
/**
* Helper function to wrap Https.get as an async call
* @param {string} url
* @returns {Promise<JSON>}
*/
async function getJson(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = [];
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = Buffer.concat(data).toString();
resolve(JSON.parse(response));
});
})
.on('error', (error) => reject(error));
});
}

View File

@ -1,52 +0,0 @@
#!/bin/bash
PROJECT_SLUG="$1/$2/$3"
BRANCH_NAME=$4
INPUTS_MAIN_BRANCH_NAME=$5
INPUTS_ERROR_ON_NO_SUCCESSFUL_WORKFLOW=$6
if [ "$BRANCH_NAME" != $INPUTS_MAIN_BRANCH_NAME ]; then
BASE_SHA=$(echo $(git merge-base origin/$INPUTS_MAIN_BRANCH_NAME HEAD))
echo ""
echo "Branch found. Using base from 'origin/$INPUTS_MAIN_BRANCH_NAME': $BASE_SHA"
echo ""
else
# We will make an https request to CircleCI API getting all the pipelines from the $INPUTS_MAIN_BRANCH_NAME on $PROJECT_SLUG
# For each pipeline we check if it was successful and whether the commit still exists
BASE_SHA=$(node scripts/circleci/find-successful-workflow.js $INPUTS_MAIN_BRANCH_NAME $PROJECT_SLUG )
if [ -z $BASE_SHA ]; then
if [ $INPUTS_ERROR_ON_NO_SUCCESSFUL_WORKFLOW = "true" ]; then
echo ""
echo "ERROR: Unable to find a successful workflow run on 'origin/$INPUTS_MAIN_BRANCH_NAME'"
echo ""
echo "NOTE: You have set 'error-on-no-successful-workflow' on the action so this is a hard error."
echo ""
echo "Is it possible that you have no runs currently on 'origin/$INPUTS_MAIN_BRANCH_NAME' in your repo?"
echo ""
echo "- If yes, then you should run the workflow without this flag first."
echo "- If no, then you might have changed your git history and those commits no longer exist."
echo ""
exit 1
else
echo ""
echo "WARNING: Unable to find a successful workflow run on 'origin/$INPUTS_MAIN_BRANCH_NAME'"
echo ""
echo "We are therefore defaulting to use HEAD~1 on 'origin/$INPUTS_MAIN_BRANCH_NAME'"
echo ""
echo "NOTE: You can instead make this a hard error by settting 'error-on-no-successful-workflow' on the action in your workflow."
echo ""
SHA="HEAD~1"
fi
else
echo ""
echo "Found the last successful workflow run on 'origin/$INPUTS_MAIN_BRANCH_NAME'"
echo ""
echo "Commit: $BASE_SHA"
echo ""
fi
fi
HEAD_SHA=$(git rev-parse HEAD)