Skip to content

Commit ea3f0cb

Browse files
committed
I've added some context to the introduction.
1 parent b714276 commit ea3f0cb

File tree

1 file changed

+254
-0
lines changed

1 file changed

+254
-0
lines changed

snap/go-example.md

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
Snapcraft can be used to package and distribute Go applications in a way that
2+
enables convenient installation by users.
3+
4+
This is the work I needed to do.
5+
6+
The process of creating a snap for a Go application builds on standard
7+
Go packaging tools, making it possible to adapt or integrate an
8+
application's existing packaging into the snap building process.
9+
10+
## Getting started
11+
12+
Snaps are defined in a single `snapcraft.yaml` file placed in a
13+
`snap` folder at the root of your project. This YAML file describes
14+
the application, its dependencies and how it should be built.
15+
16+
The following example shows the entire `snapcraft.yaml` file for an existing project, [Woke](https://github.com/degville/woke-snap):
17+
18+
```yaml
19+
name: woke
20+
summary: Detect non-inclusive language in your source code
21+
description: |
22+
Creating an inclusive work environment is imperative to a healthy,
23+
supportive, and productive culture, and an environment where everyone
24+
feels welcome and included. woke is a text file analysis tool that finds
25+
places within your source code that contain non-inclusive language and
26+
suggests replacing them with more inclusive alternatives.
27+
version: git
28+
grade: stable
29+
base: core20
30+
31+
confinement: devmode
32+
33+
apps:
34+
woke:
35+
command: bin/woke
36+
plugs:
37+
- home
38+
parts:
39+
woke:
40+
plugin: go
41+
source-type: git
42+
source: https://github.com/get-woke/woke
43+
```
44+
45+
We'll break this file down into its components in the following sections.
46+
47+
### Metadata
48+
49+
The `snapcraft.yaml` file starts with a small amount of
50+
human-readable metadata, which is often already available in the project's
51+
own packaging metadata or `README.md` file. This data is used in the
52+
presentation of the application in the Snap Store.
53+
54+
```yaml
55+
name: woke
56+
summary: Detect non-inclusive language in your source code
57+
description: |
58+
Creating an inclusive work environment is imperative to a healthy,
59+
supportive, and productive culture, and an environment where everyone
60+
feels welcome and included. woke is a text file analysis tool that finds
61+
places within your source code that contain non-inclusive language and
62+
suggests replacing them with more inclusive alternatives.
63+
version: git
64+
```
65+
66+
The `name` must be unique in the Snap Store. Valid snap names consist of lower-case alphanumeric characters and hyphens. They cannot be all numbers and they also cannot start or end with a hyphen.
67+
68+
By specifying `git` for the version, the current git tag or commit will be used as the version string. Versions carry no semantic meaning in snaps.
69+
70+
The `summary` can not exceed 79 characters. You can use a chevron '>' in the `description` key to declare a multi-line description.
71+
72+
### Base
73+
74+
The base keyword declares which :term:`base snap` to use with the project.
75+
A base snap is a special kind of snap that provides a run-time environment
76+
alongside a minimal set of libraries that are common to most applications.
77+
78+
```yaml
79+
base: core20
80+
```
81+
82+
In this example, [core20](https://snapcraft.io/core20) is used as the base for snap building, and is based
83+
on [Ubuntu 20.04 LTS](http://releases.ubuntu.com/20.04/). See [Base snaps](/t/11198) for more details.
84+
85+
### Security model
86+
87+
Snaps are containerised to ensure more predictable application behaviour and
88+
greater security. The general level of access a snap has to the user's system
89+
depends on its level of confinement.
90+
91+
The next section of the `snapcraft.yaml` file describes the level of
92+
:term:`confinement` applied to the running application:
93+
94+
```yaml
95+
confinement: devmode
96+
```
97+
98+
It is best to start creating a snap with a confinement level that provides
99+
warnings for confinement issues instead of strictly applying confinement.
100+
This is done by specifying the `devmode` (developer mode) confinement value.
101+
When a snap is in devmode, runtime confinement violations will be allowed but
102+
reported. These can be reviewed by running `journalctl -xe`.
103+
104+
Because devmode is only intended for development, snaps must be set to strict
105+
confinement before they can be published as "stable" in the Snap Store.
106+
Once an application is working well in devmode, you can review confinement
107+
violations, add appropriate interfaces, and switch to strict confinement.
108+
109+
The above example will also work if you change the confinement from `devmode`
110+
to `strict`, as you would before a release.
111+
112+
### Parts
113+
114+
Parts define what sources are needed to build your application. Parts can be
115+
anything: programs, libraries, or other needed assets, but for this example,
116+
we only need to use one part for the *woke* source code:
117+
118+
```yaml
119+
parts:
120+
woke:
121+
plugin: go
122+
source-type: git
123+
source: https://github.com/get-woke/woke
124+
```
125+
126+
The `plugin` keyword is used to select a language or technology-specific
127+
plugin that knows how to perform the build steps for the project.
128+
In this example, the [go plugin](/t/7818) is used to
129+
automate the build of this project using the version of Go on the host system.
130+
131+
The `source` keyword points to the source code of the project, which
132+
can be a local directory or remote Git repository. In this case, it refers to
133+
the main project repository.
134+
135+
### Apps
136+
137+
Apps are the commands and services that the snap provides to users. Each key
138+
under `apps` is the name of a command or service that should be made
139+
available on users' systems.
140+
141+
```yaml
142+
apps:
143+
woke:
144+
command: bin/woke
145+
plugs:
146+
- home
147+
```
148+
149+
The `command` specifies the path to the binary to be run. This is resolved
150+
relative to the root of the snap contents.
151+
152+
If the command name matches the name of the snap specified in the top-level
153+
`name` keyword (see **Metadata** above), the binary file will be given the
154+
same name as the snap, as in this example.
155+
If the names differ, the binary file name will be prefixed with the snap name
156+
to avoid naming conflicts between installed snaps. An example of this would be
157+
`woke.some-command`.
158+
159+
The confinement of the snap, which was defined in the **Security model** section
160+
above, can be changed through a set of :term:`interfaces`. In this example,
161+
the `plugs` keyword is used to specify the interfaces that the snap needs
162+
to access.
163+
164+
### Building the snap
165+
166+
You can download the example repository with the following command:
167+
168+
```bash
169+
$ git clone https://github.com/degville/woke-snap
170+
```
171+
172+
After you have created the `snapcraft.yaml` file (which already exists
173+
in the above repository), you can build the snap by simply executing the
174+
`snapcraft` command in the project directory:
175+
176+
```bash
177+
$ snapcraft
178+
Launching a container.
179+
Waiting for container to be ready
180+
[...]
181+
Pulling woke
182+
+ snapcraftctl pull
183+
Cloning into '/root/parts/woke/src'...
184+
remote: Enumerating objects: 2723, done.
185+
remote: Counting objects: 100% (939/939), done.
186+
remote: Compressing objects: 100% (401/401), done.
187+
remote: Total 2723 (delta 697), reused 635 (delta 522), pack-reused 1784
188+
Receiving objects: 100% (2723/2723), 22.33 MiB | 2.88 MiB/s, done.
189+
Resolving deltas: 100% (1574/1574), done.
190+
Building woke
191+
+ snapcraftctl build
192+
+ go mod download
193+
+ go install -p 8 -ldflags -linkmode=external ./...
194+
Staging woke
195+
+ snapcraftctl stage
196+
Priming woke
197+
+ snapcraftctl prime
198+
Determining the version from the project repo (version: git).
199+
The version has been set to '0+git.f23bb0a-dirty'
200+
Snapping |
201+
Snapped woke_0+git.f23bb0a-dirty_multi.snap
202+
```
203+
204+
The resulting snap can be installed locally. This requires the `--dangerous` flag because the snap is not signed by the Snap Store. The `--devmode` flag acknowledges that you are installing an unconfined application:
205+
206+
```bash
207+
$ sudo snap install woke_*.snap --devmode --dangerous
208+
```
209+
210+
You can then try it out:
211+
212+
```bash
213+
$ woke -h
214+
```
215+
216+
Removing the snap is simple too:
217+
218+
```bash
219+
$ sudo snap remove woke
220+
```
221+
222+
## Publishing your snap
223+
224+
To share your snaps you need to publish them in the Snap Store. First, create an account on [the dashboard](https://dashboard.snapcraft.io/dev/account/). Here you can customise how your snaps are presented, review your uploads and control publishing.
225+
226+
You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps.
227+
228+
Make sure the `snapcraft` command is authenticated using the email address attached to your Snap Store account:
229+
230+
```
231+
$ snapcraft login
232+
```
233+
234+
### Reserve a name for your snap
235+
236+
You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on [dashboard.snapcraft.io](https://dashboard.snapcraft.io/register-snap/), or by running the following command:
237+
238+
```
239+
$ snapcraft register mygosnap
240+
```
241+
242+
Be sure to update the `name:` in your `snapcraft.yaml` to match this registered name, then run `snapcraft` again.
243+
244+
### Upload your snap
245+
246+
Use snapcraft to push the snap to the Snap Store.
247+
248+
```
249+
$ snapcraft upload --release=edge mygosnap_*.snap
250+
```
251+
252+
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and [turn on automatic builds](https://build.snapcraft.io) so any further commits automatically get released to edge, without requiring you to manually build locally.
253+
254+
Congratulations! You've just built and published your first Go snap. For a more in-depth overview of the snap building process, see [Creating a snap](/t/creating-a-snap/6799).

0 commit comments

Comments
 (0)