Skip to content

Commit b36b8f9

Browse files
authored
feat: golang release (#23)
Publishes a directory that is expected to be the output of `jsii-pacmak` (i.e `dist/go`) to the appropriate GitHub repo.
1 parent ec64e67 commit b36b8f9

5 files changed

Lines changed: 188 additions & 3 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2020 Amazon Web Services
189+
Copyright 2021 Amazon Web Services
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ structure is compatible with `jsii-pacmak`:
2121
- `dist/python/*.whl` - Python wheels
2222
- `dist/nuget/*.nupkg` - Nuget packages
2323
- `dist/java/**` - Maven artifacts in local repository structure
24+
- `dist/go/**/go.mod` - Go modules. Each subdirectory should have its own go.mod file.
2425

2526
Each publisher needs a set of environment variables with credentials as
2627
described below (`NPM_TOKEN`, `TWINE_PASSWORD` etc).
@@ -43,6 +44,7 @@ You can also execute individual publishers:
4344
* `jsii-release-nuget`
4445
* `jsii-release-npm`
4546
* `jsii-release-pypi`
47+
* `jsii-release-golang`
4648

4749
## npm
4850

@@ -115,7 +117,7 @@ $ gpg -a --export-secret-keys <fingerprint> > private.pem
115117
```
116118

117119
Now, either set `MAVEN_GPG_PRIVATE_KEY_FILE` to point to `private.pem` or
118-
export the private key to a single line where newlines are encoded as `\n`
120+
export the private key to a single line where newlines are encoded as `\n`
119121
and then assign it to `MAVEN_GPG_PRIVATE_KEY`:
120122

121123
```console
@@ -158,7 +160,7 @@ npx jsii-release-nuget [DIR]
158160

159161
* Set `NUGET_SERVER` to `https://nuget.pkg.github.com/(`org or user`).
160162
* Set `NUGET_API_KEY` to a token with write packages permissions.
161-
* Make sure the repository url in the project file matches the org or user used for the server
163+
* Make sure the repository url in the project file matches the org or user used for the server
162164

163165
## PyPI
164166

@@ -181,6 +183,33 @@ npx jsii-release-pypi [DIR]
181183
|`TWINE_REPOSITORY_URL`|Optional|The registry URL (defaults to Twine default)|
182184

183185

186+
## Golang
187+
188+
Pushes a directory of golang modules to a GitHub repository.
189+
190+
**Usage:**
191+
192+
```shell
193+
npx jsii-release-golang [DIR]
194+
```
195+
196+
`DIR` is a directory where the golang modules are located (default is `dist/go`). Each subdirectory inside it
197+
must be a go module and contain a go.mod file. This directory is pushed as is to the repository root, overriding existing files.
198+
199+
**Options (environment variables):**
200+
201+
|Option|Required|Description|
202+
|------|--------|-----------|
203+
|`GITHUB_TOKEN`|Required|[GitHub personal access token.](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)|
204+
|`VERSION`|Optional|Module version. Defaults to the value in the 'version' file of the module directory. Fails if it doesn't exist.|
205+
|`GITHUB_REPO`|Optional|GitHub repository to push to. Default is derived from the module name.|
206+
|`GIT_BRANCH`|Optional|Branch to push to. Defaults to 'main'.|
207+
|`GIT_USER_NAME`|Optional|Username to perform the commit with. Defaults to the global git user.name config. Fails it it doesn't exist.|
208+
|`GIT_USER_EMAIL`|Optional|Email to perform the commit with. Defaults to the global git user.email config. Fails it it doesn't exist.|
209+
|`GIT_COMMIT_MESSAGE`|Optional|The commit message. Defaults to 'chore(release): $VERSION'.|
210+
|`DRYRUN`|Set to "true" for a dry run.|
211+
212+
184213
## Roadmap
185214

186215
- [X] GitHub Support: Maven

bin/jsii-release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ release maven java
3131
release nuget dotnet
3232
release npm js
3333
release pypi python
34+
release golang go

bin/jsii-release-golang

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
###
5+
#
6+
# Pushes a directory of golang modules to a GitHub repository.
7+
#
8+
# Usage: ./jsii-release-golang [DIR]
9+
#
10+
# DIR is a directory where the golang modules are located (default is `dist/go`). Each subdirectory inside it must be a
11+
# go module and contain a go.mod file. This directory is pushed as is to the repository root, overriding existing files.
12+
#
13+
# GITHUB_TOKEN (required): Authentication token that allows to push and tag the repository.
14+
# VERSION (optional): Module version. Defaults to the value in the 'version' file of the module directory. Fails if it doesn't exist.
15+
# GITHUB_REPO (optional): GitHub repository to push to. Default is derived from the module name.
16+
# GIT_BRANCH (optional): Branch to push to. Defaults to 'main'.
17+
# GIT_USER_NAME (optional): Username to perform the commit with. Defaults to the global git user.name config. Fails it it doesn't exist.
18+
# GIT_USER_EMAIL (optional): Email to perform the commit with. Defaults to the global git user.email config. Fails it it doesn't exist.
19+
# GIT_COMMIT_MESSAGE (optional): The commit message. Defaults to 'chore(release): $VERSION'.
20+
# DRYRUN (optional): Set to "true" for a dry run
21+
###
22+
23+
dir=${1:-dist/go}
24+
GIT_BRANCH=${GIT_BRANCH:-main}
25+
26+
# remember the full path since we might be changing dirs later on
27+
dir="$(pwd)/${dir}"
28+
29+
error() { echo "$@"; exit 1; }
30+
31+
if ! command -v git &> /dev/null; then
32+
error "git must be available to run this script"
33+
fi
34+
35+
if [ -z "${GIT_USER_NAME:-}" ]; then
36+
GIT_USER_NAME=$(git config user.name) || error "Unable to detect username. either configure a global git user.name or pass GIT_USER_NAME env variable"
37+
fi
38+
39+
if [ -z "${GIT_USER_EMAIL:-}" ]; then
40+
GIT_USER_EMAIL=$(git config user.email) || error "Unable to detect user email. either configure a global git user.email or pass GIT_USER_EMAIL env variable"
41+
fi
42+
43+
dry_run=false
44+
if [[ ${DRYRUN:-"false"} = "true" ]]; then
45+
echo "==========================================="
46+
echo " 🏜️ DRY-RUN MODE 🏜️"
47+
echo "==========================================="
48+
dry_run=true
49+
fi
50+
51+
if [ ! -d ${dir} ]; then
52+
error "${dir} either doesnt exist or is not a directory"
53+
fi
54+
55+
if [ -z "${GITHUB_TOKEN:-}" ]; then
56+
error "GITHUB_TOKEN env variable is required"
57+
fi
58+
59+
go_mod=""
60+
versions=("")
61+
62+
# make sure each directory has a go.mod file
63+
for subdir in $(find ${dir} -mindepth 1 -maxdepth 1 -type d)
64+
do
65+
if [ ! -f ${subdir}/go.mod ]; then
66+
error "expected to find go.mod in ${subdir}. make sure you specify the root directory of all your modules."
67+
fi
68+
# pick one mod file to derive repository
69+
go_mod=${subdir}/go.mod
70+
71+
# collect versions if they exist. used both for validation and for default value.
72+
if [ -f ${subdir}/version ]; then
73+
version=$(cat ${subdir}/version)
74+
versions=(${versions[@]} ${version})
75+
fi
76+
77+
done
78+
79+
if [ ${go_mod} = "" ]; then
80+
error "No go modules found in ${dir}"
81+
fi
82+
83+
unique_versions=($(echo "${versions[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
84+
count_unique_version=$(echo "${#unique_versions[@]}")
85+
86+
if [ ${count_unique_version} -gt 1 ]; then
87+
error "multiple versions are not supported. please use a single version across all your modules."
88+
fi
89+
90+
if [ -z "${VERSION:-}" ]; then
91+
92+
if [ ${count_unique_version} -eq 0 ]; then
93+
error "No version files found. Please supply a version using the VERSION env variable."
94+
fi
95+
96+
# must be a single element at this point
97+
VERSION=${unique_versions[0]}
98+
fi
99+
100+
GIT_COMMIT_MESSAGE=${GIT_COMMIT_MESSAGE:-"chore(release): ${VERSION}"}
101+
102+
module_name=$(grep module ${go_mod} | cut -d ' ' -f 2)
103+
domain=$(echo ${module_name} | cut -d '/' -f 1)
104+
105+
if [ ${domain} != "github.com" ]; then
106+
# the way we clone the repo with the token is a GitHub thing.
107+
error "'${domain}' domain is not supported. Only GitHub repositories are allowed"
108+
fi
109+
110+
if [ -z "${GITHUB_REPO:-}" ]; then
111+
owner=$(echo ${module_name} | cut -d '/' -f 2)
112+
repo=$(echo ${module_name} | cut -d '/' -f 3)
113+
GITHUB_REPO=${owner}/${repo}
114+
fi
115+
116+
target_repo_dir=$(mktemp -d)/repo
117+
118+
echo "Cloning target repository ${GITHUB_REPO}"
119+
git clone https://${GITHUB_TOKEN}@github.com/${GITHUB_REPO}.git ${target_repo_dir}
120+
121+
pushd ${target_repo_dir}
122+
git config user.name ${GIT_USER_NAME}
123+
git config user.email ${GIT_USER_EMAIL}
124+
125+
# checkout or create
126+
git checkout ${GIT_BRANCH} || git checkout -b ${GIT_BRANCH}
127+
128+
for subdir in $(find $(pwd) -mindepth 1 -maxdepth 1 -type d)
129+
do
130+
if [ -f ${subdir}/go.mod ]; then
131+
# remove to account for deleted files or entire modules
132+
echo "Removing ${subdir}"
133+
rm -r ${subdir}
134+
fi
135+
done
136+
137+
echo "Copying go modules to repository root"
138+
cp -r ${dir}/* .
139+
140+
git add .
141+
git commit -m "${GIT_COMMIT_MESSAGE}"
142+
143+
tag_name=v${VERSION}
144+
git tag -a ${tag_name} -m $VERSION
145+
146+
if $dry_run; then
147+
echo "Will create a tag: ${tag_name}"
148+
echo "Will push to branch: ${GIT_BRANCH}"
149+
else
150+
git push origin ${tag_name}
151+
git push origin ${GIT_BRANCH}
152+
fi
153+
154+
popd

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"bin": {
99
"jsii-release": "bin/jsii-release",
10+
"jsii-release-golang": "bin/jsii-release-golang",
1011
"jsii-release-maven": "bin/jsii-release-maven",
1112
"jsii-release-npm": "bin/jsii-release-npm",
1213
"jsii-release-nuget": "bin/jsii-release-nuget",

0 commit comments

Comments
 (0)