Last active
January 24, 2024 15:47
-
-
Save dakzilla/7e51748a2f03e038b6a4d6b122feaeed to your computer and use it in GitHub Desktop.
Shell script to upload a Composer package to Artifactory from Gitlab CI and tag it with a version number
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
#!/usr/bin/env bash | |
set -e | |
# Variables | |
COMPOSER_NAME="$(cat composer.json | jq -r '.name')" | |
PROJECT_NAME="$(echo ${COMPOSER_NAME} | cut -d'/' -f 2)" | |
BUILD_NAME="${PROJECT_NAME}-${CI_COMMIT_TAG}.zip" | |
ARTIFACTORY_REPOSITORY="php" | |
ARTIFACTORY_PATH="${ARTIFACTORY_REPOSITORY}/${COMPOSER_NAME}" | |
# Debug variables | |
echo "Composer name property: ${COMPOSER_NAME}" | |
echo "Composer project name: ${PROJECT_NAME}" | |
echo "Build artifact name: ${BUILD_NAME}" | |
echo "Artifactory repository path: ${ARTIFACTORY_PATH}" | |
# Build package archive from commit tag | |
git archive -o ${BUILD_NAME} ${CI_COMMIT_TAG} | |
# Check archive conformity | |
if [[ ! -f ${BUILD_NAME} ]] ; then | |
echo "Archive not found" | |
exit 1 | |
elif [[ ! -s ${BUILD_NAME} ]] ; then | |
echo "Archive empty or corrupted" | |
exit 1 | |
fi | |
# Upload archive to Artifactory | |
STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" -u "${ARTIFACTORY_CREDENTIALS}" "${ARTIFACTORY_URL}/${ARTIFACTORY_PATH}/${BUILD_NAME}" -T "${BUILD_NAME}") | |
# Debug Artifactory response | |
echo "Status code returned by Artifactory package push: ${STATUSCODE}" | |
if [[ ${STATUSCODE:0:1} =~ ^(4|5)$ ]] ; then | |
echo "Could not upload package to Artifactory" | |
exit 1 | |
fi | |
# Set package version in Artifactory | |
STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" -u "${ARTIFACTORY_CREDENTIALS}" -X PUT "${ARTIFACTORY_URL}/api/storage/${ARTIFACTORY_PATH}/${BUILD_NAME}?properties=composer.version=${CI_COMMIT_TAG}") | |
# Debug Artifactory response | |
echo "Status code returned by Artifactory package version set: ${STATUSCODE}" | |
if [[ ${STATUSCODE:0:1} =~ ^(4|5)$ ]] ; then | |
echo "Could not set Composer package version" | |
exit 1 | |
fi | |
# Final echo to prove that everything worked | |
echo "Composer package successfully uploaded to Artifactory" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment