Skip to content

Commit 4cbd99c

Browse files
authored
Merge pull request #33 from degville/coda-032
Coda 032
2 parents b714276 + 5c86e52 commit 4cbd99c

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed

snap/go-example.md

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