Skip to content

Instantly share code, notes, and snippets.

@egonelbre
Created March 2, 2016 09:21
Show Gist options
  • Save egonelbre/b2ea0343398644624738 to your computer and use it in GitHub Desktop.
Save egonelbre/b2ea0343398644624738 to your computer and use it in GitHub Desktop.
Deploying to ElasticBeanstalk
FROM alpine:3.1
RUN apk add --update ca-certificates && rm -rf /var/cache/apk/*
ADD . /app
WORKDIR /app/
ENV DEVELOPMENT true
ENV PORT 80
EXPOSE 80
RUN ["chmod", "+x", "/app/.bin/run"]
CMD ["/app/.bin/run"]
// +build ignore
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
var ZIP *zip.Writer
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func run(name string, args ...string) error {
fmt.Println("> ", name, strings.Join(args, " "))
cmd := exec.Command(name, args...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
cmd.Env = append([]string{
"GOOS=linux",
"GOARCH=amd64",
"CGO_ENABLED=0",
}, os.Environ()...)
return cmd.Run()
}
func build() {
check(run("go", "build", "-v", "-o", filepath.Join(".bin", "run"), "."))
AddDir(".bin")
AddGlob("*.json")
AddGlob("Docker*")
AddGlob("LICENSE-*")
AddDir("client")
}
func main() {
os.Mkdir(".bin", 0755)
os.Mkdir(".deploy", 0755)
filename := fmt.Sprintf("%s.zip", time.Now().Format("2006-01-02-15-04"))
file, err := os.Create(filepath.Join(".deploy", filename))
check(err)
defer file.Close()
fmt.Println("Creating:", filename)
ZIP = zip.NewWriter(file)
build()
ZIP.Close()
}
// filename with forward slashes
func AddFile(filename string) {
fmt.Printf(" %-40s", filename)
defer fmt.Println("+")
file, err := os.Open(filepath.FromSlash(filename))
check(err)
defer file.Close()
w, err := ZIP.Create(filename)
check(err)
_, err = io.Copy(w, file)
check(err)
}
// glob with forward slashes
func AddGlob(glob string) {
fmt.Printf("G %v\n", glob)
matches, err := filepath.Glob(filepath.FromSlash(glob))
check(err)
for _, match := range matches {
AddFile(filepath.ToSlash(match))
}
}
// dir with forward slashes
func AddDir(dir string) {
fmt.Printf("D %v\n", dir)
check(filepath.Walk(filepath.FromSlash(dir),
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
AddFile(filepath.ToSlash(path))
return nil
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment