-
Notifications
You must be signed in to change notification settings - Fork 18
130 lines (110 loc) · 6.16 KB
/
pr-on-integration-branch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Unique name for this workflow
name: Validate PR on integration branch
# Definition when the workflow should run
on:
pull_request:
# This workflow will run when a pull request is opened, or when a commit is pushed
# to a branch that has an existing pull request
types: [opened, synchronize]
branches: [ development ]
# We only care about changes to the force-app directory. This prevents the job from running
# when changing non-salesforce files (like this yml file).
paths:
- 'force-app/**'
# Jobs to be executed
jobs:
validate-deployment-on-integration-org:
runs-on: ubuntu-latest
if: ${{ github.actor != 'dependabot[bot]' }}
steps:
# Install nodejs
- uses: actions/setup-node@v2
with:
node-version: '14'
# Checkout the source code
- name: 'Checkout source code'
uses: actions/checkout@v2
with:
fetch-depth: 0
# Read the body of the pull request and extract the names of the
# tests classes that should be run when deploying the changes on this commit
# The tests should be specified in the PR body using the following syntax
# Apex::[CommunitiesLoginControllerTest,MyProfilePageControllerTest,AnotherTestClass]::Apex
# This special delimeter can be added to the PR template so that your team doesn't have to remember the syntax
- name: 'Read PR Body'
env:
# The pull request body is available through the github context object
# we put the body of the pull request in an env variable (only available to this step)
PAYLOAD: ${{github.event.pull_request.body}}
# Here we print the content of the environment variable and
# pipe to a a text file.
# Then we call the local script parsePR.js, which will create
# a new file called testsToRun.txt. This file will have the list
# of tests to run separated by a comma
# Finally, we add the list of tests to the $GITHUB_ENV variable
# as this allows us to reference the list in a subsequent step. If you
# were using a normal env variable, its value would not be available outside this step.
run: |
echo $PAYLOAD > ./pr_body.txt
node ./parsePR.js
TESTS=$(cat testsToRun.txt)
echo "APEX_TESTS=$TESTS" >> $GITHUB_ENV
# Install Salesforce CLI
- name: 'Install Salesforce CLI'
run: |
wget https://developer.salesforce.com/media/salesforce-cli/sfdx/channels/stable/sfdx-linux-x64.tar.xz
mkdir ~/sfdx
tar xJf sfdx-linux-x64.tar.xz -C ~/sfdx --strip-components 1
echo "$HOME/sfdx/bin" >> $GITHUB_PATH
~/sfdx/bin/sfdx version
# install SFDX-Git-Delta plugin - https://github.com/scolladon/sfdx-git-delta
- name: 'Installing sfdx git delta'
run: |
echo y | sfdx plugins:install sfdx-git-delta
sfdx plugins
# Install java
- name: 'Installing java'
run: sudo apt-get install openjdk-8-jdk
# Install SFDX scanner
- name: 'Installing SFDX scanner'
run: sfdx plugins:install @salesforce/sfdx-scanner
# Get the Integration Org SFDX URL and store it in a text file
- name: 'Populate auth file with SFDX_URL secret of integration org'
shell: bash
run: |
echo ${{ secrets.SFDX_INTEGRATION_URL}} > ./SFDX_INTEGRATION_URL.txt
# Authenticate to org using the URL stored in the text file
- name: 'Authenticate to Integration Org'
run: sfdx auth:sfdxurl:store -f ./SFDX_INTEGRATION_URL.txt -s -a integration
# We use SFDX Git Delta to create a directory with only the metadata that has changed, this allows us
# to deploy only those changes, as opposed to deploying the entire branch. This helps reducing deployment times
- name: 'Create delta packages for new, modified or deleted metadata'
run: |
mkdir changed-sources
sfdx sgd:source:delta --to "HEAD" --from "HEAD^" --output changed-sources/ --generate-delta --source force-app/
# Scan the code in the delta directory by using sfdx scanner. Output the results of the scan in a new file
# called apexScanResults.sarif
- name: 'Scan code'
run: |
cd changed-sources
sfdx scanner:run --format sarif --target './**/*.cls' --category "Design,Best Practices,Performance" --outfile 'apexScanResults.sarif'
cd ..
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: changed-sources/apexScanResults.sarif
# We do a check-only depploy and we only run the tests specified in the PR
# If the env variable does not equal 'all', we know that there is a list of
# tests that can be run
- name: 'Check-only deploy delta changes - run specified tests'
if: ${{ env.APEX_TESTS != 'all' }}
run: |
echo ${{env.APEX_TESTS}}
sfdx force:source:deploy -p "changed-sources/force-app" --checkonly --testlevel RunSpecifiedTests --runtests ${{env.APEX_TESTS}} --json
# We do a check-only deploy and we run all tests
- name: 'Check-only deploy delta changes - run all tests'
if: ${{ env.APEX_TESTS == 'all' }}
run: |
sfdx force:source:deploy -p "changed-sources/force-app" --checkonly --testlevel RunLocalTests --json
- name: 'Deploy destructive changes (if any)'
run: sfdx force:mdapi:deploy -d "changed-sources/destructiveChanges" --checkonly --ignorewarnings