Skip to content

Instantly share code, notes, and snippets.

@donny-dont
Forked from bradrydzewski/build_latest.go
Last active April 14, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donny-dont/1eebf4095e80a6a46954cc9c21a0ba9d to your computer and use it in GitHub Desktop.
Save donny-dont/1eebf4095e80a6a46954cc9c21a0ba9d to your computer and use it in GitHub Desktop.
Drone nightly build
# Build
build:
image: golang:1.5
environment:
- GO15VENDOREXPERIMENT=1
- GOOS=linux
- GOARCH=amd64
- CGO_ENABLED=0
commands:
- go get
- go build
- go test
# Publish
publish:
docker:
# Your values go here
# The o-59/x syntax runs the script every x minutues offset by o.
#
# 5-59/15 would mean that the script runs at 5, 20, 35, and 50 after.
#
# All projects are assumed to be building master. If not make a new entry
# that uses --branch.
0-59/15 * * * * root drone-cron --repo foo/bar --server {{SERVER}} --token {{TOKEN}} >> /var/log/cron.log 2>&1
FROM ubuntu
# Add the files
ADD crontab.template \
drone-cron \
run.sh \
/bin/
# Give execution rights
RUN chmod +x /bin/run.sh
RUN chmod +x /bin/drone-cron
# Run the script
CMD /bin/run.sh
package main
import (
"flag"
"log"
"strings"
"github.com/drone/drone-go/drone"
)
var (
repo = flag.String("repo", "", "repository name (ie octocat/hello-world")
branch = flag.String("branch", "master", "repository branch (ie master)")
token = flag.String("token", "", "drone token")
server = flag.String("server", "", "drone server")
)
func main() {
flag.Parse()
var (
owner = strings.Split(*repo, "/")[0]
name = strings.Split(*repo, "/")[1]
)
client := drone.NewClientToken(*server, *token)
builds, err := client.BuildList(owner, name)
if err != nil {
log.Fatalf("Unable to get build list for %s. %s", name, err)
}
for _, build := range builds {
if build.Status == drone.StatusPending || build.Status == drone.StatusRunning {
log.Fatalf("Build is already in progress for %s.", name)
}
if build.Branch == *branch && build.Event == "push" {
forked, err := client.BuildFork(owner, name, build.Number)
if err != nil {
log.Fatalf("Unable to fork build %s/%s#%d. %s.", owner, name, build.Number, err)
}
log.Printf("Created new build for %s/%s@%s", owner, name, forked.Commit)
break
}
}
}
#!/bin/bash
set -ex
CRON_FILE=/etc/cron.d/drone-cron
CRON_LOG=/var/log/cron.log
# Create the crontab entry
sed -e s/{{SERVER}}/${DRONE_SERVER//\//\\/}/ \
-e s/{{TOKEN}}/${DRONE_TOKEN//\//\\/}/ \
/bin/crontab.template > $CRON_FILE
# Make it executable
chmod 0644 $CRON_FILE
# Create the log file
touch $CRON_LOG
# Run the cron command
cron && tail -f $CRON_LOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment