api/.gitlab-ci.yml

97 lines
2.6 KiB
YAML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

stages:
- test
- build
- deploy
# 1⃣ Tests Laravel
test:
stage: test
image: laravelsail/php84-composer
variables:
APP_ENV: testing
APP_KEY: base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
DB_CONNECTION: sqlite
DB_DATABASE: ":memory:"
before_script:
- apt-get update && apt-get install -y libicu-dev
- docker-php-ext-configure intl && docker-php-ext-install intl
- composer install --no-interaction --prefer-dist
script:
- php artisan test
rules:
- when: always
# 2⃣ Build + push image
docker-build-push:
stage: build
image: docker:24
services:
- docker:24-dind
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_TAG: "$CI_REGISTRY_IMAGE:${CI_COMMIT_SHORT_SHA}"
IMAGE_LATEST: "$CI_REGISTRY_IMAGE:latest"
before_script:
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
script:
- docker build -t "$IMAGE_TAG" -t "$IMAGE_LATEST" .
- docker push "$IMAGE_TAG"
- docker push "$IMAGE_LATEST"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
# 3⃣ Deploy via Portainer
deploy-to-portainer:
stage: deploy
image: alpine:latest
needs:
- docker-build-push
before_script:
- apk add --no-cache curl jq gettext
script:
- envsubst < docker-compose.prod.yml > docker-compose.rendered.yml
- |
STACKS_JSON=$(curl -s -H "X-API-Key: $PORTAINER_API_KEY" "$PORTAINER_URL/api/stacks")
STACK_ID=$(echo "$STACKS_JSON" | jq ".[] | select(.Name == \"$PORTAINER_STACK_NAME\") | .Id")
STACK_FILE_PATH="docker-compose.rendered.yml"
if [ -z "$STACK_ID" ]; then
JSON_PAYLOAD=$(jq -n \
--arg name "$PORTAINER_STACK_NAME" \
--rawfile content "$STACK_FILE_PATH" \
'{name: $name, stackFileContent: $content, prune: true}')
curl -s -f \
-X POST "$PORTAINER_URL/api/stacks/create/standalone/string?endpointId=$PORTAINER_ENDPOINT_ID" \
-H "X-API-Key: $PORTAINER_API_KEY" \
-H "Content-Type: application/json" \
--data "$JSON_PAYLOAD" \
> /dev/null
else
JSON_PAYLOAD=$(jq -n \
--arg id "$STACK_ID" \
--rawfile content "$STACK_FILE_PATH" \
'{id: ($id|tonumber), stackFileContent: $content, prune: true, pullImage: true}')
curl -s -f \
-X PUT "$PORTAINER_URL/api/stacks/$STACK_ID?endpointId=$PORTAINER_ENDPOINT_ID" \
-H "X-API-Key: $PORTAINER_API_KEY" \
-H "Content-Type: application/json" \
--data "$JSON_PAYLOAD" \
> /dev/null
fi
rules:
- if: '$CI_COMMIT_BRANCH == "main"'