summaryrefslogtreecommitdiff
path: root/posts/releasing-pypi-gitlab.md
blob: db0cc701535a0fa801c13bcb05c169df4cd664b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
title: releasing: gitlab to pypi
date: 2020-05-17

This post contains my notes for how I release a Python project from GitLab to PyPI.
I love hosting projects on GitLab since I can use the built-in runners to release automatically.

# table of contents

[TOC]

# first release

The very first release is necessarily a very manual process.
Once the project is on PyPI, subsequent releases can be automated.

## test packaging

First, you want to make sure that your source code is correct.
There are a lot of little regretable mistakes to be made.

- `python setup.py sdist bdist_wheel`
- open `dist\<package>.tar.gz`
- ensure that LICENSE is packaged
- ensure that VERSION is correct
- ensure that CHANGELOG is correct
- ensure that PKG-INFO is correct

## tag on GitLab

- New Tag
- Tag name should start with v, e.g. v0.1.0, v2020.05.0
- under release notes: `release [version](link-to-changelog)`

If you already have gitlab-ci setup to release on tag, it will try and surely fail.
That's OK for this initail release.
You can cancel it, if you think to.

## release on PyPI

Now we actually release to PyPI.

- `rm -rf dist`
- `python setup.py sdist bdist_wheel`
- `twine upload dist/*`

Horray!
Don't forget to log into PyPI and make other project maintainers into "owners".

# gitlab-ci

In this section we will set up gitlab-ci to automatically release everytime a new tag is made.

## add release to ci

In `.gitlab-ci.yml`:

```
twine:
  stage: deploy
  script:
    - python setup.py sdist bdist_wheel
    - twine upload dist/*
  artifacts:
    paths:
      - dist/*
  only:
    - tags
```

## protect tags

Within the GitLab repository, we must mark tags that we intend to release as protected.
This is intended as a safety feature so that only maintainers are able to release.
On GitLab:

- Settings > Repository
- Protected Tags
- Create wildcard `v*`
- Allowed to create: Maintainers
- Protect

## add variables

PyPI allows uploads via tokens.
This is how we will authenticate and let PyPI know that we are who we say we are.
First, we need to generate the token.
On PyPI:

- Account
- API tokens
- Create token `gitlab-<project>`, scope only for project

On GitLab:

- Settings > CI/CD
- Variables > Add Variable
- (make sure variables are protected)
- key `TWINE_PASSWORD` value `pypi-<TOKEN>`
- key `TWINE_USERNAME` value `__token__`

# subsequent releases

On subsequent releases gitlab should release for you on tag.

- update CHANGELOG
- update VERSION
- tag
- watch ci release for you