From 4ba2e16b2c0f0c019da10c4d27ca361fcd4e7371 Mon Sep 17 00:00:00 2001 From: Buk Bukowski <38087302+bukowa@users.noreply.github.com> Date: Thu, 13 Oct 2022 19:39:23 +0200 Subject: [PATCH] chore(ci): add new ci jobs for docker builds & changelogs (#19) --- .dockerignore | 3 + .github/dependabot.yml | 4 +- .github/workflows/changelog.yaml | 46 +++++++++ .github/workflows/docker.yaml | 126 +++++++++++++++++++++++++ .github/workflows/test-applicaton.yaml | 36 ------- .github/workflows/test.yaml | 34 +++++++ Dockerfile | 4 + Makefile | 18 ++++ cliff.toml | 68 +++++++++++++ scripts/generate-changelog.sh | 18 ++++ test.sh | 2 +- 11 files changed, 320 insertions(+), 39 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/changelog.yaml create mode 100644 .github/workflows/docker.yaml delete mode 100644 .github/workflows/test-applicaton.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 Makefile create mode 100644 cliff.toml create mode 100755 scripts/generate-changelog.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..073481f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +* +!main.go +!go.mod diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 309b998..2bbfda4 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -3,8 +3,8 @@ updates: - package-ecosystem: "github-actions" directory: "/" schedule: - interval: "monthly" + interval: "weekly" - package-ecosystem: "docker" directory: "/" schedule: - interval: "monthly" + interval: "weekly" diff --git a/.github/workflows/changelog.yaml b/.github/workflows/changelog.yaml new file mode 100644 index 0000000..be50215 --- /dev/null +++ b/.github/workflows/changelog.yaml @@ -0,0 +1,46 @@ +name: Detect Changes + +# todo change branch main +on: + push: + branches: + - "main" + +jobs: + + detect-changes: + runs-on: ubuntu-latest + + steps: + + - + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + # https://github.com/tj-actions/changed-files/blob/main/README.md#inputs + - + name: Detect Changes + uses: tj-actions/changed-files@v32.1.0 + id: changes + with: + files: | + *.go + *.mod + *Dockerfile + + # Makefile + - + name: Generate Changelog + run: make generate-changelog-ci + if: steps.changes.outputs.any_changed == 'true' + + # https://github.com/peter-evans/create-pull-request#action-inputs + - + name: Create Pull Request + uses: peter-evans/create-pull-request@v4 + with: + token: "${{ secrets.GITHUB_TOKEN }}" + branch: "change/${{ github.sha }}" + commit-message: "new changes" + title: "maybe new release?" diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..e62f0be --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,126 @@ +name: Build and Push Docker Images + +on: + push: + branches: + - "*" + tags: + - "*" + +jobs: + + Docker: + name: Build and Push Docker Images + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + + steps: + + - + name: Checkout + uses: actions/checkout@v3 + + - + name: Set up Docker Build + uses: docker/setup-buildx-action@v2 + + - + name: Build Docker Test Image + uses: docker/build-push-action@v3 + id: build + with: + context: . + load: true + cache-from: type=gha + cache-to: type=gha,mode=max + + - + name: Test Docker Image + run: | + CMD="docker run --rm -i -p 9001:9001 ${{ steps.build.outputs.imageid }} --port=9001" ./test.sh + + - name: Get Docker Image Metadata + id: meta + uses: docker/metadata-action@v4 + with: + images: | + quay.io/k8start/http-headers + ghcr.io/bukowa/http-headers + bukowa/http-headers + tags: | + type=schedule + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=ref,event=branch + type=ref,event=pr + + - name: Set up QEMU + id: qemu + uses: docker/setup-qemu-action@v2 + + - name: Login to Docker Hub Container Registry + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_ROBOT_TOKEN }} + + - name: Login to GHCR.io Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Login to Quay.io Container Registry + uses: docker/login-action@v2 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_ROBOT_TOKEN }} + +# - name: Get Docker Expire Image Metadata +# id: meta-expire +# uses: docker/metadata-action@v4 +# with: +# images: | +# quay.io/k8start/http-headers +# tags: | +# type=sha + +# - +# # Push images that expire +# name: Build and Push Docker Expire Images +# uses: docker/build-push-action@v3 +# with: +# context: . +# push: true +# tags: ${{ steps.meta-expire.outputs.tags }} +# labels: | +# quay.expires-after=72h +# ${{ steps.meta-expire.outputs.labels }} +# cache-from: | +# type=gha +# type=registry,ref=quay.io/k8start/http-headers +# cache-to: | +# type=gha,mode=max +# platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x + + - + # Push other images + name: Build and Push Docker Images + uses: docker/build-push-action@v3 + id: build-all + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: | + ${{ steps.meta.outputs.labels }} + cache-from: | + type=gha + type=registry,ref=quay.io/k8start/http-headers + cache-to: | + type=gha,mode=max + platforms: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x diff --git a/.github/workflows/test-applicaton.yaml b/.github/workflows/test-applicaton.yaml deleted file mode 100644 index db1794e..0000000 --- a/.github/workflows/test-applicaton.yaml +++ /dev/null @@ -1,36 +0,0 @@ -name: Build & Test Application in Docker - -on: - pull_request: - paths: - - "Dockerfile" - - "main.go" - - "test.sh" - branches: - - "*" - - push: - paths: - - "Dockerfile" - - "main.go" - - "test.sh" - branches: - - "*" - -jobs: - build_test: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v3.1.0 - with: - fetch-depth: 0 - - - name: build docker image - run : | - docker build --tag=test . - - - name: test docker image - run: | - CMD="docker run --rm -i -p 9001:9001 test --port=9001" ./test.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..cfe57b5 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,34 @@ +name: Test + +on: + pull_request: + +jobs: + + Docker: + name: Test in Docker + runs-on: ubuntu-latest + steps: + + - + name: Checkout + uses: actions/checkout@v3 + + - + name: Set up Docker Build + uses: docker/setup-buildx-action@v2 + + - + name: Build Docker Test Image + uses: docker/build-push-action@v3 + id: build + with: + context: . + load: true + cache-from: type=gha + cache-to: type=gha,mode=max + + - + name: Test Docker Image + run: | + CMD="docker run --rm -i -p 9001:9001 ${{ steps.build.outputs.imageid }} --port=9001" ./test.sh diff --git a/Dockerfile b/Dockerfile index 48ba142..b8dd011 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,14 @@ FROM golang:1.19-alpine as builder COPY main.go . +ENV CGO_ENABLED=0 RUN go build -o /app main.go FROM alpine +LABEL maintainer="github.com/bukowa" + # Define GOTRACEBACK to mark this container as using the Go language runtime # for `skaffold debug` (https://skaffold.dev/docs/workflows/debug/). ENV GOTRACEBACK=single ENTRYPOINT ["./app"] COPY --from=builder /app . + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7af863a --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ + +generate-changelog: + ./scripts/generate-changelog.sh + +generate-changelog-ci: + touch CHANGELOG.md CHANGELOG.LATEST.md \ + && \ + ./scripts/generate-changelog.sh \ + -u --prepend=CHANGELOG.md \ + --include-path "*.go" \ + --include-path "*.mod" \ + --include-path "*Dockerfile" \ + && \ + ./scripts/generate-changelog.sh \ + -u --output=CHANGELOG.LATEST.md \ + --include-path "*.go" \ + --include-path "*.mod" \ + --include-path "*Dockerfile" diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 0000000..159d376 --- /dev/null +++ b/cliff.toml @@ -0,0 +1,68 @@ +# configuration file for git-cliff (0.1.0) + +[changelog] +# changelog header +header = """ +# Changelog\n +All notable changes to this project will be documented in this file.\n +""" +# template for the changelog body +# https://tera.netlify.app/docs/#introduction +body = """ +{% if version %}\ + ## [{{ version | trim_start_matches(pat="[-[:alpha:]]") }}] - {{ timestamp | date(format="%Y-%m-%d") }} +{% else %}\ + ## [unreleased] +{% endif %}\ +{% for group, commits in commits | group_by(attribute="group") %} + ### {{ group | upper_first }} + {% for commit in commits %} + - {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message }}\ + {% endfor %} +{% endfor %}\n +""" +# remove the leading and trailing whitespace from the template +trim = true +# changelog footer +footer = """ + +""" + +[git] +# parse the commits based on https://www.conventionalcommits.org +conventional_commits = true +# filter out the commits that are not conventional +filter_unconventional = false +# process each line of a commit as an individual commit +split_commits = false +# regex for preprocessing the commit messages +commit_preprocessors = [ + { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/bukowa/http-headers/issues/${2}))"}, +] +# regex for parsing and grouping commits +commit_parsers = [ + { message = "^feat", group = "Features"}, + { message = "^fix", group = "Bug Fixes"}, + { message = "^doc", group = "Documentation"}, + { message = "^perf", group = "Performance"}, + { message = "^refactor", group = "Refactor"}, + { message = "^style", group = "Styling"}, + { message = "^test", group = "Testing"}, + { message = "^build", group = "Build"}, + { message = "^chore\\(release\\): prepare for", skip = true}, + { message = "^chore", group = "Miscellaneous Tasks"}, + { message = "^add", group = "Core"}, + { body = ".*security", group = "Security"}, +] +# filter out the commits that are not matched by commit parsers +filter_commits = true +# glob pattern for matching git tags +tag_pattern = "[0-9].[0-9].[0-9].*" +# regex for skipping tags +skip_tags = "v0.1.0-beta.1" +# regex for ignoring tags +ignore_tags = "" +# sort the tags chronologically +date_order = false +# sort the commits inside sections by oldest/newest order +sort_commits = "oldest" diff --git a/scripts/generate-changelog.sh b/scripts/generate-changelog.sh new file mode 100755 index 0000000..775c345 --- /dev/null +++ b/scripts/generate-changelog.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -eux + +echo "Building docker image for git-cliff..." + +GIT_CLIFF=$(docker build -q - <