Skip to content

Commit 481bfc5

Browse files
authored
Merge pull request #3 from eksrvb/feature/upload-asset-to-github
Feature/upload asset to GitHub
2 parents 6baccdf + fd2a013 commit 481bfc5

File tree

8 files changed

+75
-36
lines changed

8 files changed

+75
-36
lines changed

.github/workflows/Release.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ jobs:
1919
go-version: 1.15
2020
- name: Build
2121
run: go build -v
22-
- name: Upload a Build Artifact
23-
uses: actions/[email protected]
24-
with:
25-
path: awesome-ci
2622
- name: Create Release
27-
run: ./awesome-ci createRelease
23+
run: ./awesome-ci createRelease -uploadArtifacts awesome-ci
2824
env:
2925
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/branchPR.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ jobs:
1919
go-version: 1.15
2020
- name: Build
2121
run: go build -v
22-
- name: Upload a Build Artifact
23-
uses: actions/[email protected]
24-
with:
25-
path: awesome-ci
22+
- name: get files list
23+
run: ls -l
2624
- name: Dryrun dummy Release
2725
run: ./awesome-ci createRelease -patchLevel bugfix -dry-run
2826
env:

LICENSE

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
MIT License
22

3-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3+
Copyright (c) 2021 eksrvb
44

5-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
611

7-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

gitcontroller/github.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
8+
"log"
79
"net/http"
810
"os"
11+
"strings"
912
"time"
1013
)
1114

1215
type GitConfiguration struct {
13-
ApiUrl string
14-
Repository string
15-
AccessToken string
16+
ApiUrl string
17+
Repository string
18+
AccessToken string
19+
DefaultBranchName string
1620
}
1721

1822
type NewRelease struct {
@@ -54,18 +58,22 @@ func newGetRequest(endpoint string, token string) map[string]interface{} {
5458
return result
5559
}
5660

57-
func newPostRequest(endpoint string, token string, requestBody []byte) map[string]interface{} {
61+
func newPostRequest(endpoint string, token string, isFile bool, requestBody []byte) map[string]interface{} {
5862
timeout := time.Duration(5 * time.Second)
5963
client := http.Client{
6064
Timeout: timeout,
6165
}
6266

6367
request, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(requestBody))
64-
request.Header.Set("Accept", "application/vnd.github.v3+json")
65-
request.Header.Set("Authorization", "token "+token)
6668
if err != nil {
6769
fmt.Println("(newPostRequest) Error at building request: ", err)
6870
}
71+
if isFile {
72+
request.Header.Set("Content-Type", "application/octet-stream")
73+
} else {
74+
request.Header.Set("Accept", "application/vnd.github.v3+json")
75+
}
76+
request.Header.Set("Authorization", "token "+token)
6977

7078
resp, err := client.Do(request)
7179
if err != nil {
@@ -94,11 +102,11 @@ func github_getLatestReleaseVersion(conf GitConfiguration) string {
94102
return version
95103
}
96104

97-
func github_createNextGitHubRelease(conf GitConfiguration, newReleaseVersion string) {
105+
func github_createNextGitHubRelease(conf GitConfiguration, newReleaseVersion string, uploadArtifacts string) {
98106

99107
requestBody, err := json.Marshal(NewRelease{
100108
TagName: newReleaseVersion,
101-
TargetCommitish: "master",
109+
TargetCommitish: conf.DefaultBranchName,
102110
Name: "Release " + newReleaseVersion,
103111
Body: "",
104112
Draft: false,
@@ -109,11 +117,32 @@ func github_createNextGitHubRelease(conf GitConfiguration, newReleaseVersion str
109117
}
110118

111119
url := fmt.Sprintf("%s/repos/%s/releases", conf.ApiUrl, conf.Repository)
112-
result := newPostRequest(url, conf.AccessToken, requestBody)
120+
result := newPostRequest(url, conf.AccessToken, false, requestBody)
113121

114122
if result["name"] == "Release "+newReleaseVersion {
115123
fmt.Println("Release " + newReleaseVersion + " sucsessfully created")
116124
} else {
117125
fmt.Println("Somethin went worng at creating release!")
118126
}
127+
128+
if uploadArtifacts != "" {
129+
log.Printf("Uploading artifacts from: %s\n", uploadArtifacts)
130+
131+
file, err := ioutil.ReadFile(uploadArtifacts)
132+
if err != nil {
133+
log.Fatal(err)
134+
}
135+
136+
releaseFileName := uploadArtifacts[strings.LastIndex(uploadArtifacts, "/")+1:]
137+
138+
uploadUrl := fmt.Sprintf("%s", result["upload_url"])
139+
newUploadUrl := strings.Replace(uploadUrl, "{?name,label}", "?name="+releaseFileName, -1)
140+
resultTwo := newPostRequest(newUploadUrl, conf.AccessToken, true, file)
141+
142+
if resultTwo["name"] == releaseFileName {
143+
fmt.Println(releaseFileName + " sucsessfully uploaded")
144+
} else {
145+
fmt.Println("Somethin went worng at uploading release!")
146+
}
147+
}
119148
}

gitcontroller/initsenv.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ func determanEnvironment(environment string) GitConfiguration {
2121
switch environment {
2222
case "Github":
2323
return GitConfiguration{
24-
ApiUrl: os.Getenv("GITHUB_API_URL"),
25-
Repository: os.Getenv("GITHUB_REPOSITORY"),
26-
AccessToken: os.Getenv("GITHUB_TOKEN"),
24+
ApiUrl: os.Getenv("GITHUB_API_URL"),
25+
Repository: os.Getenv("GITHUB_REPOSITORY"),
26+
AccessToken: os.Getenv("GITHUB_TOKEN"),
27+
DefaultBranchName: getEnv("GIT_DEFAULT_BRANCH_NAME", "main", false),
2728
}
2829

2930
case "Custom":
@@ -40,7 +41,6 @@ func determanEnvironment(environment string) GitConfiguration {
4041

4142
// GetLatestReleaseVersion
4243
func GetLatestReleaseVersion(environment string) string {
43-
4444
switch environment {
4545
case "Github":
4646
conf := determanEnvironment(environment)
@@ -50,11 +50,10 @@ func GetLatestReleaseVersion(environment string) string {
5050
}
5151

5252
// GetLatestReleaseVersion
53-
func CreateNextGitHubRelease(environment string, newReleaseVersion string) {
54-
53+
func CreateNextGitHubRelease(environment string, newReleaseVersion string, uploadArtifacts string) {
5554
switch environment {
5655
case "Github":
5756
conf := determanEnvironment(environment)
58-
github_createNextGitHubRelease(conf, newReleaseVersion)
57+
github_createNextGitHubRelease(conf, newReleaseVersion, uploadArtifacts)
5958
}
6059
}

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
2020
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2121
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
2222
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
23+
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
2324
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
2425
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2526
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ var (
1313
)
1414

1515
type CreateReleaseSet struct {
16-
fs *flag.FlagSet
17-
version string
18-
patchLevel string
19-
publishNpm string
20-
dryRun bool
16+
fs *flag.FlagSet
17+
version string
18+
patchLevel string
19+
publishNpm string
20+
uploadArtifacts string
21+
dryRun bool
2122
}
2223

2324
type getBuildInfosSet struct {
@@ -35,6 +36,7 @@ func init() {
3536
createRelease.fs.StringVar(&createRelease.version, "version", "", "override version to Update")
3637
createRelease.fs.StringVar(&createRelease.patchLevel, "patchLevel", "bugfix", "predefine version to Update")
3738
createRelease.fs.StringVar(&createRelease.publishNpm, "publishNpm", "", "runs npm publish --tag <createdTag> with custom directory")
39+
createRelease.fs.StringVar(&createRelease.uploadArtifacts, "uploadArtifacts", "", "uploads atifacts to release (file)")
3840
createRelease.fs.BoolVar(&createRelease.dryRun, "dry-run", false, "make dry-run before writing version to Git")
3941

4042
// getNewReleaseVersion
@@ -54,7 +56,7 @@ func main() {
5456
switch os.Args[1] {
5557
case "createRelease":
5658
createRelease.fs.Parse(os.Args[2:])
57-
service.CreateRelease(cienv, &createRelease.version, &createRelease.patchLevel, &createRelease.dryRun, &createRelease.publishNpm)
59+
service.CreateRelease(cienv, &createRelease.version, &createRelease.patchLevel, &createRelease.dryRun, &createRelease.publishNpm, &createRelease.uploadArtifacts)
5860
case "getBuildInfos":
5961
getBuildInfos.fs.Parse(os.Args[2:])
6062
service.GetBuildInfos(cienv, &getBuildInfos.version, &getBuildInfos.patchLevel, &getBuildInfos.output)

service/createRelease.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func runcmd(cmd string, shell bool) string {
2828
return string(out)
2929
}
3030

31-
func CreateRelease(cienv *string, overrideVersion *string, getVersionIncrease *string, isDryRun *bool, publishNpm *string) {
31+
func CreateRelease(cienv *string, overrideVersion *string, getVersionIncrease *string, isDryRun *bool, publishNpm *string, uploadArtifacts *string) {
3232
var gitVersion string
3333
if *overrideVersion != "" {
3434
gitVersion = *overrideVersion
@@ -67,7 +67,7 @@ func CreateRelease(cienv *string, overrideVersion *string, getVersionIncrease *s
6767
} else {
6868
fmt.Printf("Old version: %s\n", gitVersion)
6969
fmt.Printf("Writing new release: %s\n", newVersion)
70-
gitcontroller.CreateNextGitHubRelease(*cienv, newVersion)
70+
gitcontroller.CreateNextGitHubRelease(*cienv, newVersion, *uploadArtifacts)
7171
}
7272

7373
if *publishNpm != "" {

0 commit comments

Comments
 (0)