Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Create github action for triggering Bitrise E2E builds based on label #7339

Merged
merged 86 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
0f72d3e
Create github action for triggering Bitrise E2E builds based on label
Cal-L Sep 26, 2023
30699ac
Remove -L
Cal-L Sep 26, 2023
89252de
Fix string
Cal-L Sep 26, 2023
9f0f615
Update format
Cal-L Sep 26, 2023
1acc611
Remove unrelated workflows
Cal-L Sep 26, 2023
571302d
Remove PR id
Cal-L Sep 26, 2023
a9fcef0
Add back PR ID
Cal-L Sep 26, 2023
748323a
Fix workflow
Cal-L Sep 26, 2023
3202a66
Simplify Bitrise command
Cal-L Sep 26, 2023
f4bd209
Remove extra comma
Cal-L Sep 26, 2023
e54d17e
Revert "Remove unrelated workflows"
Cal-L Oct 17, 2023
63c33ab
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
Cal-L Oct 17, 2023
07c1e7f
Comment out label triggered workflow
Cal-L Oct 17, 2023
2dcf209
Add Bitrise trigger map for commits
Cal-L Oct 17, 2023
f444328
Add extra space
Cal-L Oct 17, 2023
48c6cd8
Change trigger to push
Cal-L Oct 17, 2023
a2d0682
PR into feature branch
Cal-L Oct 17, 2023
fe16292
Remove trigger map
Cal-L Oct 17, 2023
4b89c04
Log values
Cal-L Oct 18, 2023
039d1d8
Change to main branch
Cal-L Oct 18, 2023
63c2650
Revert ios_e2e_test
Cal-L Oct 23, 2023
2fd06fa
Reduce logic for testing
Cal-L Oct 23, 2023
c7a2369
Create tag whenever label is applied
Cal-L Oct 24, 2023
864cb96
Kick off Bitrise build for PR label
Cal-L Oct 24, 2023
42b8a04
Rename context
Cal-L Oct 24, 2023
bde6989
Split out script
Cal-L Oct 24, 2023
1201f45
Add missing yarn command
Cal-L Oct 24, 2023
4ba4478
Install yarn
Cal-L Oct 24, 2023
df8a4b9
Install deps
Cal-L Oct 24, 2023
098b870
Fix RUN E2E label name
Cal-L Oct 24, 2023
5f7e4e9
Rename action
Cal-L Oct 24, 2023
623acf7
Fix label name
Cal-L Oct 24, 2023
c13ccb0
Fetch using public api
Cal-L Oct 24, 2023
8b26e4f
Add axios
Cal-L Oct 24, 2023
7a25d2c
Try creating a tag using github actions account
Cal-L Oct 24, 2023
1d895ea
Trigger Bitrise build in script
Cal-L Oct 24, 2023
f1eb0d2
Pass missing env vars
Cal-L Oct 24, 2023
7787f7f
Log build url
Cal-L Oct 24, 2023
ca6e07d
Log build id
Cal-L Oct 24, 2023
a051705
Post comment
Cal-L Oct 24, 2023
2552de4
Allow write permissions
Cal-L Oct 25, 2023
7724400
Try to update PR status
Cal-L Oct 25, 2023
f71778c
Readd token
Cal-L Oct 25, 2023
2e3fa57
Add write status permissions
Cal-L Oct 25, 2023
e1d590b
Just post comment for now
Cal-L Oct 25, 2023
949942d
Change workflow names
Cal-L Oct 25, 2023
f6de803
Add logs
Cal-L Oct 25, 2023
78b87c2
Provide PR link in logs
Cal-L Oct 25, 2023
52ab76f
Re-enable workflows
Cal-L Oct 25, 2023
382c913
Provide Bitrise with commit message
Cal-L Oct 25, 2023
c82d20a
Fix formatting
Cal-L Oct 25, 2023
9ba8f7d
Fix commit key
Cal-L Oct 25, 2023
cd1b0ea
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
Cal-L Oct 25, 2023
99ac0f9
Undo workflow changes
Cal-L Oct 25, 2023
ba206a3
Remove unused code
Cal-L Oct 25, 2023
ffe0862
Remove more unused code
Cal-L Oct 25, 2023
29db28d
Check for label first
Cal-L Oct 25, 2023
907ae44
Test fail scenario
Cal-L Oct 25, 2023
ec03cc2
Fix comments error
Cal-L Oct 25, 2023
3c29a4d
Conditionally run run-bitrise-e2e job
Cal-L Oct 25, 2023
50a4f6f
Fix reference error
Cal-L Oct 25, 2023
b2a32b9
Check boolean
Cal-L Oct 25, 2023
a856f0e
Set output of label step
Cal-L Oct 25, 2023
3d31699
Try moving things around
Cal-L Oct 25, 2023
abba06d
Create env vars
Cal-L Oct 25, 2023
c79e28e
Improve logs
Cal-L Oct 25, 2023
17dd38d
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
Cal-L Oct 25, 2023
d12e5e2
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
Cal-L Oct 25, 2023
1f0a6d8
Format run-bitrise-e2e
Cal-L Oct 26, 2023
5abaf37
Create add-e2e-label workflow for opening and
Cal-L Oct 26, 2023
a1eeb45
Fix trigger name
Cal-L Oct 26, 2023
3636a1a
No need to remove label that doesn't exist
Cal-L Oct 26, 2023
b4d22d7
Type Error
Cal-L Oct 26, 2023
9697de6
Merge triggers since API added labels do not seem to trigger trigger
Cal-L Oct 26, 2023
84db312
Fix typo
Cal-L Oct 26, 2023
58d7f37
Rename files
Cal-L Oct 26, 2023
ecbe017
Conditionally run the last step
Cal-L Oct 26, 2023
71f694d
Fix path
Cal-L Oct 26, 2023
e75b0d9
Fix typo
Cal-L Oct 26, 2023
4feb455
Simplify bitrise run step
Cal-L Oct 26, 2023
ed26bf0
Add reopened trigger
Cal-L Oct 26, 2023
e4659ac
Handle reopened scenario
Cal-L Oct 26, 2023
bd24991
Unlock comment
Cal-L Oct 26, 2023
fe0bb10
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
Cal-L Oct 26, 2023
34fa593
Add instructions to re-build Bitrise E2E smoke tests
Cal-L Oct 26, 2023
59409c1
Update workflow name
Cal-L Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .github/scripts/check-bitrise-e2e-smoke/check-e2e-smoke-trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';

enum PullRequestTriggerType {
Opened = 'opened',
Reopened = 'reopened',
ReadyForReview = 'ready_for_review',
Labeled = 'labeled',
}

main().catch((error: Error): void => {
console.error(error);
process.exit(1);
});

async function main(): Promise<void> {
const githubToken = process.env.GITHUB_TOKEN;
const e2eLabel = process.env.E2E_LABEL;
const triggerAction = context.payload.action as PullRequestTriggerType;
const pullRequestLink = `https://github.com/MetaMask/metamask-mobile/pull/${context.issue.number}`;
let shouldTriggerE2E = false;

if (!githubToken) {
core.setFailed('GITHUB_TOKEN not found');
process.exit(1);
}

if (!e2eLabel) {
core.setFailed('E2E_LABEL not found');
process.exit(1);
}

console.log(`Workflow triggered by the event (${triggerAction})`);

switch (triggerAction) {
case PullRequestTriggerType.Opened:
shouldTriggerE2E = !context.payload.pull_request?.draft;
if (!shouldTriggerE2E) {
console.log(`Skipping E2E smoke since opened PR is in draft.`);
} else {
console.log(`Starting E2E smoke since opened PR is ready for review.`);
}
break;
case PullRequestTriggerType.Reopened:
shouldTriggerE2E = !context.payload.pull_request?.draft;
if (!shouldTriggerE2E) {
console.log(`Skipping E2E smoke since reopened PR is in draft.`);
} else {
console.log(`Starting E2E smoke since reopened PR is ready for review.`);
}
break;
case PullRequestTriggerType.ReadyForReview:
shouldTriggerE2E = true;
console.log(
`Starting E2E smoke since PR has changed to ready for review.`,
);
break;
case PullRequestTriggerType.Labeled:
shouldTriggerE2E = e2eLabel === context.payload.label.name;
if (!shouldTriggerE2E) {
console.log(
`Skipping E2E smoke since (${e2eLabel}) is not the label that triggered this workflow.`,
);
} else {
console.log(
`Starting E2E smoke since (${e2eLabel}) is the label that triggered this workflow.`,
);
}
break;
}

if (shouldTriggerE2E) {
// Apply the E2E smoke label
const { owner, repo, number: issue_number } = context.issue;
const octokit: InstanceType<typeof GitHub> = getOctokit(githubToken);
try {
const applyLabelResponse = await octokit.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [e2eLabel],
});
if (applyLabelResponse.status === 200) {
console.log(`Applied (${e2eLabel}) label to PR ${pullRequestLink}`);
} else {
core.setFailed(
`Failed to apply (${e2eLabel}) label to ${pullRequestLink}`,
);
process.exit(1);
}
} catch (error) {
console.log(`Error occured when applying label: ${error}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this to setFailed instead

process.exit(1);
}
}

// Set the output for the next step to use.
core.setOutput("shouldTriggerE2E", shouldTriggerE2E);
}
108 changes: 108 additions & 0 deletions .github/scripts/check-bitrise-e2e-smoke/run-bitrise-e2e-smoke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';
import axios from 'axios';

main().catch((error: Error): void => {
console.error(error);
process.exit(1);
});

async function main(): Promise<void> {
const e2eLabel = process.env.E2E_LABEL;
const githubToken = process.env.GITHUB_TOKEN;
const e2ePipeline = process.env.E2E_PIPELINE;
const workflowName = process.env.WORKFLOW_NAME;
const pullRequestNumber = context.issue.number;
const repoOwner = context.repo.owner;
const repo = context.repo.repo;
const pullRequestLink = `https://github.com/MetaMask/metamask-mobile/pull/${pullRequestNumber}`;

if (!githubToken) {
core.setFailed('GITHUB_TOKEN not found');
process.exit(1);
}

if (!e2ePipeline) {
core.setFailed('E2E_PIPELINE not found');
process.exit(1);
}

const octokit: InstanceType<typeof GitHub> = getOctokit(githubToken);

const data = {
hook_info: {
type: 'bitrise',
build_trigger_token: process.env.BITRISE_BUILD_TRIGGER_TOKEN,
},
build_params: {
branch: process.env.GITHUB_HEAD_REF,
pipeline_id: e2ePipeline,
commit_message: `Triggered by (${workflowName}) workflow in ${pullRequestLink}`,
},
triggered_by: workflowName,
};

const bitriseProjectUrl = `https://app.bitrise.io/app/${process.env.BITRISE_APP_ID}`;

// Start Bitrise build.
const bitriseBuildResponse = await axios.post(
`${bitriseProjectUrl}/build/start.json`,
data,
{
headers: {
'Content-Type': 'application/json',
},
},
);

if (!bitriseBuildResponse.data.build_slug) {
core.setFailed(`Bitrise build slug not found`);
process.exit(1);
}

const buildLink = `${bitriseProjectUrl}/pipelines/${bitriseBuildResponse.data.build_slug}`;
const message = `E2E test started on Bitrise: ${buildLink}\nYou can also kick off another Bitrise E2E smoke test by removing and re-applying the (${e2eLabel}) label`;

if (bitriseBuildResponse.status === 201) {
console.log(message);
} else {
core.setFailed(
`Bitrise build request returned with status code ${bitriseBuildResponse.status}`,
);
process.exit(1);
}

// Reopen conversation in case it's locked
const unlockConvoResponse = await octokit.rest.issues.unlock({
owner: repoOwner,
repo,
issue_number: pullRequestNumber,
});

if (unlockConvoResponse.status === 204) {
console.log(`Unlocked conversation for PR ${pullRequestLink}`);
} else {
core.setFailed(
`Unlock conversation request returned with status code ${unlockConvoResponse.status}`,
);
process.exit(1);
}

// Post build link in PR comments.
const postCommentResponse = await octokit.rest.issues.createComment({
owner: repoOwner,
repo,
issue_number: pullRequestNumber,
body: message,
});

if (postCommentResponse.status === 201) {
console.log(`Posting comment in pull request ${pullRequestLink}`);
} else {
core.setFailed(
`Post comment request returned with status code ${postCommentResponse.status}`,
);
process.exit(1);
}
}
46 changes: 46 additions & 0 deletions .github/workflows/check-bitrise-e2e-smoke.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Check Bitrise E2E Smoke Tests

on:
pull_request:
types: [opened, reopened, ready_for_review, labeled]

env:
E2E_LABEL: 'Run Smoke E2E'
E2E_PIPELINE: 'pr_smoke_e2e_pipeline'
WORKFLOW_NAME: 'check-bitrise-e2e-smoke'

jobs:
check-bitrise-e2e-smoke:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: yarn

- name: Install Axios
run: yarn add axios

- name: Install dependencies
run: yarn --immutable

- name: Check E2E trigger
id: check-trigger
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: yarn run check-e2e-smoke-trigger

- name: Run Bitrise E2E
if: steps.check-trigger.outputs.shouldTriggerE2E == 'true'
env:
BITRISE_BUILD_TRIGGER_TOKEN: ${{ secrets.BITRISE_BUILD_TRIGGER_TOKEN }}
BITRISE_APP_ID: 'be69d4368ee7e86d'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: yarn run run-bitrise-e2e-smoke
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
"deduplicate": "yarn yarn-deduplicate",
"create-release": "./scripts/set-versions.sh && yarn update-changelog",
"add-release-label-to-pr-and-linked-issues": "ts-node ./.github/scripts/add-release-label-to-pr-and-linked-issues.ts",
"check-e2e-smoke-trigger": "ts-node ./.github/scripts/check-bitrise-e2e-smoke/check-e2e-smoke-trigger.ts",
"run-bitrise-e2e-smoke": "ts-node ./.github/scripts/check-bitrise-e2e-smoke/run-bitrise-e2e-smoke.ts",
"check-pr-has-required-labels": "ts-node ./.github/scripts/check-pr-has-required-labels.ts",
"close-release-bug-report-issue": "ts-node ./.github/scripts/close-release-bug-report-issue.ts",
"check-template-and-add-labels": "ts-node ./.github/scripts/check-template-and-add-labels.ts",
Expand Down
Loading