From d89d1a500ba1ea6abd9be218a7a85bc660af0a13 Mon Sep 17 00:00:00 2001
From: steel <mael.acier@ensiie.fr>
Date: Sun, 29 Sep 2024 20:47:21 +0200
Subject: [PATCH] initial version

---
 .gitlab-ci.yml        | 35 ++++++++++++++++++++++++++++++
 .vscode/settings.json |  5 +++++
 templates/deploy.yml  | 50 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .vscode/settings.json
 create mode 100644 templates/deploy.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..fabbb64
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,35 @@
+include:
+  # include the component located in the current project from the current SHA
+  - component: $CI_SERVER_FQDN/$CI_PROJECT_PATH/my-component@$CI_COMMIT_SHA
+    inputs:
+      stage: build
+
+stages: [build, test, release]
+
+# Check if `component job of my-component` is added.
+# This example job could also test that the included component works as expected.
+# You can inspect data generated by the component, use GitLab API endpoints, or third-party tools.
+ensure-job-added:
+  stage: test
+  image: badouralix/curl-jq
+  # Replace "component job of my-component" with the job name in your component.
+  script:
+    - |
+      route="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/pipelines/${CI_PIPELINE_ID}/jobs"
+      count=`curl --silent "$route" | jq 'map(select(.name | contains("deploy-dokku"))) | length'`
+      if [ "$count" != "1" ]; then
+        exit 1; else
+        echo "Component Job present"
+      fi
+
+# If the pipeline is for a new tag with a semantic version, and all previous jobs succeed,
+# create the release.
+create-release:
+  stage: release
+  image: registry.gitlab.com/gitlab-org/release-cli:latest
+  script: echo "Creating release $CI_COMMIT_TAG"
+  rules:
+    - if: $CI_COMMIT_TAG
+  release:
+    tag_name: $CI_COMMIT_TAG
+    description: "Release $CI_COMMIT_TAG of components repository $CI_PROJECT_PATH"
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..10edfd8
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,5 @@
+{
+    "yaml.schemas": {
+        "https://gitlab.com/gitlab-org/gitlab/-/raw/master/app/assets/javascripts/editor/schema/ci.json": "file:///home/steel/code/arise/components/dokku/templates/deploy.yml"
+    }
+}
\ No newline at end of file
diff --git a/templates/deploy.yml b/templates/deploy.yml
new file mode 100644
index 0000000..4ea206b
--- /dev/null
+++ b/templates/deploy.yml
@@ -0,0 +1,50 @@
+spec:
+  inputs:
+    stage:
+      default: deploy
+    project:
+      description: "Le nom du projet Dokku"
+    when:
+      default: manual
+    environment:
+      default: production
+    url:
+      default: ""
+    image:
+      description: "L'image Docker à déployer"
+      default: ""
+    remote:
+      description: "L'adresse SSH du serveur Dokku"
+      default: "ssh://dokku@deploy.runner.iiens.net"
+---
+.dokku:
+  stage: $[[ inputs.stage ]]
+  when: $[[ inputs.when ]]
+  image: ${CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX}/dokku/ci-docker-image
+  variables:
+    GIT_PUSH_FLAGS: --force
+    BRANCH: main
+    GIT_REMOTE_URL: $[[ inputs.remote ]]/$[[ inputs.project ]]
+    DEPLOY_DOCKER_IMAGE: $[[ inputs.image ]]
+  script:
+    - dokku-deploy
+  after_script:
+    - dokku-unlock
+
+deploy-prod:
+  extends: .dokku
+  environment:
+    name: $[[ inputs.environment ]]
+    url: $[[ inputs.url ]]
+    action: start
+    on_stop: stop-prod
+
+stop-deploy-dokku:
+  extends: .dokku-stop
+  environment:
+    name: $[[ inputs.environment ]]
+    url: $[[ inputs.url ]]
+    action: stop
+  script:
+    - setup-ssh
+    - ssh "$[[ inputs.remote ]]" -- ps:stop "$(parse-app-name)"
-- 
GitLab