From 70dae2a78f8716dad788b2fcecac2c396f89b86f Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:12:17 +0200 Subject: [PATCH 1/6] ci: inject app version during Docker build and set default config - Updated `github/workflows/build-docker-container.yml` to pass the app version as a build argument using the release tag name.- Modified `Dockerfile` to include `APP_VERSION` argument and inject it during the build process via `ldflags`.- Enhanced `ConfigProvider` in `conf-init.go` to set the default app version using the injected value.- Added `AppVersion` variable in `main.go` for linker injection, ensuring the version is set at runtime based on the Docker build process. --- .github/workflows/build-docker-container.yml | 2 ++ Dockerfile | 5 +++-- config/conf-init.go | 1 + main.go | 10 +++++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-docker-container.yml b/.github/workflows/build-docker-container.yml index 1d306db..b4fd469 100644 --- a/.github/workflows/build-docker-container.yml +++ b/.github/workflows/build-docker-container.yml @@ -20,3 +20,5 @@ jobs: with: push: true tags: hilkopterbob/packagelock:latest + build-args: | + APP_VERSION=${{ github.event.release.tag_name }} diff --git a/Dockerfile b/Dockerfile index 0fbe7c2..a725d4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,8 +16,9 @@ RUN go mod download # Build - -RUN CGO_ENABLED=0 GOOS=linux go build -o /packagelock +ARG APP_VERSION="v0.1.0+hotfixes" +RUN \ + CGO_ENABLED=0 GOOS=linux go build -ldflags "-X 'main.AppVersion=$APP_VERSION'" -o /packagelock # Optional: # To bind to a TCP port, runtime parameters must be supplied to the docker command. diff --git a/config/conf-init.go b/config/conf-init.go index a521c19..a18ee5c 100644 --- a/config/conf-init.go +++ b/config/conf-init.go @@ -20,6 +20,7 @@ type ConfigProvider interface { ReadConfig(in io.Reader) error AllSettings() map[string]any GetString(string string) string + SetDefault(key string, value any) } // TODO: How to test? diff --git a/main.go b/main.go index 419126f..279ad9a 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,20 @@ import ( "github.com/spf13/viper" ) -// Data structs +// Linker Injections +// Version injection with Docker Build & ldflags +// Do not modify, init or change in code! +var AppVersion string // TODO: support for multiple network adapters. func main() { Config := config.StartViper(viper.New()) + + if AppVersion != "" { + Config.SetDefault("general.app-version", AppVersion) + } + fmt.Println(Config.AllSettings()) // Channel to signal the restart From f027f2848237c94f150fa2ee0ce6ba38ebcfa0c0 Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:28:28 +0200 Subject: [PATCH 2/6] ci: moved devel builds to own action, added -unstable tag for containers --- .github/workflows/build-docker-container.yml | 2 +- .../unstable-build-docker-container.yml | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/unstable-build-docker-container.yml diff --git a/.github/workflows/build-docker-container.yml b/.github/workflows/build-docker-container.yml index 1d306db..9b3d205 100644 --- a/.github/workflows/build-docker-container.yml +++ b/.github/workflows/build-docker-container.yml @@ -2,7 +2,7 @@ name: ci on: push: - branches: ["binary-release", "master", "devel"] + branches: ["master"] jobs: docker: diff --git a/.github/workflows/unstable-build-docker-container.yml b/.github/workflows/unstable-build-docker-container.yml new file mode 100644 index 0000000..8f1c41f --- /dev/null +++ b/.github/workflows/unstable-build-docker-container.yml @@ -0,0 +1,22 @@ +name: ci + +on: + push: + branches: ["devel"] + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v6 + with: + push: true + tags: hilkopterbob/packagelock-unstable:latest From 51f695a5de8e93c3079bf446ecae56a081acd664 Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:30:03 +0200 Subject: [PATCH 3/6] ci: changed action name to unstable- docker-build --- .github/workflows/build-docker-container.yml | 2 +- .github/workflows/unstable-build-docker-container.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-docker-container.yml b/.github/workflows/build-docker-container.yml index 9b3d205..a32b6c1 100644 --- a/.github/workflows/build-docker-container.yml +++ b/.github/workflows/build-docker-container.yml @@ -1,4 +1,4 @@ -name: ci +name: docker-build on: push: diff --git a/.github/workflows/unstable-build-docker-container.yml b/.github/workflows/unstable-build-docker-container.yml index 8f1c41f..a7215a8 100644 --- a/.github/workflows/unstable-build-docker-container.yml +++ b/.github/workflows/unstable-build-docker-container.yml @@ -1,4 +1,4 @@ -name: ci +name: unstable-docker-build on: push: From 3430bcd712e98add677ece4a59ccfeeef7519539 Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:38:02 +0200 Subject: [PATCH 4/6] ci: build main container only on published releases & add branch name to unstable containers as docker tag --- .github/workflows/build-docker-container.yml | 4 ++-- .github/workflows/unstable-build-docker-container.yml | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-docker-container.yml b/.github/workflows/build-docker-container.yml index ba2df07..52f4967 100644 --- a/.github/workflows/build-docker-container.yml +++ b/.github/workflows/build-docker-container.yml @@ -1,8 +1,8 @@ name: docker-build on: - push: - branches: ["master"] + release: + types: [published] jobs: docker: diff --git a/.github/workflows/unstable-build-docker-container.yml b/.github/workflows/unstable-build-docker-container.yml index a7215a8..a3f1488 100644 --- a/.github/workflows/unstable-build-docker-container.yml +++ b/.github/workflows/unstable-build-docker-container.yml @@ -2,7 +2,6 @@ name: unstable-docker-build on: push: - branches: ["devel"] jobs: docker: @@ -19,4 +18,4 @@ jobs: uses: docker/build-push-action@v6 with: push: true - tags: hilkopterbob/packagelock-unstable:latest + tags: hilkopterbob/packagelock-unstable:${{ env.BRANCH }} From 587fddb32080479d69e8926c69700eb6f2f2d7c3 Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:41:43 +0200 Subject: [PATCH 5/6] ci: change docker tag --- .github/workflows/unstable-build-docker-container.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unstable-build-docker-container.yml b/.github/workflows/unstable-build-docker-container.yml index a3f1488..d83502a 100644 --- a/.github/workflows/unstable-build-docker-container.yml +++ b/.github/workflows/unstable-build-docker-container.yml @@ -18,4 +18,4 @@ jobs: uses: docker/build-push-action@v6 with: push: true - tags: hilkopterbob/packagelock-unstable:${{ env.BRANCH }} + tags: hilkopterbob/packagelock:unstable-${{ env.BRANCH }} From c9852385b40fc23319e46eea833600e2401feb38 Mon Sep 17 00:00:00 2001 From: HilkopterBob Date: Fri, 16 Aug 2024 08:53:12 +0200 Subject: [PATCH 6/6] ci: add branchname to docker tag --- .github/workflows/unstable-build-docker-container.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unstable-build-docker-container.yml b/.github/workflows/unstable-build-docker-container.yml index d83502a..368beb2 100644 --- a/.github/workflows/unstable-build-docker-container.yml +++ b/.github/workflows/unstable-build-docker-container.yml @@ -18,4 +18,4 @@ jobs: uses: docker/build-push-action@v6 with: push: true - tags: hilkopterbob/packagelock:unstable-${{ env.BRANCH }} + tags: hilkopterbob/packagelock:unstable-${{ github.head_ref || github.ref_name }}