Last active
October 14, 2021 18:20
-
-
Save mehmetcantas/a78daf574684f0405cb5145f8b44f6e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You need create a project access token "Repository -> Settings -> Access Token" | |
# When you create access token you need to add to the CI/CD variables "Repository -> Settings -> CI/CD -> Expand Variables Section" | |
# Click to add variable button and set variable name as GITLAB_CI_PUSH_TOKEN then copy your access token value | |
# Well done after these steps copy and paste following instructions to your .gitlab-ci.yaml file | |
Merge master into hotfix: | |
stage: merge-master-to-hotfix | |
needs: ["StageName"] | |
before_script: | |
- apk update && apk add git | |
- git config user.email "[email protected]" | |
- git config user.name "gitlab-pipeline" | |
script: | |
- echo "Merge master to hotfix" | |
- from=$CI_BUILD_REF_NAME | |
- to=hotfix | |
- git remote set-url origin $CI_REPOSITORY_URL | |
- git fetch --all | |
- git checkout $to | |
- git merge origin/$from | |
- git push -o ci-skip https://$GITLAB_USER_LOGIN:$GITLAB_CI_PUSH_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git | |
type: deploy | |
tags: | |
- k8s | |
rules: | |
- if: '$CI_BUILD_REF_NAME == "master"' | |
when: manual | |
- when: never | |
Merge hotfix into master: | |
stage: merge-hotfix-to-master | |
needs: ["StageName"] | |
before_script: | |
- apk update && apk add git | |
- git config user.email "[email protected]" | |
- git config user.name "gitlab-pipeline" | |
script: | |
- from=$CI_BUILD_REF_NAME | |
- to=master | |
- git remote set-url origin $CI_REPOSITORY_URL | |
- git fetch --all | |
- git checkout $to | |
- git merge origin/$from | |
- git push -o ci-skip https://$GITLAB_USER_LOGIN:$GITLAB_CI_PUSH_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git | |
type: deploy | |
tags: | |
- k8s | |
rules: | |
- if: '$CI_BUILD_REF_NAME == "hotfix"' | |
when: manual | |
- when: never | |
# IF YOUR BRANCH IS PROTECTED USE FOLLOWING STAGE | |
Merge hotfix into master: | |
stage: merge-hotfix-to-master | |
needs: ["StageName"] | |
before_script: | |
- apk update && apk add --no-cache bash curl grep jq | |
variables: | |
GIT_STRATEGY: none | |
script: | |
- from=$CI_BUILD_REF_NAME | |
- to=master | |
- 'curl --header "PRIVATE-TOKEN: $GITLAB_CI_PUSH_TOKEN" -X POST "https://gitlab.yourdomain.com/api/v4/projects/$CI_PROJECT_ID/merge_requests?source_branch=$from&target_branch=$to&title=Merge%20Request%20Created%20by%20GitlabCI%20via%20Pipeline:%20$CI_PIPELINE_IID&target_project_id=$CI_PROJECT_ID"' | |
type: deploy | |
tags: | |
- k8s | |
rules: | |
- if: '$CI_BUILD_REF_NAME == "hotfix"' | |
when: manual | |
- when: never | |
# Auto create deployment on New Relic | |
# Add following curl to script section in your stage | |
#- 'http_response=$(curl --header "Api-Key: ${NEW_RELIC_API_KEY}" --header "Content-Type: application/json" -X POST -d "{\"deployment\":{ \"revision\": \"REVISION\", \"changelog\": \"Commit ID = $CI_COMMIT_SHORT_SHA\", \"description\": \"PIPELINE ID = $CI_PIPELINE_ID - QA\", \"user\": \"$GITLAB_USER_EMAIL\", \"timestamp\": \"$CI_JOB_STARTED_AT\" }}" "https://api.newrelic.com/v2/applications/${NEW_RELIC_APP_ID_PROD}/deployments.json")' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment