Skip to content

Commit

Permalink
tools/server, docker: use downloaded version of artifact
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Nov 24, 2021
1 parent 6562dfc commit 91106a1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
keys/*.pem
tools/docker/versions/*.tar.gz
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/bradleyfalzon/ghinstallation v1.1.1
github.com/cavaliercoder/grab v2.0.0+incompatible // indirect
github.com/google/go-github/v40 v40.0.0
github.com/mattermost/go-circleci v0.7.1
go.bug.st/serial v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/bradleyfalzon/ghinstallation v1.1.1 h1:pmBXkxgM1WeF8QYvDLT5kuQiHMcmf+X015GI0KM/E3I=
github.com/bradleyfalzon/ghinstallation v1.1.1/go.mod h1:vyCmHTciHx/uuyN82Zc3rXN3X2KTK8nUTCrTMwAhcug=
github.com/cavaliercoder/grab v2.0.0+incompatible h1:wZHbBQx56+Yxjx2TCGDcenhh3cJn7cCLMfkEPmySTSE=
github.com/cavaliercoder/grab v2.0.0+incompatible/go.mod h1:tTBkfNqSBfuMmMBFaO2phgyhdYhiZQ/+iXCZDzcDsMI=
github.com/creack/goselect v0.1.1 h1:tiSSgKE1eJtxs1h/VgGQWuXUP0YS4CDIFMp6vaI1ls0=
github.com/creack/goselect v0.1.1/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
12 changes: 6 additions & 6 deletions tools/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ RUN wget https://dl.google.com/go/go${GO_RELEASE}.linux-amd64.tar.gz && \
ENV PATH=${PATH}:/usr/local/go/bin

FROM tinygohci-base AS tinygohci-build
ENV TINYGO_RELEASE=0.19.0
ARG TINYGO_DOWNLOAD_URL=https://github.com/tinygo-org/tinygo/releases/download/v${TINYGO_RELEASE}/tinygo${TINYGO_RELEASE}.linux-amd64.tar.gz
RUN wget ${TINYGO_DOWNLOAD_URL} -O tinygo.zip && \
unzip tinygo.zip tinygo.linux-amd64.tar.gz && \
tar -xzf tinygo.linux-amd64.tar.gz -C /usr/local && \
rm tinygo.zip tinygo.linux-amd64.tar.gz
ENV TINYGO_RELEASE=0.21.0
ARG TINYGO_DOWNLOAD_SHA=1234

COPY versions/${TINYGO_DOWNLOAD_SHA}.tar.gz tinygo.linux-amd64.tar.gz
RUN tar -xzf tinygo.linux-amd64.tar.gz -C /usr/local && \
rm tinygo.linux-amd64.tar.gz
ENV PATH=${PATH}:/usr/local/tinygo/bin
RUN go get -d tinygo.org/x/drivers

Expand Down
1 change: 1 addition & 0 deletions tools/docker/versions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory is to hold downloaded version of the TinyGo executable.
57 changes: 54 additions & 3 deletions tools/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"net/http"

"github.com/cavaliercoder/grab"
"github.com/google/go-github/v40/github"
)

Expand Down Expand Up @@ -207,7 +208,14 @@ func processBuilds(builds chan *Build) {
}

log.Printf("Building docker image using TinyGo from %s\n", url)
err := buildDocker(url, build.sha)
err := downloadBinary(url, build.sha)
if err != nil {
log.Println(err)
build.failCheckSuite("binary download failed")
continue
}

err = buildDocker(build.sha)
if err != nil {
log.Println(err)
build.failCheckSuite("docker build failed")
Expand All @@ -233,8 +241,8 @@ func processBuilds(builds chan *Build) {

// buildDocker does the docker build for the binary download
// with this SHA.
func buildDocker(url, sha string) error {
buildarg := fmt.Sprintf("TINYGO_DOWNLOAD_URL=%s", url)
func buildDocker(sha string) error {
buildarg := fmt.Sprintf("TINYGO_DOWNLOAD_SHA=%s", sha)
buildtag := "tinygohci:" + sha[:7]
out, err := exec.Command("docker", "build",
"-t", buildtag,
Expand All @@ -249,6 +257,49 @@ func buildDocker(url, sha string) error {
return nil
}

// downloadBinary does the download for the binary build
// with this SHA.
func downloadBinary(url, sha string) error {
// check if the file is already downloaded for this sha
if !fileExists("tools/docker/versions/" + sha + ".tar.gz") {
log.Println("Downloading binary for", sha)

_, err := grab.Get("tinygo-latest.zip", url)
if err != nil {
return err
}

// unzip
_, err = exec.Command("unzip", "tinygo-latest.zip",
"tinygo.linux-amd64.tar.gz").CombinedOutput()
if err != nil {
return err
}

// move file
err = os.Rename("tinygo.linux-amd64.tar.gz", "tools/docker/versions/"+sha+".tar.gz")
if err != nil {
return err
}

err = os.Remove("tinygo-latest.zip")
if err != nil {
return err
}
}
return nil
}

// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func performCheckRun(cr *github.CheckRun, runID int64, buildsCh chan *Build) {
// do the retest here
url, err := getTinygoBinaryURLFromGH(runID)
Expand Down

0 comments on commit 91106a1

Please sign in to comment.