diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index e114acc0..dd4ca1de 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -270,3 +270,59 @@ jobs: with: github_token: ${{ secrets.GITHUB_TOKEN }} branches: integration-test-delete-prod + + # Creates two competing deployments, one of which should rebase over the other. + # First conflicting deployment + integration-rebase-conflicts-1: + needs: integration-branch-creation + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create random file + runs: echo $RANDOM > integration/1 + + - name: Build and Deploy + uses: JamesIves/github-pages-deploy-action@v4.2.5 + with: + git-config-name: Montezuma + git-config-email: montezuma@jamesiv.es + repository-name: MontezumaIves/lab + token: ${{ secrets.ACCESS_TOKEN }} + branch: gh-pages-rebase-conflict + folder: integration + force: false + # Second conflicting deployment + integration-rebase-conflicts-2: + needs: integration-branch-creation + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Create random file + runs: echo $RANDOM > integration/2 + + - name: Build and Deploy + uses: JamesIves/github-pages-deploy-action@v4.2.5 + with: + git-config-name: Montezuma + git-config-email: montezuma@jamesiv.es + repository-name: MontezumaIves/lab + token: ${{ secrets.ACCESS_TOKEN }} + branch: gh-pages-rebase-conflict + folder: integration + force: false + # Clean up conflicting deployments + integration-rebase-conflicts-cleanup: + needs: [integration-rebase-conflicts-1, integration-rebase-conflicts-2] + runs-on: ubuntu-latest + steps: + - name: Cleanup Generated Branch + uses: dawidd6/action-delete-branch@v3.1.0 + with: + github_token: ${{ secrets.ACCESS_TOKEN }} + owner: MontezumaIves + repository: lab + branches: gh-pages diff --git a/README.md b/README.md index be3a865a..0fc6786c 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,7 @@ By default, the action does not need any token configuration and uses the provid | `clean-exclude` | If you need to use `clean` but you'd like to preserve certain files or folders you can use this option. This should contain each pattern as a single line in a multiline string. | `with` | **No** | | `dry-run` | Do not actually push back, but use `--dry-run` on `git push` invocations instead. | `with` | **No** | | `single-commit` | This option can be toggled to `true` if you'd prefer to have a single commit on the deployment branch instead of maintaining the full history. **Using this option will also cause any existing history to be wiped from the deployment branch**. | `with` | **No** | +| `force` | Force-push new deployments to overwrite the previous version; otherwise, attempt to rebase new deployments onto any existing ones. This option is turned on by default and can be toggled off by setting it to `false`, which may be useful if there are multiple deployments in a single branch. | `with` | **No** | | `silent` | Silences the action output preventing it from displaying git messages. | `with` | **No** | | `workspace` | This should point to where your project lives on the virtual machine. The GitHub Actions environment will set this for you. It is only necessary to set this variable if you're using the node module. | `with` | **No** | diff --git a/__tests__/execute.test.ts b/__tests__/execute.test.ts index 14831ba8..9216eb19 100644 --- a/__tests__/execute.test.ts +++ b/__tests__/execute.test.ts @@ -13,8 +13,10 @@ describe('execute', () => { expect(exec).toBeCalledWith('echo Montezuma', [], { cwd: './', silent: true, + ignoreReturnCode: false, listeners: { - stdout: expect.any(Function) + stdout: expect.any(Function), + stderr: expect.any(Function) } }) }) @@ -28,8 +30,10 @@ describe('execute', () => { expect(exec).toBeCalledWith('echo Montezuma', [], { cwd: './', silent: false, + ignoreReturnCode: false, listeners: { - stdout: expect.any(Function) + stdout: expect.any(Function), + stderr: expect.any(Function) } }) }) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 366da22b..cc10b236 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -30,7 +30,7 @@ jest.mock('@actions/io', () => ({ jest.mock('../src/execute', () => ({ // eslint-disable-next-line @typescript-eslint/naming-convention __esModule: true, - execute: jest.fn() + execute: jest.fn(() => ({stdout: '', stderr: ''})) })) describe('git', () => { diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 0ec3a9c0..758e1115 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -16,7 +16,7 @@ import {setFailed, exportVariable} from '@actions/core' const originalAction = JSON.stringify(action) jest.mock('../src/execute', () => ({ - execute: jest.fn() + execute: jest.fn(() => ({stdout: '', stderr: ''})) })) jest.mock('@actions/io', () => ({ diff --git a/__tests__/ssh.test.ts b/__tests__/ssh.test.ts index 39481506..602e12d1 100644 --- a/__tests__/ssh.test.ts +++ b/__tests__/ssh.test.ts @@ -33,7 +33,7 @@ jest.mock('@actions/core', () => ({ })) jest.mock('../src/execute', () => ({ - execute: jest.fn() + execute: jest.fn(() => ({stdout: '', stderr: ''})) })) describe('configureSSH', () => { diff --git a/__tests__/worktree.error.test.ts b/__tests__/worktree.error.test.ts index 73f91410..5fdff2c9 100644 --- a/__tests__/worktree.error.test.ts +++ b/__tests__/worktree.error.test.ts @@ -5,7 +5,7 @@ import {generateWorktree} from '../src/worktree' jest.mock('../src/execute', () => ({ // eslint-disable-next-line @typescript-eslint/naming-convention __esModule: true, - execute: jest.fn() + execute: jest.fn(() => ({stdout: '', stderr: ''})) })) describe('generateWorktree', () => { diff --git a/__tests__/worktree.test.ts b/__tests__/worktree.test.ts index 4d5e1e96..58b39c7c 100644 --- a/__tests__/worktree.test.ts +++ b/__tests__/worktree.test.ts @@ -104,7 +104,7 @@ describe('generateWorktree', () => { path.join(workspace, 'worktree'), true ) - expect(commitMessages).toBe('gh1') + expect(commitMessages.stdout).toBe('gh1') }) }) describe('with missing branch and new commits', () => { @@ -132,7 +132,7 @@ describe('generateWorktree', () => { path.join(workspace, 'worktree'), true ) - expect(commitMessages).toBe('Initial no-pages commit') + expect(commitMessages.stdout).toBe('Initial no-pages commit') }) }) describe('with existing branch and singleCommit', () => { diff --git a/action.yml b/action.yml index ed3f21a5..8f761b37 100644 --- a/action.yml +++ b/action.yml @@ -60,6 +60,11 @@ inputs: description: 'Do not actually push back, but use `--dry-run` on `git push` invocations insead.' required: false + force: + description: 'Whether to force-push and overwrite any existing deployment. Setting this to false will attempt to rebase simultaneous deployments. This option is on by default and can be toggled off by setting it to false.' + required: false + default: true + git-config-name: description: 'Allows you to customize the name that is attached to the GitHub config which is used when pushing the deployment commits. If this is not included it will use the name in the GitHub context, followed by the name of the action.' required: false diff --git a/src/constants.ts b/src/constants.ts index 02e9e8bb..55905164 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -33,6 +33,8 @@ export interface ActionInterface { folder: string /** The auto generated folder path. */ folderPath?: string + /** Whether to force-push or attempt to merge existing changes. */ + force?: boolean /** Determines test scenarios the action is running in. */ isTest: TestFlag /** The git config name. */ @@ -85,6 +87,9 @@ export const action: ActionInterface = { dryRun: !isNullOrUndefined(getInput('dry-run')) ? getInput('dry-run').toLowerCase() === 'true' : false, + force: !isNullOrUndefined(getInput('force')) + ? getInput('force').toLowerCase() === 'true' + : true, clean: !isNullOrUndefined(getInput('clean')) ? getInput('clean').toLowerCase() === 'true' : false, diff --git a/src/execute.ts b/src/execute.ts index 0f41a55b..db560fe9 100644 --- a/src/execute.ts +++ b/src/execute.ts @@ -1,7 +1,12 @@ import {exec} from '@actions/exec' import buffer from 'buffer' -let output = '' +type ExecuteOutput = { + stdout: string + stderr: string +} + +const output: ExecuteOutput = {stdout: '', stderr: ''} /** Wrapper around the GitHub toolkit exec command which returns the output. * Also allows you to easily toggle the current working directory. @@ -9,21 +14,24 @@ let output = '' * @param {string} cmd - The command to execute. * @param {string} cwd - The current working directory. * @param {boolean} silent - Determines if the in/out should be silenced or not. + * @param {boolean} ignoreReturnCode - Determines whether to throw an error + * on a non-zero exit status or to leave implementation up to the caller. */ export async function execute( cmd: string, cwd: string, - silent: boolean -): Promise { - output = '' + silent: boolean, + ignoreReturnCode = false +): Promise { + output.stdout = '' + output.stderr = '' await exec(cmd, [], { // Silences the input unless the INPUT_DEBUG flag is set. silent, cwd, - listeners: { - stdout - } + listeners: {stdout, stderr}, + ignoreReturnCode }) return Promise.resolve(output) @@ -31,7 +39,20 @@ export async function execute( export function stdout(data: Buffer | string): void { const dataString = data.toString().trim() - if (output.length + dataString.length < buffer.constants.MAX_STRING_LENGTH) { - output += dataString + if ( + output.stdout.length + dataString.length < + buffer.constants.MAX_STRING_LENGTH + ) { + output.stdout += dataString + } +} + +export function stderr(data: Buffer | string): void { + const dataString = data.toString().trim() + if ( + output.stderr.length + dataString.length < + buffer.constants.MAX_STRING_LENGTH + ) { + output.stderr += dataString } } diff --git a/src/git.ts b/src/git.ts index 45ab9821..33565fb7 100644 --- a/src/git.ts +++ b/src/git.ts @@ -108,11 +108,15 @@ export async function deploy(action: ActionInterface): Promise { // Checks to see if the remote exists prior to deploying. const branchExists = action.isTest & TestFlag.HAS_REMOTE_BRANCH || - (await execute( - `git ls-remote --heads ${action.repositoryPath} refs/heads/${action.branch}`, - action.workspace, - action.silent - )) + Boolean( + ( + await execute( + `git ls-remote --heads ${action.repositoryPath} refs/heads/${action.branch}`, + action.workspace, + action.silent + ) + ).stdout + ) await generateWorktree(action, temporaryDeploymentDirectory, branchExists) @@ -186,11 +190,15 @@ export async function deploy(action: ActionInterface): Promise { const hasFilesToCommit = action.isTest & TestFlag.HAS_CHANGED_FILES || - (await execute( - checkGitStatus, - `${action.workspace}/${temporaryDeploymentDirectory}`, - true // This output is always silenced due to the large output it creates. - )) + Boolean( + ( + await execute( + checkGitStatus, + `${action.workspace}/${temporaryDeploymentDirectory}`, + true // This output is always silenced due to the large output it creates. + ) + ).stdout + ) if ( (!action.singleCommit && !hasFilesToCommit) || @@ -216,12 +224,67 @@ export async function deploy(action: ActionInterface): Promise { `${action.workspace}/${temporaryDeploymentDirectory}`, action.silent ) - if (!action.dryRun) { + + if (action.dryRun) { + info(`Dry run complete`) + return Status.SUCCESS + } + + if (action.force) { + // Force-push our changes, overwriting any changes that were added in + // the meantime + info(`Force-pushing changes...`) await execute( `git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, `${action.workspace}/${temporaryDeploymentDirectory}`, action.silent ) + } else { + // Attempt to push our changes, but fetch + rebase if there were + // other changes added in the meantime + const ATTEMPT_LIMIT = 3 + let attempt = 0 + // Keep track of whether the most recent attempt was rejected + let rejected = false + do { + attempt++ + if (attempt > ATTEMPT_LIMIT) throw new Error(`Attempt limit exceeded`) + + // Handle rejection for the previous attempt first such that, on + // the final attempt, time is not wasted rebasing it when it will + // not be pushed + if (rejected) { + info(`Fetching upstream ${action.branch}...`) + await execute( + `git fetch ${action.repositoryPath} ${action.branch}:${action.branch}`, + `${action.workspace}/${temporaryDeploymentDirectory}`, + action.silent + ) + info(`Rebasing this deployment onto ${action.branch}...`) + await execute( + `git rebase ${action.branch} ${temporaryDeploymentBranch}`, + `${action.workspace}/${temporaryDeploymentDirectory}`, + action.silent + ) + } + + info(`Pushing changes... (attempt ${attempt} of ${ATTEMPT_LIMIT})`) + const pushResult = await execute( + `git push --porcelain ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, + `${action.workspace}/${temporaryDeploymentDirectory}`, + action.silent, + true // Ignore non-zero exit status + ) + + rejected = + pushResult.stdout.includes(`[rejected]`) || + pushResult.stdout.includes(`[remote rejected]`) + if (rejected) info('Updates were rejected') + + // If the push failed for any reason other than being rejected, + // there is a problem + if (!rejected && pushResult.stderr) throw new Error(pushResult.stderr) + } while (rejected) } info(`Changes committed to the ${action.branch} branch… đŸ“¦`)