I’m a junior web dev working on a project.
I have to work on gitlab-ci.yml to automatic create a merge request when a specific branch is updated by a commit.
I found a script here but being junior i can’t find why it doesn’t work.
I made those changes :
#!/usr/bin/env bash
# Extract the host where the server is running, and add the URL to the APIs
[[ $CI_PROJECT_URL =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"
# Look which is the default branch
TARGET_BRANCH=`curl --silent "${HOST}${CI_PROJECT_ID}" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" | python3 -c "import sys, json; print(json.load(sys.stdin)['default_branch'])"`;
# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"remove_source_branch\": false,
\"title\": \"WIP: ${CI_COMMIT_REF_NAME}\",
\"assignee_id\":\"${GITLAB_USER_ID}\"
}";
# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
--header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
--header "Content-Type: application/json" \
--data "${BODY}";
echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_NAME} and assigned to you";
exit;
fi
echo “No new merge request opened”;
I updated my gitlab-ci.yml with that :
merge-translation:
stage: merge-translation
rules:
- if: ‘$CI_COMMIT_BRANCH == “traduction-recette”’
when: always
- when: never
script:
- echo “Test de lacement du script”
- ./merge-traduction.sh
But when i push a change on my branch “traduction-recette”, no job is launched…
I explain you what’s the purpose of that : I work on a project we are translating using weblate.
Our translators add translations then push on the “traduction-recette” branch.
To see their job, i must create a job which allow to create a merge request from traduction-recette to devel (which is branch of our server).
That MR should be create when traduction-recette receive a commit (filtered on commit message or not, anyway).
But with the actual code i gave mentioned, no job is started when i push something on traduction-recette…
I ddin’t insert all our .gitlab-ci.yml (yes the . is on my file) : There is beginning of file :
variables:
SENTRY_ORG: “sentry”
SENTRY_PROJECT: “plateforme-api”
SENTRY_URL: “https://sentry.lde.fr/”
stages:
- merge-translation
- lint
- test
- deploy
- pages
lint:
stage: lint
only:
- merge_requests
except:
refs:
- traduction-recette # Inutile de passer le linter sur ce qui est modifié par Weblate (fichiers po et mo)
script:
- poetry install
- poetry run black . --check -l 120 --exclude=frontend
- poetry run flake8 . --statistics --show-source --exclude="*/migrations/*,frontend/*" --ignore="E126, E131, W503, W605, F405, F403, E722, E999" --max-line-length=120
tags:
- poplab
And end of file :
test:
stage: test
only:
- devel
except:
refs:
- traduction-recette # Inutile de lancer les tests sur ce qui est modifié par Weblate (fichiers po et mo)
script:
- >
git submodule sync --recursive
&& git submodule update --init --recursive --force
&& poetry install
&& poetry run ./manage.py collectstatic
&& poetry run pytest --ds=projet.test_settings -m “not integration” -n auto --cov=backend
coverage: ‘/TOTAL.*\s+(\d+%)$/’
tags:
- poplab
I resolved my question. It seems that a merge request can’t be automatically created in an effective way. So i’ve choosen to directly merge traduction-recette into recette just adding the Weblate deploy key on my gitlab repository settings.