From 0ede519b2e74ada0a9a669efea8df19e16f2b007 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sat, 3 Apr 2021 13:27:05 +0200
Subject: [PATCH] [ci] #388: Add image generation build automation

Introduce Dockerfile based on latest Python available
Install silkaj and make it available to the CLI

Manual triggered job to generate the image
Definition as an extensible job for future usages
Rename stage
---
 .gitlab-ci.yml | 28 +++++++++++++++++++++++++---
 Dockerfile     | 12 ++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)
 create mode 100644 Dockerfile

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7a6c0f35..95d719a2 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,7 +1,7 @@
 stages:
   - checks
   - tests
-  - publish
+  - package
   - coverage
 
 variables:
@@ -76,8 +76,30 @@ tests-3.9:
   variables:
     PYTHON_VERSION: "3.9"
 
+.container:
+  stage: package
+  tags: [docker]
+  image: docker:latest
+  services:
+  - docker:dind
+  script:
+    - docker login -u "gitlab-ci-token" -p "$CI_BUILD_TOKEN" $CI_REGISTRY
+    - docker build --pull -t "$CI_REGISTRY_IMAGE:$IMAGE_TAG" .
+    - docker login -u "gitlab-ci-token" -p "$CI_BUILD_TOKEN" $CI_REGISTRY
+    - docker tag "$CI_REGISTRY_IMAGE:$IMAGE_TAG" "$CI_REGISTRY_IMAGE:$IMAGE_TAG"
+    - docker push "$CI_REGISTRY_IMAGE:$IMAGE_TAG"
+    - docker tag "$CI_REGISTRY_IMAGE:$IMAGE_TAG" "$CI_REGISTRY_IMAGE:latest"
+    - docker push "$CI_REGISTRY_IMAGE:latest"
+
+container:
+  extends: .container
+  variables:
+    IMAGE_TAG: "dev"
+  rules:
+    - when: manual
+
 pypi_test:
-  stage: publish
+  stage: package
   rules:
     - if: $CI_COMMIT_TAG
       when: manual
@@ -86,7 +108,7 @@ pypi_test:
     - poetry publish --build --username $PYPI_TEST_LOGIN --password $PYPI_TEST_PASSWORD --repository pypi_test
 
 pypi:
-  stage: publish
+  stage: package
   rules:
     - if: $CI_COMMIT_TAG
       when: manual
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..366c80bd
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,12 @@
+FROM registry.duniter.org/docker/python3/poetry/3.9:latest
+
+# Install Silkaj
+RUN poetry install
+
+# Set up alias to directly get silkaj command
+# https://stackoverflow.com/a/3638886
+RUN printf '#!/bin/bash\npoetry run silkaj "$@"' > /usr/bin/silkaj && \
+    chmod +x /usr/bin/silkaj
+
+WORKDIR /silkaj
+CMD ["/usr/bin/silkaj"]
-- 
GitLab