Automatic release with GitLab and SimpleRepos

Apr 27, 2019 by Thibault Debatty - 545 views

https://cylab.be/blog/23/automatic-release-with-gitlab-and-simplerepos

SimpleRepos is a simple file sharing website that allows to upload files using a REST API. Here is how to use it in conjunction with GitLab to automatically release your binaries...

Requirements

You will need a running web server (like one from DigitalOcean for example) and a GitLab project with a working Continus Integration (.gitlab-ci.yml).

Installation of SimpleRepos

On your webserver, download the latest release of SimpleRepos from https://download.cylab.be/simple-repos

wget https://download.cylab.be/simple-repos/simple-repos-0.0.7.zip

Extract the archive

unzip simple-repos-*.zip

Copy the example env file and modify it to fit your needs. Don't forget to modify the upload key!

cp env.example.prod .env
nano .env

Install composer dependencies:

composer install

Configure the database:

php artisan migrate

You're done!

Uploading files to SimpleRepos

SimpleRepos has a very simple REST API that allows to upload files. You can test it simply using curl on the command line:

export MY_TOKEN="upload_token_from_env_file"
curl "http://my.server.com/api/readme.md"  --request POST \\
  --header "Authorization: Bearer $MY_TOKEN" -F 'file=@./readme.md'

Integration with GitLab

You can now use GitLab CI to automatically upload your releases to your SimpleRepos server.

Here is an example snippet for your .gitlab-ci.yml:

stages:
  - test
  - build
  - release

release:
  stage: release
  # This runs only when we push new tags
  only:
    - tags
  image: debian:stretch-slim
  script:
    - apt-get update -yqq
    - apt-get install -yqq zip
    ## We first create a zip for this release
    - ZIP_FILE="MY_PROJECT-$CI_COMMIT_TAG.zip"
    - zip $ZIP_FILE -r MY_PROJECT
    - curl http://my.server.com/api/MY_PROJECT/$ZIP_FILE  \\
        --request POST --header \"Authorization: Bearer $UPLOAD_TOKEN\" \\
        -F \"file=@./$ZIP_FILE\"

It would not be safe to write your upload token ($UPLOAD_TOKEN in the example above) directly in your git repository, hence you now have to define an environment variable in GitLab:

  1. open your project's page in GitLab
  2. go to Settings > CI / CD
  3. click on 'Expand' next to 'Environment variables'
  4. define a variable called UPLOAD_TOKEN, with the value you defined in your env file

GitLab environment variables

Releasing

Now you can create and upload a release simply by creating a tag and pushing it to your GitLab repository:

git tag 0.0.1
git push --tags

This will automatically trigger the release script, that will build the zip and upload it to SimpleRepos...