Skip to content

Commit

Permalink
cascade skip release [skip release]
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBoll committed May 23, 2023
1 parent b31e412 commit 021ffca
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 134 deletions.
278 changes: 145 additions & 133 deletions .github/workflows/forward-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- prerelease/minor

jobs:
test-ff-only:
get-branch-names:
# Only run if:
# - commit contains '[skip release]' (release commits or commits that skipped a release)
# - branch is prerelease/minor
Expand Down Expand Up @@ -49,160 +49,172 @@ jobs:
run: |
echo ${{steps.extract-next-branch.outputs.branch}}
## First, we'll checkout the repository. We don't persist credentials because we need a
## Personal Access Token to push on a branch that is protected. See
## https://github.com/cycjimmy/semantic-release-action#basic-usage
- uses: actions/checkout@v3
with:
persist-credentials: false
ref: ${{steps.extract-next-branch.outputs.branch}} # checkout the next branch
fetch-depth: 0 # Needed to do merges

## Attempt to do a fast-forward-only merge. If this succeeds, there is no divergence
## between the branches and we do not need to retest. The commit has already been
## verified. If this line fails, it will trigger `verify-merge`
- name: Test ff-only merge
run: git merge origin/${{ steps.extract-branch.outputs.branch }} --ff-only

## If the previous step passed, push the verified commit directly to the next branch
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_RW_TOKEN }}
branch: refs/heads/${{ steps.extract-next-branch.outputs.branch }}
test-ff-only:
needs: ['get-branch-names']
runs-on: ubuntu-latest

steps:

- run: exit 1
# ## First, we'll checkout the repository. We don't persist credentials because we need a
# ## Personal Access Token to push on a branch that is protected. See
# ## https://github.com/cycjimmy/semantic-release-action#basic-usage
# - uses: actions/checkout@v3
# with:
# persist-credentials: false
# ref: ${{needs.get-branch-names.outputs.next-branch}} # checkout the next branch
# fetch-depth: 0 # Needed to do merges

# ## Attempt to do a fast-forward-only merge. If this succeeds, there is no divergence
# ## between the branches and we do not need to retest. The commit has already been
# ## verified. If this line fails, it will trigger `verify-merge`
# - name: Test ff-only merge
# run: git merge origin/${{ needs.get-branch-names.outputs.branch }} --ff-only

# ## If the previous step passed, push the verified commit directly to the next branch
# - name: Push changes
# uses: ad-m/github-push-action@master
# with:
# github_token: ${{ secrets.GH_RW_TOKEN }}
# branch: refs/heads/${{ needs.get-branch-names.outputs.next-branch }}

## If the previous step failed, it means the fast-forward attempt failed. There is a
## divergence and we will need to merge the branches and verify everything works.
verify-merge:
runs-on: ubuntu-latest
if: failure()
needs: ['test-ff-only']
steps:
## First, we'll checkout the repository. We don't persist credentials because we need a
## Personal Access Token to push on a branch that is protected. See
## https://github.com/cycjimmy/semantic-release-action#basic-usage
- uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0 # Needed to do merges
needs: ['get-branch-names', 'test-ff-only']

- uses: Workday/canvas-kit-actions/install@v1
with:
node_version: 16.x
steps:
- run: exit 0
# ## First, we'll checkout the repository. We don't persist credentials because we need a
# ## Personal Access Token to push on a branch that is protected. See
# ## https://github.com/cycjimmy/semantic-release-action#basic-usage
# - uses: actions/checkout@v3
# with:
# persist-credentials: false
# fetch-depth: 0 # Needed to do merges

## A `yarn bump` will create a commit and a tag. We need to set up the git user to do this.
## We'll make that user be the github-actions user.
- name: Config git user
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config --global pull.rebase false
# - uses: Workday/canvas-kit-actions/install@v1
# with:
# node_version: 16.x

## Create a merge branch
- name: Forward merge
run: node utils/forward-merge.js
# ## A `yarn bump` will create a commit and a tag. We need to set up the git user to do this.
# ## We'll make that user be the github-actions user.
# - name: Config git user
# run: |
# git config --global user.name "${{ github.actor }}"
# git config --global user.email "${{ github.actor }}@users.noreply.github.com"
# git config --global pull.rebase false

- name: Git Log
run: git log
# ## Create a merge branch
# - name: Forward merge
# run: node utils/forward-merge.js

# Keep steps separate for Github Actions annotation matching: https://github.com/actions/setup-node/blob/83c9f7a7df54d6b57455f7c57ac414f2ae5fb8de/src/setup-node.ts#L26-L33
- name: Lint
run: yarn lint
# - name: Git Log
# run: git log

- name: Dependency Check
run: yarn depcheck
# # Keep steps separate for Github Actions annotation matching: https://github.com/actions/setup-node/blob/83c9f7a7df54d6b57455f7c57ac414f2ae5fb8de/src/setup-node.ts#L26-L33
# - name: Lint
# run: yarn lint

- name: Type Check
run: yarn typecheck
# - name: Dependency Check
# run: yarn depcheck

- name: Unit tests
run: yarn test
# - name: Type Check
# run: yarn typecheck

- name: Build Storybook
run: yarn build-storybook --quiet
# - name: Unit tests
# run: yarn test

- name: Cache Build
id: build-cache
uses: actions/cache/@v2
with:
path: docs
key: ${{ runner.os }}-build-${{ github.sha }}
# - name: Build Storybook
# run: yarn build-storybook --quiet

# Not working for now...
# - name: Visual Tests
# uses: chromaui/action@main
# - name: Cache Build
# id: build-cache
# uses: actions/cache/@v2
# with:
# token: ${{ secrets.GITHUB_TOKEN }}
# appCode: dlpro96xybh
# storybookBuildDir: docs
# exitOnceUploaded: false
# exitZeroOnChanges: false
# ignoreLastBuildOnBranch: ${{ needs.test-ff-only.outputs.next-branch }}
# debug: true

- name: Start Server
run: npx http-server docs -p 9001 & npx wait-on http://localhost:9001

- name: Integration tests
run: yarn cypress run --record --parallel --env skip_storybook_test=true # skip the Storybook test during forward merges
env:
# Github Actions doesn't support encryption on forks
# If these keys become compromised, we will rotate and disable these features
# on forked PRs until a suitable workaround is found
CYPRESS_RECORD_KEY: 3a9347b6-36ab-4a36-823d-709f4078b148
CYPRESS_CACHE_FOLDER: .cache/cypress

## Push both the commit and tag created by Lerna's version command using a PAT
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_RW_TOKEN }}
branch: refs/heads/${{ needs.test-ff-only.outputs.next-branch }}
# path: docs
# key: ${{ runner.os }}-build-${{ github.sha }}

# # Not working for now...
# # - name: Visual Tests
# # uses: chromaui/action@main
# # with:
# # token: ${{ secrets.GITHUB_TOKEN }}
# # appCode: dlpro96xybh
# # storybookBuildDir: docs
# # exitOnceUploaded: false
# # exitZeroOnChanges: false
# # ignoreLastBuildOnBranch: ${{ needs.get-branch-names.outputs.next-branch }}
# # debug: true

# - name: Start Server
# run: npx http-server docs -p 9001 & npx wait-on http://localhost:9001

# - name: Integration tests
# run: yarn cypress run --record --parallel --env skip_storybook_test=true # skip the Storybook test during forward merges
# env:
# # Github Actions doesn't support encryption on forks
# # If these keys become compromised, we will rotate and disable these features
# # on forked PRs until a suitable workaround is found
# CYPRESS_RECORD_KEY: 3a9347b6-36ab-4a36-823d-709f4078b148
# CYPRESS_CACHE_FOLDER: .cache/cypress

# ## Push both the commit and tag created by Lerna's version command using a PAT
# - name: Push changes
# uses: ad-m/github-push-action@master
# with:
# github_token: ${{ secrets.GH_RW_TOKEN }}
# branch: refs/heads/${{ needs.get-branch-names.outputs.next-branch }}

## If we get here, it means the branches are not fast-forward mergeable, OR the merge commit
## failed verification. We will need manual intervention, so we'll create a pull request to
## be verified by a person.
make-pull-request:
runs-on: ubuntu-latest
if: failure()
needs: ['test-ff-only', 'verify-merge']
# Run only if the verify-merge job failed. If the test-ff-only fails, but verify-merge passes, we should skip
if: needs.verify-merge.result == 'failure'
needs: ['get-branch-names', 'verify-merge']
steps:
## If we've failed any previous step, we'll need to create a PR instead
- uses: NicholasBoll/action-forward-merge-pr@main
with:
token: ${{secrets.GH_RW_TOKEN}} # use PAT to force GH Actions to run the PR verify. The regular token will not
branches: support+master,master+prerelease/minor,prerelease/minor+prerelease/major
prefix: 'chore: '
body: |
This pull request was automatically created by an automated [forward-merge job](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}). The automated job failed automated checks and must be resolved manually.
Reasons for failure may include:
- Merge conflicts that cannot be automatically resolved
- A merge resulted in check failures
- Lint or type errors
- Test failures
- Unexpected visual changes
The pull request should inform you of merge conflicts before you start if any.
1. Run the following commands in your terminal. If this succeeds, skip step 2. The last command will run a script that tries to merge and resolve conflicts automatically.
```
git branch -D merge/${{needs.test-ff-only.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }} || true
git fetch upstream
git checkout merge/${{needs.test-ff-only.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }}
git pull upstream merge/${{needs.test-ff-only.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }} -f
node utils/forward-merge.js
```
2. If the previous step succeeded, skip to step 3. Resolve conflicts manually. Then run the following.
```
git add .
git commit -m "chore: Merge ${{needs.test-ff-only.outputs.branch}} into ${{ needs.test-ff-only.outputs.next-branch }}"
```
3. Push the merge commit back to the pull request
```
git push upstream merge/${{needs.test-ff-only.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }}
```
If there were no merge conflicts, the forward-merge job failed because of a test failure. You can wait for the pull request to give errors, or you can check the logs for failures. You'll have to update code to fix errors.
This pull request will be merged using the `merge` strategy instead of the `squash` strategy. This means any commit in the log will show in the branch's history. Any commit you make should amend the merge commit. Use the following command:
```
git commit --amend --no-edit
```
You must then force-push the branch and the CI will rerun verification.
Use the `automerge` label like normal and the CI will pick the correct merge strategy.
- name: Echo
run: echo "${{needs.get-branch-names.outputs.branch}}"
# ## If we've failed any previous step, we'll need to create a PR instead
# - uses: NicholasBoll/action-forward-merge-pr@main
# with:
# token: ${{secrets.GH_RW_TOKEN}} # use PAT to force GH Actions to run the PR verify. The regular token will not
# branches: support+master,master+prerelease/minor,prerelease/minor+prerelease/major
# prefix: 'chore: '
# body: |
# This pull request was automatically created by an automated [forward-merge job](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}). The automated job failed automated checks and must be resolved manually.
# Reasons for failure may include:
# - Merge conflicts that cannot be automatically resolved
# - A merge resulted in check failures
# - Lint or type errors
# - Test failures
# - Unexpected visual changes
# The pull request should inform you of merge conflicts before you start if any.
# 1. Run the following commands in your terminal. If this succeeds, skip step 2. The last command will run a script that tries to merge and resolve conflicts automatically.
# ```
# git branch -D merge/${{needs.get-branch-names.outputs.branch}}-into-${{ needs.get-branch-names.outputs.next-branch }} || true
# git fetch upstream
# git checkout merge/${{needs.get-branch-names.outputs.branch}}-into-${{ needs.get-branch-names.outputs.next-branch }}
# git pull upstream merge/${{needs.get-branch-names.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }} -f
# node utils/forward-merge.js
# ```
# 2. If the previous step succeeded, skip to step 3. Resolve conflicts manually. Then run the following.
# ```
# git add .
# git commit -m "chore: Merge ${{needs.test-ff-only.outputs.branch}} into ${{ needs.test-ff-only.outputs.next-branch }}"
# ```
# 3. Push the merge commit back to the pull request
# ```
# git push upstream merge/${{needs.test-ff-only.outputs.branch}}-into-${{ needs.test-ff-only.outputs.next-branch }}
# ```
# If there were no merge conflicts, the forward-merge job failed because of a test failure. You can wait for the pull request to give errors, or you can check the logs for failures. You'll have to update code to fix errors.
# This pull request will be merged using the `merge` strategy instead of the `squash` strategy. This means any commit in the log will show in the branch's history. Any commit you make should amend the merge commit. Use the following command:
# ```
# git commit --amend --no-edit
# ```
# You must then force-push the branch and the CI will rerun verification.
# Use the `automerge` label like normal and the CI will pick the correct merge strategy.
9 changes: 8 additions & 1 deletion utils/forward-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ async function main() {
await spawn(`git checkout -b merge/${branch}-into-${nextBranch}`);
}

// get last commit message. If it was a skip release, we'll try to make the merge commit a skip
// release as well.
const {stdout: commitMessage} = await exec(`git log -1 --pretty=%B | cat`);
const isSkipRelease = commitMessage.includes('[skip release]');

try {
console.log(`Creating a merge branch`);
// The CI uses `origin` while locally we use `upstream`.
const remote = alreadyMerging ? 'upstream' : 'origin';
const {stdout} = await exec(
`git merge ${remote}/${nextBranch} -m 'chore: Merge ${branch} into ${nextBranch}'`
`git merge ${remote}/${nextBranch} -m 'chore: Merge ${branch} into ${nextBranch}${
isSkipRelease ? ' [skip release]' : '' // continue the skip release if applicable
}'`
);
// exec doesn't automatically log
console.log(stdout);
Expand Down

0 comments on commit 021ffca

Please sign in to comment.