Skip to content

Commit

Permalink
add snapshots back to feature-2gp
Browse files Browse the repository at this point in the history
  • Loading branch information
jlantz committed Sep 6, 2024
1 parent 14a29c6 commit 1002d5a
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/feature-test-2gp-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ jobs:
id: feature_test
env:
GITHUB_TOKEN: "${{ secrets.github-token }}"
run: cci flow run ci_feature_2gp | tee feature-test.log
run: |
cci flow run ci_feature_2gp | tee feature-test.log
continue-on-error: true

- name: Check for Apex Test Failure
Expand Down
181 changes: 168 additions & 13 deletions .github/workflows/feature-test-2gp.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
name: 2GP Feature Test

name: 2GP Feature Test and PR Snapshot
on:
workflow_call:
inputs:
org:
debug:
required: false
default: false
type: boolean
snapshot-pr:
required: false
default: false
type: boolean
snapshot-pr-label:
required: false
default: feature
type: string
debug:
snapshot-pr-draft:
required: false
default: false
type: boolean
# snapshot-fail:
# required: false
# default: false
# type: boolean
snapshot-fail-pr:
required: false
default: false
type: boolean
snapshot-fail-pr-label:
required: false
type: string
snapshot-fail-pr-draft:
required: false
default: false
type: boolean
snapshot-fail-test-only:
required: false
default: false
type: boolean
project-code:
required: false
default: "VA"
type: string

secrets:
# Either dev-hub-auth-url or dev-hub-username, dev-hub-client-id, and dev-hub-private-key are required
dev-hub-auth-url:
required: false
dev-hub-username:
Expand All @@ -28,8 +56,8 @@ on:
required: true

jobs:
feature-test:
name: "2GP Feature Test"
feature-test-and-snapshot:
name: "2GP Feature Test and PR Snapshot"
runs-on: ubuntu-latest
container:
image: ghcr.io/muselab-d2x/d2x:cumulusci-next
Expand All @@ -46,15 +74,47 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Check for PR
id: check-pr
uses: actions/github-script@v6
with:
script: |
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${context.ref.replace('refs/heads/', '')}`,
state: 'open'
});
if (pullRequests.length > 0) {
const pr = pullRequests[0];
console.log('PR found:', pr.number);
core.setOutput('pr-number', pr.number);
core.setOutput('has-pr', 'true');
core.setOutput('is-draft', pr.draft.toString());
const labels = pr.labels.map(label => label.name);
core.setOutput('has-snapshot-label', labels.includes('${{ inputs.snapshot-pr-label }}').toString());
core.setOutput('has-snapshot-fail-label', labels.includes('${{ inputs.snapshot-fail-pr-label }}').toString());
} else {
console.log('No PR found');
core.setOutput('has-pr', 'false');
core.setOutput('is-draft', 'false');
core.setOutput('has-snapshot-label', 'false');
core.setOutput('has-snapshot-fail-label', 'false');
}
- name: Auth to DevHub
run: /usr/local/bin/devhub.sh
- name: Set ${{ inputs.org }} org as default org
run: cci org default ${{ inputs.org }}

- name: Set feature org as default org
run: cci org default feature

- name: Build Feature Test Package
env:
GITHUB_TOKEN: "${{ secrets.github-token }}"
run: cci flow run build_feature_test_package $([[ "${{ inputs.debug }}" == "true" ]] && echo " --debug") | tee cumulusci-flow.log
shell: bash

- name: Set Commit Status
env:
GITHUB_TOKEN: "${{ secrets.github-token }}"
Expand All @@ -68,12 +128,107 @@ jobs:
-f description="version_id: $VERSION" \
-f context='Build Feature Test Package'
shell: bash

- name: Run Feature Test
id: feature_test
env:
GITHUB_TOKEN: "${{ secrets.github-token }}"
run: cci flow run ci_feature_2gp
run: |
cci flow run ci_feature_2gp | tee feature-test.log
continue-on-error: true

- name: Check for Apex Test Failure
id: check_apex_failure
if: ${{ failure() }}
run: |
if cci error info | grep -q "cumulusci.salesforce_api.exceptions.ApexTestException"; then
echo "Apex test failure detected"
echo "apex_failure=true" >> $GITHUB_OUTPUT
elif grep -q "Apex Test Failure" feature-test.log; then
echo "Apex test failure detected in log"
echo "apex_failure=true" >> $GITHUB_OUTPUT
else
echo "No Apex test failure detected"
echo "apex_failure=false" >> $GITHUB_OUTPUT
fi
shell: bash

- name: Determine if Snapshot Should be Created
id: should-snapshot
run: |
CREATE_SNAPSHOT="false"
if [[ "${{ steps.check-pr.outputs.has-pr }}" == "true" ]]; then
if [[ "${{ inputs.snapshot-pr }}" == "true" ]]; then
if [[ "${{ steps.check-pr.outputs.has-snapshot-label }}" == "true" || "${{ inputs.snapshot-pr-label }}" == "" ]]; then
if [[ "${{ inputs.snapshot-pr-draft }}" == "true" || "${{ steps.check-pr.outputs.is-draft }}" != "true" ]]; then
CREATE_SNAPSHOT="true"
fi
fi
fi
if [[ "${{ steps.check_apex_failure.outputs.apex_failure }}" == "true" ]]; then
if [[ "${{ inputs.snapshot-fail-pr }}" == "true" ]]; then
if [[ "${{ steps.check-pr.outputs.has-snapshot-fail-label }}" == "true" || "${{ inputs.snapshot-fail-pr-label }}" == "" ]]; then
if [[ "${{ inputs.snapshot-fail-pr-draft }}" == "true" || "${{ steps.check-pr.outputs.is-draft }}" != "true" ]]; then
CREATE_SNAPSHOT="true"
fi
fi
else
CREATE_SNAPSHOT="true"
fi
elif [[ "${{ inputs.snapshot-fail-test-only }}" == "true" && "${{ failure() }}" == "true" ]]; then
CREATE_SNAPSHOT="true"
fi
elif [[ "${{ inputs.snapshot-fail }}" == "true" && ("${{ steps.check_apex_failure.outputs.apex_failure }}" == "true" || ("${{ inputs.snapshot-fail-test-only }}" == "true" && "${{ failure() }}" == "true")) ]]; then
CREATE_SNAPSHOT="true"
fi
echo "create=$CREATE_SNAPSHOT" >> $GITHUB_OUTPUT
shell: bash

- name: Create Snapshot
if: steps.should-snapshot.outputs.create == 'true'
run: |
PR_NUMBER=${{ steps.check-pr.outputs.pr-number }}
# Product Code (PC)
PC="${{ inputs.project-code }}"
if [[ ${#PC} -ne 2 ]]; then
echo "Error: Project code must be exactly 2 characters long"
exit 1
fi
IMAGE_NAME="${IMAGE_NAME}Pr${PR_NUMBER}"
# Image Name
if [[ "${{ steps.check_apex_failure.outputs.apex_failure }}" == "true" ]]; then
IMAGE_NAME="Ftests"
elif [[ "${{ failure() }}" == "true" ]]; then
IMAGE_NAME="Fbuild"
# Truncate Image Name if necessary (leaving room for OrgTypeCode)
MAX_IMAGE_LENGTH=$((11 - ${#PC}))
if [[ ${#IMAGE_NAME} -gt $MAX_IMAGE_LENGTH ]]; then
IMAGE_NAME="${IMAGE_NAME:0:$MAX_IMAGE_LENGTH}"
fi
# Org Type Code
ORG_TYPE_CODE="M" # Since this is a 2GP managed package org snapshot
# Construct the final snapshot name
SNAPSHOT_NAME="${PC}${IMAGE_NAME}${ORG_TYPE_CODE}"
# Ensure the snapshot name is not longer than 14 characters
if [[ ${#SNAPSHOT_NAME} -gt 13 ]]; then
echo "Warning: Snapshot name exceeds 13 characters without the Org Code. Truncating."
SNAPSHOT_NAME="${SNAPSHOT_NAME:0:13}"
fi
echo "Creating snapshot: $SNAPSHOT_NAME"
cci task run create_snapshot --org feature --snapshot-name $SNAPSHOT_NAME --description "Snapshot for ${{ github.ref }}"
shell: bash

- name: Delete Scratch Org
if: ${{ always() }}
run: |
cci org scratch_delete feature
run: cci org scratch_delete feature
shell: bash

0 comments on commit 1002d5a

Please sign in to comment.