Files
rss-proxy/.gitlab-ci.yml
2025-03-03 22:23:24 +03:00

75 lines
2.3 KiB
YAML
Executable File

stages:
- test
- build
- pre_push
- push
test:
stage: test
image: python:3.11-alpine
script:
- pip install -q -r requirements.txt
- python -m unittest discover tests
only:
- main
build:
needs:
- test
stage: build
script:
- 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
before_script:
- apk add --no-cache curl jq
script:
- echo "Running container to test image"
- docker run -d --rm --name test_container $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG
- CONTAINER_IP=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' test_container)
- |
echo "Waiting for the application to start..."
sleep 10
if curl --fail http://$CONTAINER_IP:5050/health; then
echo "Healthcheck passed"
else
echo "Healthcheck failed, stopping push"
exit 1
fi
after_script:
- docker rm -f test_container
only:
- main
push:
needs:
- pre_push
stage: push
before_script:
- apk add --no-cache curl jq
script:
- |
CURRENT_IMAGE_HASH=$(docker inspect --format='{{if .RepoDigests}}{{index .RepoDigests 0}}{{else}}no_digest{{end}}' $CI_REGISTRY/$CI_PROJECT_PATH:$CI_COMMIT_REF_SLUG)
EXISTING_IMAGE_HASH=$(curl -s --header "PRIVATE-TOKEN: $CI_JOB_TOKEN" "https://gl.beaconborn.ru/api/v4/projects/$CI_PROJECT_ID/registry/repositories/1/tags/$CI_COMMIT_REF_SLUG" | jq -r '.digest')
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
docker push $CI_REGISTRY/$CI_PROJECT_PATH:latest
else
echo "Image is unchanged, skipping push."
fi
only:
- main