refactoring

This commit is contained in:
Robert Kaussow 2020-09-21 09:15:27 +02:00
parent 57418eaed5
commit 731d2b8aa7
Signed by: xoxys
GPG Key ID: 65362AE74AF98B61
20 changed files with 759 additions and 669 deletions

317
.drone.jsonnet Normal file
View File

@ -0,0 +1,317 @@
local PipelineTest = {
kind: 'pipeline',
name: 'test',
platform: {
os: 'linux',
arch: 'amd64',
},
steps: [
{
name: 'staticcheck',
image: 'golang:1.14',
commands: [
'go run honnef.co/go/tools/cmd/staticcheck ./...',
],
volumes: [
{
name: 'gopath',
path: '/go',
},
],
},
{
name: 'lint',
image: 'golang:1.14',
commands: [
'go run golang.org/x/lint/golint -set_exit_status ./...',
],
volumes: [
{
name: 'gopath',
path: '/go',
},
],
},
{
name: 'vet',
image: 'golang:1.14',
commands: [
'go vet ./...',
],
volumes: [
{
name: 'gopath',
path: '/go',
},
],
},
{
name: 'test',
image: 'golang:1.14',
commands: [
'go test -cover ./...',
],
volumes: [
{
name: 'gopath',
path: '/go',
},
],
},
],
volumes: [
{
name: 'gopath',
temp: {},
},
],
trigger: {
ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'],
},
};
local PipelineBuildBinaries = {
kind: 'pipeline',
name: 'build-binaries',
platform: {
os: 'linux',
arch: 'amd64',
},
steps: [
{
name: 'build',
image: 'techknowlogick/xgo:go-1.14.x',
commands: [
'[ -z "${DRONE_TAG}" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}',
'mkdir -p release/',
"cd cmd/github-releases-notifier && xgo -ldflags \"-s -w -X main.version=$BUILD_VERSION\" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier .",
'mv /build/* /drone/src/release/',
],
},
{
name: 'executable',
image: 'alpine',
commands: [
'$(find release/ -executable -type f | grep github-releases-notifier-linux-amd64) --help',
],
},
{
name: 'compress',
image: 'alpine',
commands: [
'apk add upx',
'find release/ -maxdepth 1 -executable -type f -exec upx {} \\;',
'ls -lh release/',
],
},
{
name: 'checksum',
image: 'alpine',
commands: [
'cd release/ && sha256sum * > sha256sum.txt',
],
},
{
name: 'publish',
image: 'plugins/github-release',
settings: {
overwrite: true,
api_key: {
from_secret: 'github_token',
},
files: ['release/*'],
title: '${DRONE_TAG}',
note: 'CHANGELOG.md',
},
when: {
ref: [
'refs/tags/**',
],
},
},
],
depends_on: [
'test',
],
trigger: {
ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'],
},
};
local PipelineBuildContainer(arch='amd64') = {
kind: 'pipeline',
name: 'build-container-' + arch,
platform: {
os: 'linux',
arch: arch,
},
steps: [
{
name: 'build',
image: 'golang:1.14',
commands: [
'[ -z "${DRONE_TAG}" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}',
'go build -v -ldflags "-X main.version=$BUILD_VERSION" -a -tags netgo -o release/' + arch + '/github-releases-notifier ./cmd/github-releases-notifier',
],
},
{
name: 'dryrun',
image: 'plugins/docker:18-linux-' + arch,
settings: {
dry_run: true,
dockerfile: 'docker/Dockerfile.' + arch,
repo: 'thegeeklab/${DRONE_REPO_NAME}',
username: { from_secret: 'docker_username' },
password: { from_secret: 'docker_password' },
},
depends_on: ['build'],
when: {
ref: ['refs/pull/**'],
},
},
{
name: 'publish-dockerhub',
image: 'plugins/docker:18-linux-' + arch,
settings: {
auto_tag: true,
auto_tag_suffix: arch,
dockerfile: 'docker/Dockerfile.' + arch,
repo: 'thegeeklab/${DRONE_REPO_NAME}',
username: { from_secret: 'docker_username' },
password: { from_secret: 'docker_password' },
},
when: {
ref: ['refs/heads/master', 'refs/tags/**'],
},
depends_on: ['dryrun'],
},
{
name: 'publish-quay',
image: 'plugins/docker:18-linux-' + arch,
settings: {
auto_tag: true,
auto_tag_suffix: arch,
dockerfile: 'docker/Dockerfile.' + arch,
registry: 'quay.io',
repo: 'quay.io/thegeeklab/${DRONE_REPO_NAME}',
username: { from_secret: 'quay_username' },
password: { from_secret: 'quay_password' },
},
when: {
ref: ['refs/heads/master', 'refs/tags/**'],
},
depends_on: ['dryrun'],
},
],
depends_on: [
'test',
],
trigger: {
ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'],
},
};
local PipelineNotifications = {
kind: 'pipeline',
name: 'notifications',
platform: {
os: 'linux',
arch: 'amd64',
},
steps: [
{
image: 'plugins/manifest',
name: 'manifest-dockerhub',
settings: {
ignore_missing: true,
auto_tag: true,
username: { from_secret: 'docker_username' },
password: { from_secret: 'docker_password' },
spec: 'docker/manifest.tmpl',
},
when: {
status: ['success'],
},
},
{
image: 'plugins/manifest',
name: 'manifest-quay',
settings: {
ignore_missing: true,
auto_tag: true,
username: { from_secret: 'quay_username' },
password: { from_secret: 'quay_password' },
spec: 'docker/manifest-quay.tmpl',
},
when: {
status: ['success'],
},
},
{
name: 'pushrm-dockerhub',
image: 'chko/docker-pushrm:1',
environment: {
DOCKER_PASS: {
from_secret: 'docker_password',
},
DOCKER_USER: {
from_secret: 'docker_username',
},
PUSHRM_FILE: 'README.md',
PUSHRM_SHORT: 'GitHub release notification bot',
PUSHRM_TARGET: 'thegeeklab/${DRONE_REPO_NAME}',
},
when: {
status: ['success'],
},
},
{
name: 'pushrm-quay',
image: 'chko/docker-pushrm:1',
environment: {
APIKEY__QUAY_IO: {
from_secret: 'quay_token',
},
PUSHRM_FILE: 'README.md',
PUSHRM_TARGET: 'quay.io/thegeeklab/${DRONE_REPO_NAME}',
},
when: {
status: ['success'],
},
},
{
name: 'matrix',
image: 'plugins/matrix',
settings: {
homeserver: { from_secret: 'matrix_homeserver' },
roomid: { from_secret: 'matrix_roomid' },
template: 'Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}',
username: { from_secret: 'matrix_username' },
password: { from_secret: 'matrix_password' },
},
when: {
status: ['success', 'failure'],
},
},
],
depends_on: [
'build-binaries',
'build-container-amd64',
'build-container-arm',
'build-container-arm64',
],
trigger: {
ref: ['refs/heads/master', 'refs/tags/**'],
status: ['success', 'failure'],
},
};
[
PipelineTest,
PipelineBuildBinaries,
PipelineBuildContainer(arch='amd64'),
PipelineBuildContainer(arch='arm64'),
PipelineBuildContainer(arch='arm'),
PipelineNotifications,
]

View File

@ -1,297 +0,0 @@
def main(ctx):
before = testing()
stages = [
linux('amd64'),
linux('arm64'),
linux('arm'),
binaries([]),
]
after = notification()
for b in before:
for s in stages:
s['depends_on'].append(b['name'])
for s in stages:
for a in after:
a['depends_on'].append(s['name'])
return before + stages + after
def testing():
return [{
'kind': 'pipeline',
'type': 'docker',
'name': 'testing',
'platform': {
'os': 'linux',
'arch': 'amd64',
},
'steps': [
{
'name': 'vet',
'image': 'golang:1.12',
'commands': [
'go vet ./...'
],
'volumes': [
{
'name': 'gopath',
'path': '/go'
}
]
},
{
'name': 'test',
'image': 'golang:1.12',
'commands': [
'go test -cover ./...'
],
'volumes': [
{
'name': 'gopath',
'path': '/go'
}
]
}
],
'volumes': [
{
'name': 'gopath',
'temp': {}
}
],
'trigger': {
'ref': [
'refs/heads/master',
'refs/tags/**',
'refs/pull/**'
]
}
}]
def linux(arch):
return {
'kind': 'pipeline',
'type': 'docker',
'name': 'build-container-%s' % arch,
'platform': {
'os': 'linux',
'arch': arch,
},
'steps': [
{
'name': 'build',
'image': 'golang:1.12',
'environment': {
'CGO_ENABLED': '0',
},
'commands': [
'[ -z "${DRONE_TAG}" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}',
'go build -v -ldflags "-X main.Version=$BUILD_VERSION" -a -tags netgo -o release/%s/github-releases-notifier' % arch
],
},
{
'name': 'executable',
'image': 'golang:1.12',
'commands': [
'./release/%s/github-releases-notifier --help' % arch,
]
},
{
'name': 'dryrun',
'image': 'plugins/docker',
'settings': {
'dry_run': True,
'tags': arch,
'dockerfile': 'docker/Dockerfile.%s' % arch,
'repo': 'xoxys/github-releases-notifier',
'username': {
'from_secret': 'docker_username'
},
'password': {
'from_secret': 'docker_password'
}
},
'when': {
'event': [
'pull_request'
]
}
},
{
'name': 'publish',
'image': 'plugins/docker',
'settings': {
'auto_tag': True,
'auto_tag_suffix': arch,
'dockerfile': 'docker/Dockerfile.%s' % arch,
'repo': 'xoxys/github-releases-notifier',
'username': {
'from_secret': 'docker_username'
},
'password': {
'from_secret': 'docker_password'
}
},
'when': {
'event': {
'exclude': [
'pull_request'
]
}
}
}
],
'depends_on': [],
'trigger': {
'ref': [
'refs/heads/master',
'refs/tags/**',
'refs/pull/**'
]
}
}
def binaries(arch):
return {
'kind': 'pipeline',
'type': 'docker',
'name': 'build-binaries',
'steps': [
{
'name': 'build',
'image': 'techknowlogick/xgo:latest',
'commands': [
'[ -z "${DRONE_TAG}" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}',
'mkdir -p release/',
"xgo -ldflags \"-X main.Version=$BUILD_VERSION\" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier-$BUILD_VERSION .",
'cp /build/* release/'
]
},
{
'name': 'checksum',
'image': 'alpine',
'commands': [
'cd release/ && sha256sum * > sha256sum.txt',
],
},
{
'name': 'publish',
'image': 'plugins/github-release',
'settings': {
'overwrite': True,
'api_key': {
'from_secret': 'github_token'
},
'files': [ "release/*" ],
'title': '${DRONE_TAG}',
'note': 'CHANGELOG.md',
},
'when': {
'ref': [
'refs/tags/**'
]
}
}
],
'depends_on': [],
'trigger': {
'ref': [
'refs/heads/master',
'refs/tags/**',
'refs/pull/**'
]
}
}
def notification():
return [{
'kind': 'pipeline',
'type': 'docker',
'name': 'notification',
'steps': [
{
'name': 'manifest',
'image': 'plugins/manifest',
'settings': {
'auto_tag': True,
'username': {
'from_secret': 'docker_username'
},
'password': {
'from_secret': 'docker_password'
},
'spec': 'docker/manifest.tmpl',
'ignore_missing': 'true',
},
'when' : {
'status': [
'success',
]
},
},
{
'name': 'readme',
'image': 'sheogorath/readme-to-dockerhub',
'environment': {
'DOCKERHUB_USERNAME': {
'from_secret': 'docker_username'
},
'DOCKERHUB_PASSWORD': {
'from_secret': 'docker_password'
},
'DOCKERHUB_REPO_PREFIX': 'xoxys',
'DOCKERHUB_REPO_NAME': 'github-releases-notifier',
'README_PATH': 'README.md',
'SHORT_DESCRIPTION': 'Receive Slack notifications for new GitHub releases'
},
},
{
'name': 'microbadger',
'image': 'plugins/webhook',
'settings': {
'urls': {
'from_secret': 'microbadger_url'
}
},
'when' : {
'status': [
'success',
]
},
},
{
'name': 'matrix',
'image': 'plugins/matrix',
'settings': {
'homeserver': {
'from_secret': 'matrix_homeserver',
},
'password': {
'from_secret': 'matrix_password',
},
'roomid': {
'from_secret': 'matrix_roomid',
},
'template': 'Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}',
'username': {
'from_secret': 'matrix_username',
},
},
},
],
'depends_on': [],
'trigger': {
'ref': [
'refs/heads/master',
'refs/tags/**'
],
'status': [
'success',
'failure'
]
}
}]

View File

@ -1,15 +1,30 @@
--- ---
kind: pipeline kind: pipeline
type: docker name: test
name: testing
platform: platform:
os: linux os: linux
arch: amd64 arch: amd64
steps: steps:
- name: staticcheck
image: golang:1.14
commands:
- go run honnef.co/go/tools/cmd/staticcheck ./...
volumes:
- name: gopath
path: /go
- name: lint
image: golang:1.14
commands:
- go run golang.org/x/lint/golint -set_exit_status ./...
volumes:
- name: gopath
path: /go
- name: vet - name: vet
image: golang:1.12 image: golang:1.14
commands: commands:
- go vet ./... - go vet ./...
volumes: volumes:
@ -17,7 +32,7 @@ steps:
path: /go path: /go
- name: test - name: test
image: golang:1.12 image: golang:1.14
commands: commands:
- go test -cover ./... - go test -cover ./...
volumes: volumes:
@ -36,196 +51,6 @@ trigger:
--- ---
kind: pipeline kind: pipeline
type: docker
name: build-container-amd64
platform:
os: linux
arch: amd64
steps:
- name: build
image: golang:1.12
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.Version=$BUILD_VERSION" -a -tags netgo -o release/amd64/github-releases-notifier
environment:
CGO_ENABLED: 0
- name: executable
image: golang:1.12
commands:
- ./release/amd64/github-releases-notifier --help
- name: dryrun
image: plugins/docker
settings:
dockerfile: docker/Dockerfile.amd64
dry_run: true
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
tags: amd64
username:
from_secret: docker_username
when:
event:
- pull_request
- name: publish
image: plugins/docker
settings:
auto_tag: true
auto_tag_suffix: amd64
dockerfile: docker/Dockerfile.amd64
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
username:
from_secret: docker_username
when:
event:
exclude:
- pull_request
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- testing
---
kind: pipeline
type: docker
name: build-container-arm64
platform:
os: linux
arch: arm64
steps:
- name: build
image: golang:1.12
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.Version=$BUILD_VERSION" -a -tags netgo -o release/arm64/github-releases-notifier
environment:
CGO_ENABLED: 0
- name: executable
image: golang:1.12
commands:
- ./release/arm64/github-releases-notifier --help
- name: dryrun
image: plugins/docker
settings:
dockerfile: docker/Dockerfile.arm64
dry_run: true
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
tags: arm64
username:
from_secret: docker_username
when:
event:
- pull_request
- name: publish
image: plugins/docker
settings:
auto_tag: true
auto_tag_suffix: arm64
dockerfile: docker/Dockerfile.arm64
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
username:
from_secret: docker_username
when:
event:
exclude:
- pull_request
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- testing
---
kind: pipeline
type: docker
name: build-container-arm
platform:
os: linux
arch: arm
steps:
- name: build
image: golang:1.12
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.Version=$BUILD_VERSION" -a -tags netgo -o release/arm/github-releases-notifier
environment:
CGO_ENABLED: 0
- name: executable
image: golang:1.12
commands:
- ./release/arm/github-releases-notifier --help
- name: dryrun
image: plugins/docker
settings:
dockerfile: docker/Dockerfile.arm
dry_run: true
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
tags: arm
username:
from_secret: docker_username
when:
event:
- pull_request
- name: publish
image: plugins/docker
settings:
auto_tag: true
auto_tag_suffix: arm
dockerfile: docker/Dockerfile.arm
password:
from_secret: docker_password
repo: xoxys/github-releases-notifier
username:
from_secret: docker_username
when:
event:
exclude:
- pull_request
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- testing
---
kind: pipeline
type: docker
name: build-binaries name: build-binaries
platform: platform:
@ -234,12 +59,24 @@ platform:
steps: steps:
- name: build - name: build
image: techknowlogick/xgo:latest image: techknowlogick/xgo:go-1.14.x
commands: commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}" - "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- mkdir -p release/ - mkdir -p release/
- xgo -ldflags "-X main.Version=$BUILD_VERSION" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier-$BUILD_VERSION . - cd cmd/github-releases-notifier && xgo -ldflags "-s -w -X main.version=$BUILD_VERSION" -tags netgo -targets 'linux/amd64,linux/arm-6,linux/arm64' -out github-releases-notifier .
- cp /build/* release/ - mv /build/* /drone/src/release/
- name: executable
image: alpine
commands:
- $(find release/ -executable -type f | grep github-releases-notifier-linux-amd64) --help
- name: compress
image: alpine
commands:
- apk add upx
- find release/ -maxdepth 1 -executable -type f -exec upx {} \;
- ls -lh release/
- name: checksum - name: checksum
image: alpine image: alpine
@ -267,19 +104,249 @@ trigger:
- refs/pull/** - refs/pull/**
depends_on: depends_on:
- testing - test
--- ---
kind: pipeline kind: pipeline
type: docker name: build-container-amd64
name: notification
platform: platform:
os: linux os: linux
arch: amd64 arch: amd64
steps: steps:
- name: manifest - name: build
image: golang:1.14
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.version=$BUILD_VERSION" -a -tags netgo -o release/amd64/github-releases-notifier ./cmd/github-releases-notifier
- name: dryrun
image: plugins/docker:18-linux-amd64
settings:
dockerfile: docker/Dockerfile.amd64
dry_run: true
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/pull/**
depends_on:
- build
- name: publish-dockerhub
image: plugins/docker:18-linux-amd64
settings:
auto_tag: true
auto_tag_suffix: amd64
dockerfile: docker/Dockerfile.amd64
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
- name: publish-quay
image: plugins/docker:18-linux-amd64
settings:
auto_tag: true
auto_tag_suffix: amd64
dockerfile: docker/Dockerfile.amd64
password:
from_secret: quay_password
registry: quay.io
repo: quay.io/thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: quay_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- test
---
kind: pipeline
name: build-container-arm64
platform:
os: linux
arch: arm64
steps:
- name: build
image: golang:1.14
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.version=$BUILD_VERSION" -a -tags netgo -o release/arm64/github-releases-notifier ./cmd/github-releases-notifier
- name: dryrun
image: plugins/docker:18-linux-arm64
settings:
dockerfile: docker/Dockerfile.arm64
dry_run: true
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/pull/**
depends_on:
- build
- name: publish-dockerhub
image: plugins/docker:18-linux-arm64
settings:
auto_tag: true
auto_tag_suffix: arm64
dockerfile: docker/Dockerfile.arm64
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
- name: publish-quay
image: plugins/docker:18-linux-arm64
settings:
auto_tag: true
auto_tag_suffix: arm64
dockerfile: docker/Dockerfile.arm64
password:
from_secret: quay_password
registry: quay.io
repo: quay.io/thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: quay_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- test
---
kind: pipeline
name: build-container-arm
platform:
os: linux
arch: arm
steps:
- name: build
image: golang:1.14
commands:
- "[ -z \"${DRONE_TAG}\" ] && BUILD_VERSION=${DRONE_COMMIT_SHA:0:8} || BUILD_VERSION=${DRONE_TAG##v}"
- go build -v -ldflags "-X main.version=$BUILD_VERSION" -a -tags netgo -o release/arm/github-releases-notifier ./cmd/github-releases-notifier
- name: dryrun
image: plugins/docker:18-linux-arm
settings:
dockerfile: docker/Dockerfile.arm
dry_run: true
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/pull/**
depends_on:
- build
- name: publish-dockerhub
image: plugins/docker:18-linux-arm
settings:
auto_tag: true
auto_tag_suffix: arm
dockerfile: docker/Dockerfile.arm
password:
from_secret: docker_password
repo: thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: docker_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
- name: publish-quay
image: plugins/docker:18-linux-arm
settings:
auto_tag: true
auto_tag_suffix: arm
dockerfile: docker/Dockerfile.arm
password:
from_secret: quay_password
registry: quay.io
repo: quay.io/thegeeklab/${DRONE_REPO_NAME}
username:
from_secret: quay_username
when:
ref:
- refs/heads/master
- refs/tags/**
depends_on:
- dryrun
trigger:
ref:
- refs/heads/master
- refs/tags/**
- refs/pull/**
depends_on:
- test
---
kind: pipeline
name: notifications
platform:
os: linux
arch: amd64
steps:
- name: manifest-dockerhub
image: plugins/manifest image: plugins/manifest
settings: settings:
auto_tag: true auto_tag: true
@ -293,23 +360,41 @@ steps:
status: status:
- success - success
- name: readme - name: manifest-quay
image: sheogorath/readme-to-dockerhub image: plugins/manifest
environment:
DOCKERHUB_PASSWORD:
from_secret: docker_password
DOCKERHUB_REPO_NAME: github-releases-notifier
DOCKERHUB_REPO_PREFIX: xoxys
DOCKERHUB_USERNAME:
from_secret: docker_username
README_PATH: README.md
SHORT_DESCRIPTION: Receive Slack notifications for new GitHub releases
- name: microbadger
image: plugins/webhook
settings: settings:
urls: auto_tag: true
from_secret: microbadger_url ignore_missing: true
password:
from_secret: quay_password
spec: docker/manifest-quay.tmpl
username:
from_secret: quay_username
when:
status:
- success
- name: pushrm-dockerhub
image: chko/docker-pushrm:1
environment:
DOCKER_PASS:
from_secret: docker_password
DOCKER_USER:
from_secret: docker_username
PUSHRM_FILE: README.md
PUSHRM_SHORT: GitHub Comment - Drone plugin to add comments to GitHub Issues/PRs
PUSHRM_TARGET: thegeeklab/${DRONE_REPO_NAME}
when:
status:
- success
- name: pushrm-quay
image: chko/docker-pushrm:1
environment:
APIKEY__QUAY_IO:
from_secret: quay_token
PUSHRM_FILE: README.md
PUSHRM_TARGET: quay.io/thegeeklab/${DRONE_REPO_NAME}
when: when:
status: status:
- success - success
@ -326,6 +411,10 @@ steps:
template: "Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}" template: "Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}"
username: username:
from_secret: matrix_username from_secret: matrix_username
when:
status:
- success
- failure
trigger: trigger:
ref: ref:
@ -336,13 +425,13 @@ trigger:
- failure - failure
depends_on: depends_on:
- build-container-amd64
- build-container-arm64
- build-container-arm
- build-binaries - build-binaries
- build-container-amd64
- build-container-arm
- build-container-arm64
--- ---
kind: signature kind: signature
hmac: 982d7a70f6cd3914794b3d085b7f949e72445698d9d4af06323239f98cf83058 hmac: 7fd3a4a3d079dd575030424aa90c99900def6bd2a7af88449933143009e28d99
... ...

View File

@ -1,5 +0,0 @@
# This file is only used for local development
GITHUB_TOKEN=XXX
INTERVAL=1m
LOG_LEVEL=debug
SLACK_HOOK=https://hooks.slack.com/services/T02MTEVH7/BASDAHYRZ/XXX...

55
.github/settings.yml vendored Normal file
View File

@ -0,0 +1,55 @@
repository:
name: github-releases-notifier
description: GitHub release notification bot
topics: drone, drone-plugin
private: false
has_issues: true
has_wiki: false
has_downloads: true
default_branch: master
allow_squash_merge: true
allow_merge_commit: true
allow_rebase_merge: true
labels:
- name: bug
color: d73a4a
description: Something isn't working
- name: documentation
color: 0075ca
description: Improvements or additions to documentation
- name: duplicate
color: cfd3d7
description: This issue or pull request already exists
- name: enhancement
color: a2eeef
description: New feature or request
- name: good first issue
color: 7057ff
description: Good for newcomers
- name: help wanted
color: 008672
description: Extra attention is needed
- name: invalid
color: e4e669
description: This doesn't seem right
- name: question
color: d876e3
description: Further information is requested
- name: wontfix
color: ffffff
description: This will not be worked on
branches:
- name: master
protection:
required_pull_request_reviews: null
required_status_checks:
strict: true
contexts:
- continuous-integration/drone/pr
enforce_admins: null
restrictions: null

View File

@ -1,8 +1,3 @@
- FEATURE
- add env variable `GITHUB_REPOS`
- add env variable `IGNORE_PRE`
- BUGFIX
- add `required` flag for GitHub token and Slack hook
- INTERNAL - INTERNAL
- provide also binary releases - publish to dockerhub and quay
- switch to gomod instead of vendor folder - refactor project structure

22
LICENSE
View File

@ -1,21 +1,21 @@
MIT License MIT License
Copyright (c) 2020 xoxys Copyright (c) 2020 Robert Kaussow <mail@thegeeklab.de>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is furnished
furnished to do so, subject to the following conditions: to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice (including the next
copies or substantial portions of the Software. paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE.

View File

@ -1,46 +1,28 @@
# github-releases-notifier # github-releases-notifier
GitHub release notification bot
[![Build Status](https://cloud.drone.io/api/badges/xoxys/github-releases-notifier/status.svg)](https://cloud.drone.io/xoxys/github-releases-notifier) [![Build Status](https://img.shields.io/drone/build/thegeeklab/github-releases-notifier?logo=drone)](https://cloud.drone.io/thegeeklab/github-releases-notifier)
[![Go Report Card](https://goreportcard.com/badge/github.com/xoxys/github-releases-notifier)](https://goreportcard.com/report/github.com/xoxyscom/github-releases-notifier) [![Docker Hub](https://img.shields.io/badge/dockerhub-latest-blue.svg?logo=docker&logoColor=white)](https://hub.docker.com/r/thegeeklab/github-releases-notifier)
[![Docker Pulls](https://img.shields.io/docker/pulls/xoxys/github-releases-notifier.svg?maxAge=604800)](https://hub.docker.com/r/xoxys/github-releases-notifier) [![Quay.io](https://img.shields.io/badge/quay-latest-blue.svg?logo=docker&logoColor=white)](https://quay.io/repository/thegeeklab/github-releases-notifier)
[![Go Report Card](https://goreportcard.com/badge/github.com/thegeeklab/github-releases-notifier)](https://goreportcard.com/report/github.com/thegeeklabcom/github-releases-notifier)
[![Source: GitHub](https://img.shields.io/badge/source-github-blue.svg?logo=github&logoColor=white)](https://github.com/thegeeklab/github-releases-notifier)
[![License: MIT](https://img.shields.io/github/license/thegeeklab/github-releases-notifier)](<[LICENSE](https://github.com/thegeeklab/github-releases-notifier/blob/master/LICENSE)>)
Receive Slack notifications if a new release of your favorite software is available on GitHub. Receive Slack notifications if a new release of your favorite software is available on GitHub.
![screenshot.png](screenshot.png) ## Setup
### Watching repositories 1. Get a URL to send WebHooks to your Slack.
2. Get a token for scraping GitHub: [https://help.github.com/](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
To watch repositories simply add them to the list of arguments `-r=kubernetes/kubernetes -r=prometheus/prometheus` and so on. To watch repositories simply add them to the list of arguments e.g. `-r=kubernetes/kubernetes -r=prometheus/prometheus`.
### Deploying ### Docker
1. Get a URL to send WebHooks to your Slack from https://api.slack.com/incoming-webhooks.
2. Get a token for scraping GitHub: [https://help.github.com/](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line).
#### Docker
```Shell
docker run --rm \
-e GITHUB_TOKEN=XXX \
-e SLACK_HOOK=https://hooks.slack.com/... \
thegeeklab/github-releases-notifier -r=kubernetes/kubernetes
``` ```
docker run --rm -e GITHUB_TOKEN=XXX -e SLACK_HOOK=https://hooks.slack.com/... xoxys/github-releases-notifier -r=kubernetes/kubernetes
```
#### docker-compose
1. Change into the `deployments/` folder.
2. Open `docker-compose.yml`
3. Change the token in the environment section to the ones obtained above.
4. `docker-compose up`
#### Kubernetes
```bash
kubectl create secret generic github-releases-notifier \
--from-literal=github=XXX` \
--from-literal=slack=XXX
```
After creating the secret with your credentials you can apply the deployment:
`kubectl apply -f deployments/kubernetes.yml`
That's it.

View File

@ -11,6 +11,8 @@ import (
"github.com/go-kit/kit/log/level" "github.com/go-kit/kit/log/level"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/shurcooL/githubv4" "github.com/shurcooL/githubv4"
"github.com/thegeeklab/github-releases-notifier/internal/handler"
"github.com/thegeeklab/github-releases-notifier/internal/model"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -70,10 +72,10 @@ func main() {
client: githubv4.NewClient(client), client: githubv4.NewClient(client),
} }
releases := make(chan Repository) releases := make(chan model.Repository)
go checker.Run(c.Interval, c.Repositories, c.IgnorePre, releases) go checker.Run(c.Interval, c.Repositories, c.IgnorePre, releases)
slack := SlackSender{Hook: c.SlackHook} slack := handler.SlackSender{Hook: c.SlackHook}
level.Info(logger).Log("msg", "waiting for new releases") level.Info(logger).Log("msg", "waiting for new releases")
for repository := range releases { for repository := range releases {

View File

@ -9,6 +9,7 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level" "github.com/go-kit/kit/log/level"
"github.com/shurcooL/githubv4" "github.com/shurcooL/githubv4"
"github.com/thegeeklab/github-releases-notifier/internal/model"
) )
// Checker has a githubv4 client to run queries and also knows about // Checker has a githubv4 client to run queries and also knows about
@ -16,14 +17,14 @@ import (
type Checker struct { type Checker struct {
logger log.Logger logger log.Logger
client *githubv4.Client client *githubv4.Client
releases map[string]Repository releases map[string]model.Repository
} }
// Run the queries and comparisons for the given repositories in a given interval. // Run the queries and comparisons for the given repositories in a given interval.
func (c *Checker) Run(interval time.Duration, repositories []string, func (c *Checker) Run(interval time.Duration, repositories []string,
ignorePre bool, releases chan<- Repository) { ignorePre bool, releases chan<- model.Repository) {
if c.releases == nil { if c.releases == nil {
c.releases = make(map[string]Repository) c.releases = make(map[string]model.Repository)
} }
for { for {
@ -78,7 +79,7 @@ func (c *Checker) Run(interval time.Duration, repositories []string,
// This should be improved in the future to make batch requests for all watched repositories at once // This should be improved in the future to make batch requests for all watched repositories at once
// TODO: https://github.com/shurcooL/githubv4/issues/17 // TODO: https://github.com/shurcooL/githubv4/issues/17
func (c *Checker) query(owner, name string) (Repository, error) { func (c *Checker) query(owner, name string) (model.Repository, error) {
var query struct { var query struct {
Repository struct { Repository struct {
ID githubv4.ID ID githubv4.ID
@ -109,32 +110,32 @@ func (c *Checker) query(owner, name string) (Repository, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
if err := c.client.Query(ctx, &query, variables); err != nil { if err := c.client.Query(ctx, &query, variables); err != nil {
return Repository{}, err return model.Repository{}, err
} }
repositoryID, ok := query.Repository.ID.(string) repositoryID, ok := query.Repository.ID.(string)
if !ok { if !ok {
return Repository{}, fmt.Errorf("can't convert repository id to string: %v", query.Repository.ID) return model.Repository{}, fmt.Errorf("can't convert repository id to string: %v", query.Repository.ID)
} }
if len(query.Repository.Releases.Edges) == 0 { if len(query.Repository.Releases.Edges) == 0 {
return Repository{}, fmt.Errorf("can't find any releases for %s/%s", owner, name) return model.Repository{}, fmt.Errorf("can't find any releases for %s/%s", owner, name)
} }
latestRelease := query.Repository.Releases.Edges[0].Node latestRelease := query.Repository.Releases.Edges[0].Node
releaseID, ok := latestRelease.ID.(string) releaseID, ok := latestRelease.ID.(string)
if !ok { if !ok {
return Repository{}, fmt.Errorf("can't convert release id to string: %v", query.Repository.ID) return model.Repository{}, fmt.Errorf("can't convert release id to string: %v", query.Repository.ID)
} }
return Repository{ return model.Repository{
ID: repositoryID, ID: repositoryID,
Name: string(query.Repository.Name), Name: string(query.Repository.Name),
Owner: owner, Owner: owner,
Description: string(query.Repository.Description), Description: string(query.Repository.Description),
URL: *query.Repository.URL.URL, URL: *query.Repository.URL.URL,
Release: Release{ Release: model.Release{
ID: releaseID, ID: releaseID,
Name: string(latestRelease.Name), Name: string(latestRelease.Name),
Description: string(latestRelease.Description), Description: string(latestRelease.Description),

View File

@ -1,17 +0,0 @@
version: '2'
services:
github-releases-notifier:
restart: always
image: justwatch/github-releases-notifier
environment:
- GITHUB_TOKEN=XXX
- SLACK_HOOK=https://hooks.slack.com/services/T02MASDF7/B6WERHYRZ/XXX
command:
- '-r=golang/go'
- '-r=justwatchcom/elasticsearch_exporter'
- '-r=justwatchcom/gopass'
- '-r=justwatchcom/sql_exporter'
- '-r=kubernetes/minikube'
- '-r=prometheus/prometheus'
- '-r=shurcooL/githubql'

View File

@ -1,52 +0,0 @@
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: github-releases-notifier
spec:
replicas: 1
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
app: github-releases-notifier
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
containers:
- name: github-releases-notifier
image: justwatch/github-releases-notifier
env:
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: github-releases-notifier
key: github
- name: SLACK_HOOK
valueFrom:
secretKeyRef:
name: github-releases-notifier
key: slack
command:
- '/bin/github-releases-notifier'
args:
- '-r=golang/go'
- '-r=justwatchcom/elasticsearch_exporter'
- '-r=justwatchcom/gopass'
- '-r=justwatchcom/sql_exporter'
- '-r=kubernetes/minikube'
- '-r=prometheus/prometheus'
- '-r=shurcooL/githubql'
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 25m
memory: 64Mi
restartPolicy: Always

View File

@ -1,4 +1,10 @@
FROM alpine:3.10 FROM alpine:3.12
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>" \
org.label-schema.name="GitHub Release Notifier" \
org.label-schema.vendor="Robert Kaussow" \
org.label-schema.schema-version="1.0"
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
ADD release/amd64/github-releases-notifier /bin/ ADD release/amd64/github-releases-notifier /bin/

View File

@ -1,4 +1,10 @@
FROM alpine:3.10 FROM alpine:3.12
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>" \
org.label-schema.name="GitHub Release Notifier" \
org.label-schema.vendor="Robert Kaussow" \
org.label-schema.schema-version="1.0"
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
ADD release/arm/github-releases-notifier /bin/ ADD release/arm/github-releases-notifier /bin/

View File

@ -1,4 +1,10 @@
FROM alpine:3.10 FROM alpine:3.12
LABEL maintainer="Robert Kaussow <mail@thegeeklab.de>" \
org.label-schema.name="GitHub Release Notifier" \
org.label-schema.vendor="Robert Kaussow" \
org.label-schema.schema-version="1.0"
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
ADD release/arm64/github-releases-notifier /bin/ ADD release/arm64/github-releases-notifier /bin/

View File

@ -1,4 +1,4 @@
image: xoxys/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} image: thegeeklab/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}}
{{#if build.tags}} {{#if build.tags}}
tags: tags:
{{#each build.tags}} {{#each build.tags}}
@ -6,18 +6,18 @@ tags:
{{/each}} {{/each}}
{{/if}} {{/if}}
manifests: manifests:
- image: xoxys/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}amd64 - image: thegeeklab/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}amd64
platform: platform:
architecture: amd64 architecture: amd64
os: linux os: linux
- image: xoxys/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm64 - image: thegeeklab/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm64
platform: platform:
architecture: arm64 architecture: arm64
os: linux os: linux
variant: v8 variant: v8
- image: xoxys/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm - image: thegeeklab/github-releases-notifier:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}arm
platform: platform:
architecture: arm architecture: arm
os: linux os: linux

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/xoxys/github-releases-notifier module github.com/thegeeklab/github-releases-notifier
go 1.13 go 1.13

View File

@ -1,4 +1,4 @@
package main package handler
import ( import (
"bytes" "bytes"
@ -8,6 +8,8 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"time" "time"
"github.com/thegeeklab/github-releases-notifier/internal/model"
) )
// SlackSender has the hook to send slack notifications. // SlackSender has the hook to send slack notifications.
@ -22,7 +24,7 @@ type slackPayload struct {
} }
// Send a notification with a formatted message build from the repository. // Send a notification with a formatted message build from the repository.
func (s *SlackSender) Send(repository Repository) error { func (s *SlackSender) Send(repository model.Repository) error {
payload := slackPayload{ payload := slackPayload{
Username: "GitHub Releases", Username: "GitHub Releases",
IconEmoji: ":github:", IconEmoji: ":github:",

View File

@ -1,4 +1,4 @@
package main package model
import ( import (
"net/url" "net/url"

View File

@ -1,4 +1,4 @@
package main package model
import "net/url" import "net/url"