2014-02-07 13:10:01 +03:00
|
|
|
package deploy
|
2014-02-11 22:23:15 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-08 09:08:35 +03:00
|
|
|
"github.com/drone/drone/plugin/condition"
|
2014-06-05 00:25:38 +03:00
|
|
|
"github.com/drone/drone/shared/build/buildfile"
|
2014-02-11 22:23:15 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Git struct {
|
|
|
|
Target string `yaml:"target,omitempty"`
|
|
|
|
Force bool `yaml:"force,omitempty"`
|
|
|
|
Branch string `yaml:"branch,omitempty"`
|
2014-09-08 09:08:35 +03:00
|
|
|
|
|
|
|
Condition *condition.Condition `yaml:"when,omitempty"`
|
2014-02-11 22:23:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Git) Write(f *buildfile.Buildfile) {
|
|
|
|
// get the current commit hash
|
|
|
|
f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)")
|
|
|
|
|
|
|
|
// set the git user and email based on the individual
|
|
|
|
// that made the commit.
|
|
|
|
f.WriteCmdSilent("git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')")
|
|
|
|
f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')")
|
|
|
|
|
|
|
|
// add target as a git remote
|
|
|
|
f.WriteCmd(fmt.Sprintf("git remote add deploy %s", g.Target))
|
|
|
|
|
2014-03-12 20:25:35 +03:00
|
|
|
destinationBranch := g.Branch
|
|
|
|
if destinationBranch == "" {
|
|
|
|
destinationBranch = "master"
|
|
|
|
}
|
|
|
|
|
2014-02-11 22:23:15 +03:00
|
|
|
switch g.Force {
|
|
|
|
case true:
|
|
|
|
// this is useful when the there are artifacts generated
|
|
|
|
// by the build script, such as less files converted to css,
|
|
|
|
// that need to be deployed to git remote.
|
|
|
|
f.WriteCmd(fmt.Sprintf("git add -A"))
|
|
|
|
f.WriteCmd(fmt.Sprintf("git commit -m 'add build artifacts'"))
|
2014-03-12 20:25:35 +03:00
|
|
|
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s --force", destinationBranch))
|
2014-02-11 22:23:15 +03:00
|
|
|
case false:
|
|
|
|
// otherwise we just do a standard git push
|
2014-03-12 20:25:35 +03:00
|
|
|
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", destinationBranch))
|
2014-02-11 22:23:15 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-08 09:08:35 +03:00
|
|
|
|
|
|
|
func (g *Git) GetCondition() *condition.Condition {
|
|
|
|
return g.Condition
|
|
|
|
}
|