I am trying to build the CI / CD for learning the Devops CI and CD flows with simple branching strategy.
Branch Strategy :
| branch name | environment |
main --> prod
release/* --> uat
feature/* --> dev
The scenario I am testing with the workflow below is as follows: whenever a developer pushes code to the feature/developer-1 branch, the build and deploy jobs should execute and deploy to the dev environment. After this, the developer will create a pull request to merge into the release/test-release branch, and the build and deploy jobs should run again. However, the workflow is skipping the jobs on the PR merge, and the jobs are incorrectly running in the feature branch instead of in the release/test-release branch after the merge.
Mock workflow :
name: mock CI/CD
on:
push:
branches:
- 'feature/*'
pull_request:
branches:
- 'release/*'
jobs:
build:
runs-on: ubuntu-latest
if: |
github.event_name == 'push' && startsWith(github.ref, 'refs/heads/feature/') ||
(github.event_name == 'pull_request' && github.event.pull_request.merged == true && startsWith(github.event.pull_request.base.ref, 'release/'))
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Get commit message
run: |
echo "Commit message: $(git log -1 --pretty=%B)" > commit_message.txt
- name: Display commit message
run: cat commit_message.txt
- name: Create build artifact
run: |
echo "Mock build complete."
mv commit_message.txt file_build.txt
- name: Upload build artifact
uses: actions/upload-artifact@v3
with:
name: build
path: file_build.txt
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Download build artifact
uses: actions/download-artifact@v3
with:
name: build
path: .
- name: Rename build artifact
run: mv file_build.txt file_deploy.txt
- name: Upload deploy artifact
uses: actions/upload-artifact@v3
with:
name: deploy
path: file_deploy.txt
here is my repo code - https://github.com/learndevops33/mock_ci_cd/tree/feature/developer-1
Ensure that your if
condition correctly identifies when a PR is merged. For a PR merge, the github.event.action
should be closed
, and github.event.pull_request.merged
should be true
.
if: |
github.event_name == 'push' && startsWith(github.ref, 'refs/heads/feature/') ||
(github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true && startsWith(github.event.pull_request.base.ref, 'release/'))
I had it working using this syntax with specifying the closed condition in the event level.
pull_request:
branches:
- 'release/*'
types:
- closed
Source documentation: Running your pull_request workflow when a pull request merges