aboutsummaryrefslogtreecommitdiffstats
path: root/posts/git_signed_releases.md
blob: a4529279f86183cbcc7a5af4c20add2bb7228fe2 (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
---
title: Creating Signed Releases of your Git Projects
published: 2018-06-08
modified: 2019-04-23
---

This post describes how to create a PGP signed software release from your Git
project.

### Setup

We want to use `tar.xz` archives, and not `zip` or `tar.gz`, for this to work
we need to add a little snippet to `${HOME}/.gitconfig`:

    [tar "tar.xz"]
            command = xz -c

Now, with that out of the way, you can put the following POSIX shell script in
`${HOME}/.local/bin/make_release`. Make sure you make it "executable" with 
`chmod 0755 ${HOME}/.local/bin/make_release`:

    #!/bin/sh
    PROJECT_NAME=$(basename "${PWD}")
    PROJECT_VERSION=${1}
    RELEASE_DIR="${PWD}/release"

    if [ -z "${1}" ]
    then
        # we take the last "tag" of the Git repository as version
        PROJECT_VERSION=$(git describe --abbrev=0 --tags)
        echo Version: "${PROJECT_VERSION}"
    fi

    mkdir -p "${RELEASE_DIR}"
    if [ -f "${RELEASE_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz" ]
    then
        echo "Version ${PROJECT_VERSION} already has a release!"
        exit 1
    fi

    git archive --prefix "${PROJECT_NAME}-${PROJECT_VERSION}/" "${PROJECT_VERSION}" -o "${RELEASE_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz"
    gpg2 --armor --detach-sign --yes "${RELEASE_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}.tar.xz"

### Creating a Release

Now, from your checked out repository you can run `make_release` and it will 
create (by default) a signed archive of the last (annotated) tag of the 
project. If you want to create a release of a specific tag, provide it as the 
first argument to `make_release`:

    $ mkdir tmp && cd tmp
    $ git clone https://git.tuxed.net/fkooman/php-yubitwee
    $ cd php-yubitwee
    $ make_release
    Version: 1.1.4

The following files are created:

    $ ls -l php-yubitwee-*
    -rw-rw-r--. 1 fkooman fkooman 8240 Jun  8 17:18 php-yubitwee-1.1.4.tar.xz
    -rw-rw-r--. 1 fkooman fkooman  833 Jun  8 17:18 php-yubitwee-1.1.4.tar.xz.asc

You can verify the signature:

    $ gpg2 --verify php-yubitwee-1.1.4.tar.xz.asc
    gpg: assuming signed data in 'php-yubitwee-1.1.4.tar.xz'
    gpg: Signature made Fri 08 Jun 2018 05:18:37 PM CEST
    gpg:                using RSA key 6237BAF1418A907DAA98EAA79C5EDD645A571EB2
    gpg: Good signature from "Fran├žois Kooman <fkooman@tuxed.net>" [ultimate]

Easy peasy ;-)

**UPDATE** (2018-06-09): the `git archive` command got a `--prefix` now as to
put the contents in a directory containing the name and version of the 
software.

**UPDATE** (2018-06-26): add the `--yes` flag to `gpg2` to avoid it asking 
to overwrite the signature file

**UPDATE** (2019-04-23): do not overwrite existing release(s)