Skip to content

WIP: Remove force push to Pantheon by preparing local history #34

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

Draft
wants to merge 44 commits into
base: 0.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0342d5d
Starting on automated tests for removal of force push (#21)
stevector Apr 10, 2025
8318b54
merge
stevector Apr 10, 2025
d67e930
fix conflict
stevector Apr 10, 2025
c86f91c
move calling of prep script
stevector Apr 10, 2025
c4d50dd
action
stevector Apr 10, 2025
5e5a26b
no force
stevector Apr 10, 2025
535b70a
trying action path again
stevector Apr 11, 2025
eddc7ad
Merge remote-tracking branch 'origin/0.x' into remove-force-pushing
stevector Apr 11, 2025
7c420f5
unshallowing probably is not necessary if the history from Pantheon i…
stevector Apr 11, 2025
5ba4388
removing comments and extra git remote -v
stevector Apr 11, 2025
c576943
site root stuff
stevector Apr 11, 2025
b90ea1d
more
stevector Apr 11, 2025
0e460b0
removing a cd
stevector Apr 11, 2025
317f93f
comment
stevector Apr 11, 2025
9f3b214
removing all cds
stevector Apr 11, 2025
d3ab753
move variable setting
stevector Apr 11, 2025
56a9b2b
run all bats tests
stevector Apr 11, 2025
8046859
set working dir at the top
stevector Apr 11, 2025
987d236
try again for working dir defaul
stevector Apr 11, 2025
3d37ffd
site_root variable
stevector Apr 11, 2025
d600880
fixing action
stevector Apr 11, 2025
7ef1f08
whitespace
stevector Apr 11, 2025
945f0a8
Delete tests/plan.txt
stevector Apr 12, 2025
6d7227e
readme
stevector Apr 12, 2025
33be6a9
whitespace to re-trigger builds
stevector Apr 14, 2025
b5e94f0
Update setup_mock_repos.bash
stevector Apr 14, 2025
5d2e0ac
intermediate
stevector Apr 14, 2025
e79a37f
hello world
stevector Apr 14, 2025
5432ac2
clean up
stevector Apr 14, 2025
cc580d8
more clean up
stevector Apr 14, 2025
3be67a5
better if statement
stevector Apr 14, 2025
a1fad21
setting PANTHEON_REPO_LOCATION
stevector Apr 14, 2025
fcbd49a
accounting for master branch
stevector Apr 14, 2025
0d7f3d8
Use 3.x branch of build tools
stevector Apr 14, 2025
a011d52
merging
stevector Apr 14, 2025
de2ee43
fixing merge conflict
stevector Apr 14, 2025
571f74f
Update action.yml
stevector Apr 14, 2025
d46a17c
trying stash and pop
stevector Apr 15, 2025
b9fbd28
Merge branch '0.x' into remove-force-pushing
stevector Apr 15, 2025
d2f9b31
try --soft
stevector Apr 15, 2025
0de8170
remove debugging
stevector Apr 15, 2025
80dfe78
remove debugging
stevector Apr 15, 2025
a0388a9
fix bats
stevector Apr 15, 2025
d0a6f1c
try again
stevector Apr 15, 2025
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
3 changes: 1 addition & 2 deletions .github/testing_fixtures/dtp-nearly-empty-site/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

echo "Hello World<br>";
echo "Hello World";
echo "Hello world";
header('Pragma: no-cache');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
echo "Hello world";
echo "In bats testing, this file will be copied to replace the root index.php file";
echo "this is a commit on test-pr";
16 changes: 15 additions & 1 deletion .github/workflows/deploy-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
permissions:
deployments: write
contents: read
pull-requests: read
pull-requests: read
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -30,3 +30,17 @@ jobs:
git_commit_message: "This is an automated commit message from GitHub Actions."
delete_old_environments: true
clone_content: true

unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Bats and bats libs
id: setup-bats
uses: bats-core/bats-action@3.0.0
- name: Test bash scripts with bats
shell: bash
# env:
# BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}
# TERM: xterm
run: bats tests/
75 changes: 39 additions & 36 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,24 @@ inputs:
required: false
default: ""

# todo checkout_repo:
# description: "A boolean for whether or not this composite step should checkout the git repo. Set to false if a checkout has already been perf"

runs:
using: 'composite'
steps:
# Setting this environment variable at the first step makes it cleaner
# for later steps to run inside with working-directory: ${{ env.SITE_ROOT }}
# Not all consumers of this Action have a Git root on GitHub that matches
# the site root on Pantheon. For those projects, we need to set this variable.
- name: Create SITE_ROOT variable
shell: bash
run: echo "SITE_ROOT=${{ inputs.relative_site_root != '' && inputs.relative_site_root || '.' }}" >> $GITHUB_ENV

- name: Set target_env
id: set_target_env
shell: bash
env:
PR_NUM: ${{ github.event.pull_request.number }}
INPUT_TARGET_ENV: ${{ inputs.target_env }}
# todo, move this to a bats-testable bash script.
run: |
# Set target_env
set +e
Expand Down Expand Up @@ -123,10 +128,13 @@ runs:

- name: Install Terminus Plugin
shell: bash
run: |
# Install Terminus Plugin
set +e
terminus self:plugin:install terminus-build-tools-plugin
run: terminus self:plugin:install "pantheon-systems/terminus-build-tools-plugin:3.x-dev"

- name: Set PANTHEON_REPO_LOCATION
shell: bash
env:
PANTHEON_SITE: ${{ inputs.site }}
run: echo "PANTHEON_REPO_LOCATION=$(terminus connection:info ${PANTHEON_SITE}.dev --field=git_url)" >> $GITHUB_ENV

- name: Configure Git User
shell: bash
Expand All @@ -139,43 +147,44 @@ runs:
git config --global user.email "${PANTHEON_GIT_USER_EMAIL}"
git config --global user.name "${PANTHEON_GIT_USER_NAME}"

- name: Prepare site root
# This change is necessary to fetch from Pantheon's Git server.
- name: Turn off strict host checking
shell: bash
run: mkdir -p $HOME/.ssh && echo "StrictHostKeyChecking no" >> "$HOME/.ssh/config"

- name: Create local Git repo (only when relative_site_root is set)
shell: bash
if: ${{ inputs.relative_site_root != '' }}
working-directory: ${{ env.SITE_ROOT }}
env:
SITE_ROOT: ${{ inputs.relative_site_root }}
run: |
# Prepare site root
# Create local Git repo (only when relative_site_root is set)
set +e
if [ -n "$SITE_ROOT" ]; then
cd ${SITE_ROOT}
git init -b main
echo "this origin setting is a hack to get past the 'inferring' of a git provider in build tools"
git remote add origin https://github.com/${{ github.repository }}
else
git fetch --unshallow origin
fi
git init -b main
echo "this origin setting is a hack to get past the 'inferring' of a Git provider in Build Tools"
git remote add origin https://github.com/${{ github.repository }}

- name: Prepare site root's Git History
shell: bash
working-directory: ${{ env.SITE_ROOT }}
env:
PANTHEON_SITE: ${{ inputs.site }}
run: bash ${{ github.action_path }}/scripts/prepare-repo.sh

- name: Deployment Processing
shell: bash
id: deploy_to_pantheon
working-directory: ${{ env.SITE_ROOT }}
env:
PANTHEON_SITE: ${{ inputs.site }}
PANTHEON_SOURCE_ENV: ${{ inputs.source_env }}
SITE_ROOT: ${{ inputs.relative_site_root }}
PANTHEON_COMMIT_MESSAGE: ${{ inputs.git_commit_message }}
# todo, might need to make this configurable too.
# the default in build tools is 180. But that's regularly failing
# on a small personal site.
TERMINUS_BUILD_TOOLS_WORKFLOW_TIMEOUT: 300
run: |
# Push to Pantheon
set +e

if [ -n "$SITE_ROOT" ]; then
cd ${SITE_ROOT}
fi

terminus -n build:env:create ${PANTHEON_SITE}.${PANTHEON_SOURCE_ENV} ${PANTHEON_TARGET_ENV} --yes --message="${PANTHEON_COMMIT_MESSAGE}" ${PANTHEON_CLONE_CONTENT_FLAG}
run: terminus -n build:env:create ${PANTHEON_SITE}.${PANTHEON_SOURCE_ENV} ${PANTHEON_TARGET_ENV} --yes --message="${PANTHEON_COMMIT_MESSAGE}" ${PANTHEON_CLONE_CONTENT_FLAG} --no-git-force

- name: Update deployment status
uses: bobheadxi/deployments@v1
Expand All @@ -189,19 +198,13 @@ runs:
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env: ${{ env.PANTHEON_TARGET_ENV }}-${{ inputs.site }}
env_url: https://${{ env.PANTHEON_TARGET_ENV }}-${{ inputs.site }}.pantheonsite.io

- name: Deleting old multidevs
if: ${{ inputs.delete_old_environments == 'true' && steps.deploy_to_pantheon.outcome == 'success' }}
working-directory: ${{ env.SITE_ROOT }}
shell: bash
env:
PANTHEON_SITE: ${{ inputs.site }}
GITHUB_TOKEN: ${{ github.token }}
SITE_ROOT: ${{ inputs.relative_site_root }}
run: |
# Delete old multidevs
set +e

if [ -n "$SITE_ROOT" ]; then
cd ${SITE_ROOT}
fi

terminus build:env:delete:pr $PANTHEON_SITE --yes
run: terminus build:env:delete:pr $PANTHEON_SITE --yes
41 changes: 41 additions & 0 deletions scripts/prepare-repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# This script prepares the local repository in a CI environment for pushing to Pantheon.
# To avoid force pushing, it brings in a history from Pantheon so that in a later step
# we can commit on top of it and push to Pantheon.

# Exit on error, undefined variables, and pipe failures
set -euo pipefail

# todo exit with errors if needed env vars are missing.
if [[ -z "${PANTHEON_REPO_LOCATION:-}" ]]; then
echo "Error: PANTHEON_REPO_LOCATION is not set. It should be set to the Pantheon repository URL."
exit 1
fi
if [[ -z "${PANTHEON_TARGET_ENV:-}" ]]; then
echo "Error: PANTHEON_TARGET_ENV is not set."
exit 1
fi

# When runnning on the "main" branch, the target environment is "dev" but the
# associated git branch on Pantheon is "master". So that is the branch we need to
# fetch and reset to.
if [[ "$PANTHEON_TARGET_ENV" == "dev" ]]; then
PANTHEON_TARGET_ENV="master"
fi

git remote add pantheon $PANTHEON_REPO_LOCATION

if git ls-remote --exit-code --heads pantheon "$PANTHEON_TARGET_ENV" > /dev/null; then
echo "the branch already exists in the remote"
else
git fetch pantheon master
git push pantheon FETCH_HEAD:refs/heads/$PANTHEON_TARGET_ENV
fi
git fetch pantheon $PANTHEON_TARGET_ENV

# Reset your working directory to match the remote branch
git reset --hard pantheon/$PANTHEON_TARGET_ENV
# Create and switch to a local branch tracking the remote one
# todo, name the branch based on some variable.
git checkout -B temp-build-branch
5 changes: 5 additions & 0 deletions scripts/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bash scripts

This directory contains bash scripts that are invoked from the `action.yml` at the root of the repository.

Breaking these operations out into separate scripts allows for easier testing and debugging of each individual step.
82 changes: 82 additions & 0 deletions tests/push-to-pantheon.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env bats

# Source the setup script
load "./test_helper/setup_mock_repos"

setup() {

ROOT_OF_TESTS_INVOCATION="$(pwd)"
# Create temporary directories for testing
GITHUB_DIR="$(mktemp -d)"
PANTHEON_DIR="$(mktemp -d)"
CI_DIR="$(mktemp -d)"

# Set up mock repositories
setup_mock_repos "$GITHUB_DIR" "$PANTHEON_DIR" "$CI_DIR"
}

@test "simulate a push to pantheon job when Pantheon does not have the target branch" {

export PANTHEON_REPO_LOCATION=$PANTHEON_DIR
export PANTHEON_TARGET_ENV=pr-123

run mock_ci_build_process "$CI_DIR"
[ "$status" -eq 0 ]

run cat "test.css"
[[ "${output}" =~ "background-color: #FF0000" ]]

run $ROOT_OF_TESTS_INVOCATION/scripts/prepare-repo.sh
echo ${output}
[ "$status" -eq 0 ]

git add .
# todo use a variable for the commit message.
git commit -m 'build process for pr-123'
git push pantheon temp-build-branch:$PANTHEON_TARGET_ENV

echo "checkout that the pantheon repo contains the built CSS"
cd $PANTHEON_DIR
run git show pr-123:test.css
echo ${output}
[[ "${output}" =~ "background-color: #FF0000" ]]
}

@test "simulate a push to pantheon job when Pantheon already does have the target branch" {

export PANTHEON_REPO_LOCATION=$PANTHEON_DIR
export PANTHEON_TARGET_ENV=pr-123

cd $PANTHEON_DIR
git checkout -b $PANTHEON_TARGET_ENV
echo "hello world" > test.txt
git add .
git commit -m 'adding test.txt'
git checkout master

run git show $PANTHEON_TARGET_ENV:test.txt
echo ${output}
[[ "${output}" =~ "hello world" ]]

cd $CI_DIR
run mock_ci_build_process "$CI_DIR"
[ "$status" -eq 0 ]

run cat "test.css"
[[ "${output}" =~ "background-color: #FF0000" ]]

run $ROOT_OF_TESTS_INVOCATION/scripts/prepare-repo.sh
echo ${output}
[ "$status" -eq 0 ]

git add .
# todo use a variable for the commit message.
git commit -m 'build process for pr-123'
git push pantheon temp-build-branch:$PANTHEON_TARGET_ENV

echo "checkout that the pantheon repo contains the built CSS"
cd $PANTHEON_DIR
run git show pr-123:test.css
echo ${output}
[[ "${output}" =~ "background-color: #FF0000" ]]
}
Loading
Loading