-
Notifications
You must be signed in to change notification settings - Fork 0
96 lines (84 loc) · 3.6 KB
/
docker.yaml
File metadata and controls
96 lines (84 loc) · 3.6 KB
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
name: Build xsnippet-api Docker image
on:
# Triggered manually
workflow_dispatch:
inputs:
image_name:
description: 'The name of the resulting Docker image'
default: 'xsnippet/xsnippet-api'
release_arch:
description: 'The CPU architecture to use when choosing the xsnippet-api binary'
default: 'x86_64'
release_os:
description: 'The OS to use when choosing the xsnippet-api binary'
default: 'linux'
release_tag:
description: 'The release tag to use when choosing the xsnippet-api binary'
default: 'latest'
registry:
description: 'The Docker registry to push the image to'
type: choice
options:
- docker.io
registry_username:
description: 'The Docker registry user name'
default: 'xsnippetci'
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- id: release-params
name: Look up the Github release by tag/name and prepare a list of tags to be attached to the Docker image
shell: python
run: |
import json
import os
import urllib.error as e
import urllib.request as r
# look up the latest stable release (if any)
try:
latest_release = json.load(r.urlopen("https://api.github.com/repos/xsnippet/xsnippet-api/releases/latest"))
except e.HTTPError as err:
if err.code != 404:
raise
latest_release = None
# look up the release by tag first; if not found, try to look it up by release name instead
try:
release = json.load(r.urlopen("https://api.github.com/repos/xsnippet/xsnippet-api/releases/tags/${{ github.event.inputs.release_tag }}"))
except e.HTTPError as err:
if err.code != 404:
raise
release = json.load(r.urlopen("https://api.github.com/repos/xsnippet/xsnippet-api/releases/${{ github.event.inputs.release_tag }}"))
# The Docker image tags should include:
# 1) the release tag
tags = set([f"{release['tag_name']}"])
# 2) "latest", if this is the latest stable release
if latest_release and latest_release['tag_name'] == release['tag_name']:
tags.add('latest')
# 3) the release name, if it is different from the git tag
if release['tag_name'] != '${{ github.event.inputs.release_tag }}':
tags.add('${{ github.event.inputs.release_tag }}')
with open(os.environ["GITHUB_OUTPUT"], "at") as f:
f.write(f"release_tag={release['tag_name']}\n")
f.write(f"image_tags={','.join('${{ github.event.inputs.image_name }}:' + t for t in tags)}\n")
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v1
- name: Log into registry ${{ github.event.inputs.registry }}
uses: docker/login-action@v1
with:
registry: ${{ github.event.inputs.registry }}
username: ${{ github.event.inputs.registry_username }}
password: ${{ secrets.XSNIPPET_CI_DOCKER_HUB_PASSWORD }}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v2
with:
context: ./docker
push: true
tags: ${{ steps.release-params.outputs.image_tags }}
build-args: |
RELEASE_ARCH=${{ github.event.inputs.release_arch }}
RELEASE_OS=${{ github.event.inputs.release_os }}
RELEASE_TAG=${{ steps.release-params.outputs.release_tag }}