Created
September 18, 2023 09:16
-
-
Save oliworx/e84550dda4c5953189b94d36f002e58e to your computer and use it in GitHub Desktop.
How to semi-automate composer updated using GitLab CI
This file contains 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
stages: | |
- update-dependencies | |
- code-style | |
- build | |
- test | |
- deploy | |
composer-update: | |
stage: update-dependencies | |
only: | |
- schedules | |
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/composer:2.5.8 | |
script: | |
- "composer update --no-ansi --no-interaction --no-scripts --no-install --no-audit | |
--no-progress --ignore-platform-req=ext-* 2>&1 | tee composer-out.txt" | |
- composer bump | |
- git config --global user.email "some.email@example.com" | |
- git config --global user.name "Some Name" | |
- git config --global --add safe.directory $CI_PROJECT_DIR | |
- BRANCH_NAME="update-php-dpendencies-$(date '+%Y-%m-%d_%H-%M')" | |
- git checkout -b $BRANCH_NAME | |
- git add composer.json composer.lock | |
- (echo "Automated PHP dependency upgrades $(date '+%Y-%m-%d')" ; echo "";) | tee commit-message.txt | |
- | # extract list of updated packages and save to a file and a variable | |
UPGRADED_PACKAGES=$(cat composer-out.txt | \ | |
sed -n '/^Writing lock file/ q; p' | \ | |
sed -n '/Updating dependencies/,$ p' | \ | |
sed '1 d' | tee -a commit-message.txt | \ | |
sed 's/$/\\n/'| tr -d '\n'); # convert to a single line for json | |
- echo $UPGRADED_PACKAGES | |
- git commit -F commit-message.txt | |
- git push "https://${GITLAB_USER_LOGIN}:${GITLAB_ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:$BRANCH_NAME" | |
- | | |
BODY="{ | |
\"id\": ${CI_PROJECT_ID}, | |
\"description\": \"${UPGRADED_PACKAGES}\", | |
\"source_branch\": \"${BRANCH_NAME}\", | |
\"target_branch\": \"master\", | |
\"remove_source_branch\": true, | |
\"title\": \"Automated PHP dependency upgrades $(date '+%Y-%m-%d')\" | |
}"; | |
- | | |
curl --fail --no-progress-meter -X POST "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/merge_requests" \ | |
--header "PRIVATE-TOKEN:${GITLAB_ACCESS_TOKEN}" \ | |
--header "Content-Type: application/json" \ | |
--data "${BODY}" | |
syntax-check: | |
stage: code-style | |
image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/php:8.0.21-cli-alpine | |
except: | |
- schedules | |
script: | |
- find . -type f -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment