-
Notifications
You must be signed in to change notification settings - Fork 153
178 lines (156 loc) · 9.75 KB
/
back-merge.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Back-merging should typically be done as part of your Continuous Integration (CI) process
# By performing back-merging as part of the CI process, we can catch and fix any conflicts between the dev and main branches early in the development cycle, before the changes are deployed to production. This helps to ensure that the dev branch remains in a releasable state, and reduces the risk of integration issues when changes are eventually merged into the main branch.
name: Back-Merge
on:
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
# https://stackoverflow.com/questions/63343937/how-to-use-the-github-actions-workflow-run-event
# https://blog.pother.ca/github-actions-workflow_run-event/
# This event will only trigger a workflow run if the workflow file is on the default branch.
# According documentation 'GITHUB_REF' for workflow_run always is default branch(develop), and for access original branch for workflow_run we can use 'github.event.workflow_run.head_branch'
workflow_run:
workflows: [ "Catalogs-CI-CD", "Customers-CI-CD", "Identity-CI-CD", "Orders-CI-CD" ]
branches: [ develop, main, preview, beta ]
types: [ completed ]
pull_request:
types: [closed] # when PR is merged, CD will be triggered
branches:
- develop
- beta
- preview
- main
workflow_dispatch:
logLevel:
description: 'Log level'
required: true
default: 'info'
type: choice
options:
- info
- warning
- debug
jobs:
pre-check:
runs-on: ubuntu-latest
# Skipping workflow runs for some commits types
# https://itnext.io/automate-your-integration-tests-and-semantic-releases-with-github-actions-43875ad83092
# https://github.com/actions/runner/issues/491#issuecomment-850884422
# https://stackoverflow.com/questions/69354003/github-action-job-fire-when-previous-job-skipped
# we should not filter on head commit message types like 'chore', 'docs' because it is possible it our latest SHA commit in integration branches like develop and main be these types and it will skips whole of our trigger
if: |
github.actor != 'dependabot[bot]'
steps:
- name: Job Info
run: |
echo "pre-check is successful."
echo workspace is: ${{ github.workspace }}
echo "is workflow_dispatch event? ${{ github.event_name == 'workflow_dispatch' }}"
echo "is push event? ${{ github.event_name == 'push' }}"
echo "is pull request event? ${{ github.event_name == 'pull_request' }}"
echo "pull_request.head.ref is: ${{ github.event.pull_request.head.ref }}"
echo "github.ref_name is: ${{ github.ref_name }}"
echo "github.ref is: ${{ github.ref }}"
echo "github.head_ref is: ${{ github.head_ref }}"
echo "should publish in dispatch mode? ${{ github.event.inputs.should-publish }}"
back-merge:
runs-on: ubuntu-latest
needs: [ pre-check ]
if: |
success() &&
github.event_name != 'pull_request' &&
((github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' && contains(fromJson('["develop", "main", "preview", "beta"]'), github.event.workflow_run.head_branch)) || (github.event_name == 'workflow_dispatch' && contains(fromJson('["develop", "main", "preview", "beta"]'), github.ref_name)))
permissions:
pull-requests: write
steps:
- name: Set branch name as env variable
shell: bash
run: |
if [ ${{ github.event_name }} = 'workflow_run' ]; then
echo "BRANCH=${{ github.event.workflow_run.head_branch }}" >> $GITHUB_ENV
else
echo "BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
fi
# https://github.com/cycjimmy/semantic-release-action/issues/6
# https://github.com/actions/checkout#push-a-commit-using-the-built-in-token
# https://stackoverflow.com/questions/750172/how-do-i-change-the-author-and-committer-name-email-for-multiple-commits
# https://github.com/semantic-release/semantic-release/issues/1208
# https://github.com/orgs/community/discussions/26560
# https://blog.pother.ca/github-actions-workflow_run-event/
# https://stackoverflow.com/questions/63343937/how-to-use-the-github-actions-workflow-run-event
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
# https://github.com/semantic-release/semantic-release/blob/b9b5c7689f0acdfdc079c839db0fcf78339745e2/index.js#L92
## https://github.com/actions/checkout/issues/439#issuecomment-965968956
# get latest remote change because sematic-release in `verifyConditions` event checks local branch has latest remote branch changes, for preventing: The local branch `something` is behind the remote one, therefore a new version won't be published.
# By default checkout@v3, will check branch on ref/SHA that triggered in starting workflow, so if inner a job in the workflow we change HEAD of repository by changing code, subsequent jobs don't get these commits and they get ref/SHA that triggered in starting workflow
# we can't create a `composite-action` for `checkout` because for finding composite-action locally with relative path, repository should be `checkout` first
- name: Check out code
uses: actions/checkout@v3
with:
# https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches
# Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags
fetch-depth: 0
repository: ${{ github.repository }}
token: ${{ secrets.GITHUB_TOKEN }}
# set ref to 'github.ref' works correctly with both pull_requests event and push event and this is default behavior checkout action when we don't use ref attribute
ref: ${{ github.ref }}
- run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
shell: bash
- name: Call Composite Action back-merge
uses: ./.github/actions/back-merge
id: back-merge-step
with:
source-branch: ${{ env.BRANCH }}
back-merge-pr-closed:
runs-on: ubuntu-latest
needs: [ pre-check ]
permissions:
contents: write # for back-merging feature branch
if: |
success() &&
(github.event_name == 'pull_request' && github.event.pull_request.merged == true && contains(fromJson('["develop", "preview", "beta"]'), github.ref_name))
steps:
- name: Set branch name as env variable
shell: bash
run: |
if [ ${{ github.event_name }} = 'workflow_run' ]; then
echo "BRANCH=${{ github.event.workflow_run.head_branch }}" >> $GITHUB_ENV
else
echo "BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
fi
# https://github.com/cycjimmy/semantic-release-action/issues/6
# https://github.com/actions/checkout#push-a-commit-using-the-built-in-token
# https://stackoverflow.com/questions/750172/how-do-i-change-the-author-and-committer-name-email-for-multiple-commits
# https://github.com/semantic-release/semantic-release/issues/1208
# https://github.com/orgs/community/discussions/26560
# https://blog.pother.ca/github-actions-workflow_run-event/
# https://stackoverflow.com/questions/63343937/how-to-use-the-github-actions-workflow-run-event
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
# https://github.com/semantic-release/semantic-release/blob/b9b5c7689f0acdfdc079c839db0fcf78339745e2/index.js#L92
## https://github.com/actions/checkout/issues/439#issuecomment-965968956
# get latest remote change because sematic-release in `verifyConditions` event checks local branch has latest remote branch changes, for preventing: The local branch `something` is behind the remote one, therefore a new version won't be published.
# By default checkout@v3, will check branch on ref/SHA that triggered in starting workflow, so if inner a job in the workflow we change HEAD of repository by changing code, subsequent jobs don't get these commits and they get ref/SHA that triggered in starting workflow
# we can't create a `composite-action` for `checkout` because for finding composite-action locally with relative path, repository should be `checkout` first
- name: Check out code
uses: actions/checkout@v3
with:
# https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches
# Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags
fetch-depth: 0
repository: ${{ github.repository }}
token: ${{ secrets.GITHUB_TOKEN }}
# set ref to 'github.ref' works correctly with both pull_requests event and push event and this is default behavior checkout action when we don't use ref attribute
ref: ${{ github.ref }}
- run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
shell: bash
# https://stackoverflow.com/questions/69839851/github-actions-copy-git-user-name-and-user-email-from-last-commit
# https://github.com/orgs/community/discussions/26560
# https://github.com/semantic-release/semantic-release/discussions/2557
# https://github.com/semantic-release/github/issues/175
# this needs a PAT with write permission without doing pull request
- name: Back Merge Feature Branches
shell: bash
run: |
./back-merge.sh ${{ github.ref_name }}