From 22d1737a8de5d522696f37a007e168054cdbb09d Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 13 May 2023 18:16:33 +0100 Subject: [PATCH] first release --- Dockerfile | 14 ++++++++++++++ Makefile | 7 +++++++ README.md | 21 ++++++++++++++++++++- main.go | 17 ++++++++++++++--- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 Makefile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..decee32 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM alpine +RUN apk --no-cache add go ca-certificates +WORKDIR /app +ADD go.mod . +ADD main.go . +RUN go build main.go +RUN ls main + +FROM alpine +RUN apk --no-cache add ca-certificates +WORKDIR /app +COPY --from=0 /app/main /app/main + +ENTRYPOINT /app/main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dec5c57 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +TAG=4 + +build: + docker build -t lxsameer/woodpecker_mailer:$(TAG) . + +push: + docker push lxsameer/woodpecker_mailer:$(TAG) diff --git a/README.md b/README.md index a74a1b8..a01a4a7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # woodpecker_mailer -This is a very basic mailer plugin for woodpecker \ No newline at end of file +A mailer plugin for the Woodpecker CI. + +Example configuration: + +```yaml +pipeline: + Mail: + image: lxsameer/woodpecker_mailer:4 + settings: + from: ci@serene-lang.org + user: + from_secret: mail_user + password: + from_secret: mail_pass + to: ${CI_COMMIT_AUTHOR_EMAIL} + subject: "[${CI_REPO}] JOB #${CI_BUILD_NUMBER} - ${CI_PREV_STEP_STATUS}" + text: | + BUILD: ${CI_BUILD_LINK} + COMMIT: ${CI_COMMIT_LINK} +``` diff --git a/main.go b/main.go index f52118a..d05855f 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "net/mail" "net/smtp" "os" + "strings" ) // "Send As" information @@ -22,6 +23,14 @@ func main() { from_name = os.Getenv("PLUGIN_FROM_NAME") ) + if username == "" || password == "" || to == "" || subject == "" || body == "" { + panic("'user', 'password', 'to', 'subject', and 'text' are mandatory!") + } + + if username == "" || password == "" || to == "" || subject == "" || body == "" { + panic("'user', 'password', 'to', 'subject', and 'text' are mandatory!") + } + const gmail = "smtp.gmail.com:587" sanitizedBody := template.HTMLEscapeString(body) @@ -41,11 +50,10 @@ func main() { } from := mail.Address{Name: from_name, Address: from_addr} - rcpt := mail.Address{Name: "", Address: to} header := make(map[string]string) header["From"] = from.String() - header["To"] = rcpt.String() + header["To"] = to header["Subject"] = subject // Convert the header map to a string and attach the body to it @@ -56,10 +64,13 @@ func main() { message += "\r\n" + sanitizedBody + "\r\n" fmt.Println("Senting an email to user") - err = smtp.SendMail(gmail, auth, from_addr, []string{rcpt.Address}, []byte(message)) + + rcpts := strings.Split(to, ",") + err = smtp.SendMail(gmail, auth, from_addr, rcpts, []byte(message)) if err != nil { panic(err) } smtpConnection.Quit() + fmt.Println("Done") }