This commit is contained in:
2025-03-03 20:30:30 +03:00
parent 972e018b4b
commit a8a7402375

View File

@@ -1,7 +1,7 @@
image: docker:latest
stages:
- test
- build
- pre_push
- push
test:
@@ -13,27 +13,40 @@ test:
only:
- main
before_script:
- apk add --no-cache docker-cli curl jq
- |
until docker info; do
echo "Waiting for Docker to be ready..."
sleep 5
done
build:
needs:
- test
stage: build
script:
- docker info
- docker build -t $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG .
- docker tag $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_SHORT_SHA
- docker tag $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG $CI_REGISTRY/$CI_PROJECT_PATH:latest
only:
- main
pre_push:
needs:
- build
stage: pre_push
script:
- echo "Running container to test image"
- docker run -d --name test_container -p 5050:5050 $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG
- |
echo "Waiting for the application to start..."
sleep 10
if curl --fail http://localhost:5050/health; then
echo "Healthcheck passed"
else
echo "Healthcheck failed, stopping push"
exit 1
fi
- docker rm -f test_container
only:
- main
push:
needs:
- build
- pre_push
stage: push
script:
- |
@@ -42,7 +55,6 @@ push:
if [ "$CURRENT_IMAGE_HASH" != "$EXISTING_IMAGE_HASH" ] && [ "$CURRENT_IMAGE_HASH" != "no_digest" ]; then
echo "Image has changed, pushing to the registry..."
echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
docker push $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG
docker push $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_SHORT_SHA
@@ -50,21 +62,5 @@ push:
else
echo "Image is unchanged, skipping push."
fi
- echo "Running container to test image"
- docker run -d --name test_container -p 5050:5050 $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG
- |
echo "Waiting for the application to start..."
sleep 10
if curl --fail http://localhost:5050/health; then
echo "Healthcheck passed"
else
echo "Healthcheck failed, stopping push"
exit 1
fi
- docker rm -f test_container
only:
- main