Commit 02fe3188 authored by Éloïs's avatar Éloïs
Browse files

Merge branch 'DockerBuild' into '1.6'

Change Vagrant with docker for building Debian

See merge request nodes/typescript/duniter!1227
parents 7f9e2b52 6fc858b2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ vagrant/*.log
vagrant/duniter

# Releases
/work
*.deb
*.tar.gz
*.log
+71 −30
Original line number Diff line number Diff line
stages:
  - github-sync
  - build
  - test

before_script:
    - export NVM_DIR="$HOME/.nvm"
    - . "$NVM_DIR/nvm.sh"

  - releases
  - releases-page
push_to_github:
    stage: github-sync
    variables:
        GIT_STRATEGY: none
    tags:
        - redshift
    before_script:
        - ''
    script:
        - rm -rf ./*
        - rm -rf .git
@@ -26,31 +22,76 @@ push_to_github:
        - mv packed-refs-new packed-refs
        - bash -c "git push --force --mirror github 2>&1 | grep -v duniter-gitlab; echo $?"
        
enforce_readme:
    stage: github-sync
    variables:
        GIT_STRATEGY: none
build:
  stage: build
  tags:
    - redshift
  before_script:
        - ''
    - export NVM_DIR="$HOME/.nvm"
    - . "$NVM_DIR/nvm.sh"
  script:
      - rm -rf ./*
      - rm -rf .git
      - git clone $GITHUB_URL_AND_KEY .
      - git config --global user.email "contact@duniter.org"
      - git config --global user.name "Duniter"
      - git checkout master
      - cat .github/github_disclaimer.md > README.md.new
      - cat README.md >> README.md.new
      - mv README.md.new README.md
      - git commit -am "Enforce github readme"
      - git push origin master
    - yarn

test:
  stage: test
  tags:
    - redshift
  before_script:
    - export NVM_DIR="$HOME/.nvm"
    - . "$NVM_DIR/nvm.sh"
  script:
    - yarn
    - yarn test

releases:test:
  stage: releases
  image: duniter/release-builder:v1.0.1
  tags:
    - redshift-duniter-builder
  variables:
    DAY: $(date +%Y%m%d)
    HOUR: $(date +%H%M)
    SEC: $(date +%S)
  script:
    - bash "release/arch/linux/build-lin.sh" "$(date +%Y%m%d).$(date +%H%M).$(date +%S)"
  artifacts:
    paths:
      - work/bin/
    expire_in: 8h
  when: manual
  except:
    - tags
  

releases:
  stage: releases
  image: duniter/release-builder:v1.0.1
  tags:
    - redshift-duniter-builder
  script:
    - bash "release/arch/linux/build-lin.sh" "${CI_COMMIT_TAG#v}"
  artifacts:
    paths:
      - work/bin/duniter-desktop-${CI_COMMIT_TAG}-linux-x64.deb
      - work/bin/duniter-desktop-${CI_COMMIT_TAG}-linux-x64.tar.gz
      - work/bin/duniter-server-${CI_COMMIT_TAG}-linux-x64.deb
    expire_in: 8h
  when: manual
  only:
  - tags
  - master
    
releases-message:
  stage: releases-page
  image: tensorflow/tensorflow:latest-py3
  tags:
    - redshift-duniter-builder
  variables:
    JOB_ARTIFACTS: 'releases'
    EXPECTED_ARTIFACTS: '["work/bin/duniter-desktop-${CI_COMMIT_TAG}-linux-x64.deb","work/bin/duniter-desktop-${CI_COMMIT_TAG}-linux-x64.tar.gz","work/bin/duniter-server-${CI_COMMIT_TAG}-linux-x64.deb"]'
  script:
    - python3 .gitlab/releaser.py
  when: manual
  only:
  - tags
  - master
+9 −0
Original line number Diff line number Diff line
{{current_message}}

# Downloads
{% for artifact in artifacts %}
***
[{{artifact.icon}} {{artifact.name}}]({{artifact.url}})  
_{{artifact.size}}_
***
{% endfor %}

.gitlab/releaser.py

0 → 100644
+143 −0
Original line number Diff line number Diff line
#!/usr/bin/python3
'''
This module is meant to overload the release note in gitlab for the current project.
Expects to find in environment following variables:
  - CI_PROJECT_URL - Automatically set by gitlab-ci
  - CI_COMMIT_TAG - Automatically set by gitlab-ci
  - CI_PROJECT_ID - Automatically set by gitlab-ci
  - CI_COMMIT_TAG - Automatically set by gitlab-ci
  - RELEASER_TOKEN - Token used by technical user
  - JOB_ARTIFACTS - String containing job name containing all artifacts, to set manually
  - EXPECTED_ARTIFACTS - List containing all artifacts generated to set manually
'''

import math
import urllib.request
import urllib.error
import json
import os
import jinja2

def convert_size(size_bytes):
    '''Print proper size'''
    if size_bytes == 0:
        return '0B'
    size_name = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
    i = int(math.floor(math.log(size_bytes, 1024)))
    power = math.pow(1024, i)
    size = round(size_bytes / power, 2)
    return '%s %s' % (size, size_name[i])

def get_current_message():
    '''Get current release message'''
    ci_project_id = os.environ['CI_PROJECT_ID']
    ci_commit_tag = os.environ['CI_COMMIT_TAG']
    tag_url = 'https://git.duniter.org/api/v4/projects/'
    tag_url += ci_project_id
    tag_url += '/repository/tags/'
    tag_url += ci_commit_tag
    request = urllib.request.Request(tag_url)
    response = urllib.request.urlopen(request)
    response_data = response.read().decode()
    data = json.loads(response_data)
    if data['release'] is None:
        return False, ''
    else:
        return True, data['release']['description'].split('# Downloads')[0]

def build_artifact_url(artifact, source):
    '''Given an artifact name, builds the url to download it'''
    job_artifacts = os.environ['JOB_ARTIFACTS']
    ci_project_url = os.environ['CI_PROJECT_URL']
    ci_commit_tag = os.environ['CI_COMMIT_TAG']
    if source:
        source_url = ci_project_url
        source_url += '/repository/'
        source_url += ci_commit_tag
        source_url += '/archive.'
        source_url += artifact
        return source_url
    else:
        artifact_url = ci_project_url
        artifact_url += '/-/jobs/artifacts/'
        artifact_url += ci_commit_tag
        artifact_url += '/raw/'
        artifact_url += artifact
        artifact_url += '?job='
        artifact_url += job_artifacts
        return artifact_url

def get_artifact_weight(location):
    '''Retrieve size of artifacts'''
    size = os.path.getsize(location)
    return convert_size(int(size))


def build_compiled_message(current_message):
    '''Create a new release message using the release template'''

    expected_artifacts = os.environ['EXPECTED_ARTIFACTS']
    try:
        expected_artifacts = json.loads(expected_artifacts)
    except json.decoder.JSONDecodeError:
        print('CRITICAL EXPECTED_ARTIFACTS environment variable JSON probably malformed')
        print('CRITICAL Correct : \'["test_linux.txt","test_windows.txt"]\' ')
        print('CRITICAL Not Correct: "[\'test_linux.txt\',\'test_windows.txt\']" ')
        exit(1)
    artifacts_list = []
    for artifact in expected_artifacts:
        artifact_dict = {
            'name': artifact.split('/')[-1],
            'url': build_artifact_url(artifact, False),
            'size': get_artifact_weight(artifact),
            'icon': ':package:'
        }
        artifacts_list.append(artifact_dict)

    j2_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(
            os.path.dirname(os.path.abspath(__file__))
            ),
        trim_blocks=True
        )
    # pylint: disable=maybe-no-member
    template = j2_env.get_template('release_template.md')
    return template.render(
        current_message=current_message,
        artifacts=artifacts_list
    )


def send_compiled_message(exists_release, compiled_message):
    '''Send to gitlab new message'''
    releaser_token = os.environ['RELEASER_TOKEN']
    ci_project_id = os.environ['CI_PROJECT_ID']
    ci_commit_tag = os.environ['CI_COMMIT_TAG']
    release_url = 'https://git.duniter.org/api/v4/projects/'
    release_url += ci_project_id
    release_url += '/repository/tags/'
    release_url += ci_commit_tag
    release_url += '/release'
    if exists_release:
        # We need to send a PUT request
        method = 'PUT'
    else:
        # We need to send a POST request
        method = 'POST'
    send_data = {
        'tag_name':ci_commit_tag,
        'description':compiled_message
        }
    send_data_serialized = json.dumps(send_data).encode('utf-8')
    request = urllib.request.Request(release_url, data=send_data_serialized, method=method)
    request.add_header('Private-Token', releaser_token)
    request.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(request)

def main():
    '''Execute main scenario'''
    exists_release, current_message = get_current_message()
    compiled_message = build_compiled_message(current_message)
    send_compiled_message(exists_release, compiled_message)
    print('Artifacts uploaded successfully')
main()
+1 −1
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ mkdir -p duniter_release
cp -R ${SRC}/* duniter_release/

# Creating DEB packaging
mv duniter_release/release/arch/debian/package duniter-${ARCH}
mv duniter_release/release/extra/debian/package duniter-${ARCH}
mkdir -p duniter-${ARCH}/opt/duniter/
chmod 755 duniter-${ARCH}/DEBIAN/post*
chmod 755 duniter-${ARCH}/DEBIAN/pre*
Loading