From 251c6c1b12bfc12607dc3b93bada1c7aa50aadb9 Mon Sep 17 00:00:00 2001 From: James Green Date: Tue, 5 Dec 2023 15:57:08 +0000 Subject: [PATCH 1/9] general repo baselineing --- .github/CODEOWNERS | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index ac066e6..2527c6c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,3 +1 @@ -# Add a team or username to this file -# Example: -# * @ministryofjustice/operations-engineering +* @ministryofjustice/nvvs-devops-admins From a50c891d017bb7c1d654ccb3f58f774a4bebe536 Mon Sep 17 00:00:00 2001 From: James Green Date: Tue, 5 Dec 2023 16:31:49 +0000 Subject: [PATCH 2/9] corrected mistake in documenation --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index a3108f9..a243c1b 100644 --- a/makefile +++ b/makefile @@ -9,7 +9,7 @@ build: ## Build the docker container .PHONO: create-dir make create-dir: ## Creates a directory for end user to put CSV file into mkdir data_src; - echo "Please put csv file into data_src then run 'make-prod'"; + echo "Please put csv file into data_src then run 'make run-prod'"; .PHONY: run-prod run-prod: ## Run the python script only mounting the host for csv-file. Format: MIST_API_TOKEN=foo ORG_ID=bar make run-prod From 9536a3f89f3884500906eba2c7372f45cadeb451 Mon Sep 17 00:00:00 2001 From: James Green Date: Wed, 6 Dec 2023 13:57:29 +0000 Subject: [PATCH 3/9] adding github action to build and push container --- .github/workflows/release.yml | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..4406a65 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,41 @@ +name: release + +on: + push: + tags: ['v*.*.*'] # only a valid semver tag + +env: + REGISTRY: ghcr.io + IMAGE_NAME: '${{ github.repository_owner }}/nvvs/juniper-mist-integration/app' + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker Login + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: metadata + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.metadata.outputs.tags }} + labels: ${{ steps.metadata.outputs.labels }} From b9cbce03be45dd42354f804f5857ac56de561585 Mon Sep 17 00:00:00 2001 From: James Green Date: Wed, 6 Dec 2023 15:41:05 +0000 Subject: [PATCH 4/9] added semvar scripts WIP --- .gitignore | 2 +- makefile | 45 +++++++++++++++++++++++++++++++ semver/LICENSE | 22 +++++++++++++++ semver/README.md | 22 +++++++++++++++ semver/increment_version.sh | 53 +++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 semver/LICENSE create mode 100644 semver/README.md create mode 100755 semver/increment_version.sh diff --git a/.gitignore b/.gitignore index 460222a..84db2d3 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ env/ terraform.tfstate data_src/**.csv -./idea/* +.idea **/__pycache__/ diff --git a/makefile b/makefile index a243c1b..b5b8a6b 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,45 @@ #!make .DEFAULT_GOAL := help SHELL := '/bin/bash' +REGISTRY := ghcr.io +GITHUB_OWNER := $$(git config remote.origin.url | cut -d : -f 2 | cut -d / -f 1) +NAME := ${GITHUB_OWNER}/nvvs/juniper-mist-integration/app +TAG := $$(git log -1 --pretty=%h) +IMG := ${NAME}:${TAG} +LATEST := ${NAME}:latest + +CURRENT_VERSION := $$(git describe --abbrev=0) +CURRENT_NUMBER := $$(echo $(CURRENT_VERSION) | cut -d "v" -f 2) + +ifeq ($(SEMVAR),patch) + NEXT_NUMBER := $$(./semver/increment_version.sh -p $(CURRENT_NUMBER)) +else ifeq ($(SEMVAR),minor) + NEXT_NUMBER := $$(./semver/increment_version.sh -m $(CURRENT_NUMBER)) +else ifeq ($(SEMVAR),major) + NEXT_NUMBER := $$(./semver/increment_version.sh -M $(CURRENT_NUMBER)) +endif + +NEXT_VERSION := "v$(NEXT_NUMBER)" + +.PHONY: debug +debug: ## debug + @echo $(NEXT_NUMBER) + +.PHONY: current_version +current_version: ## Get current version eg v3.4.1 + @echo $(CURRENT_VERSION) + @echo $(CURRENT_NUMBER) + +.PHONY: preview_version +preview_version: ## increment version eg v3.4.1 > v3.5.0. Use SEMVAR=[ patch | minor | major ] + @echo "CURRENT_VERSION := $(CURRENT_VERSION)" + @echo "$(SEMVAR) := $(NEXT_VERSION)" + +.PHONY: tag +tag: ## Tag branch in git repo with next version number. Use SEMVAR=[ patch | minor | major ] + @echo "tagging with $(NEXT_VERSION)" + @git tag -a "$(NEXT_VERSION)" -m "Bump from $(CURRENT_VERSION) to $(NEXT_VERSION)" + @git push origin main --follow-tags .PHONY: build build: ## Build the docker container @@ -40,5 +79,11 @@ shell: ## Make interactive docker container -v $(shell pwd)/data_src:/data_src \ -e RUN_UNIT_TESTS=True juniper-mist +.PHONY: tag +tag: ## Tag branch in git repo with next version number. Use SEMVAR=[ patch | minor | major ] + @echo "tagging with $(NEXT_VERSION)" + @git tag -a "$(NEXT_VERSION)" -m "Bump from $(CURRENT_VERSION) to $(NEXT_VERSION)" + @git push origin main --follow-tags + help: @grep -h -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/semver/LICENSE b/semver/LICENSE new file mode 100644 index 0000000..94a88e5 --- /dev/null +++ b/semver/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2014 Fritz Mahnke + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice 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 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/semver/README.md b/semver/README.md new file mode 100644 index 0000000..2d7b356 --- /dev/null +++ b/semver/README.md @@ -0,0 +1,22 @@ +# bash-semver + +Increment semantic versioning strings in shell scripts. + +```shell +$ ./increment_version.sh +usage: increment_version.sh [-Mmp] major.minor.patch + +$ ./increment_version.sh -p 0.0.0 +0.0.1 + +$ ./increment_version.sh -m 0.0.3 +0.1.0 + +$ ./increment_version.sh -M 1.1.15 +2.0.0 + +$ ./increment_version.sh -Mmp 2.3.4 +3.1.1 +``` + +Sourced from https://github.com/fmahnke/shell-semver diff --git a/semver/increment_version.sh b/semver/increment_version.sh new file mode 100755 index 0000000..8713d5b --- /dev/null +++ b/semver/increment_version.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +# Increment a version string using Semantic Versioning (SemVer) terminology. + +# Parse command line options. + +while getopts ":Mmp" Option +do + case $Option in + M ) major=true;; + m ) minor=true;; + p ) patch=true;; + esac +done + +shift $(($OPTIND - 1)) + +version=$1 + +# Build array from version string. + +a=( ${version//./ } ) + +# If version string is missing or has the wrong number of members, show usage message. + +if [ ${#a[@]} -ne 3 ] +then + echo "usage: $(basename $0) [-Mmp] major.minor.patch" + exit 1 +fi + +# Increment version numbers as requested. + +if [ ! -z $major ] +then + ((a[0]++)) + a[1]=0 + a[2]=0 +fi + +if [ ! -z $minor ] +then + ((a[1]++)) + a[2]=0 +fi + +if [ ! -z $patch ] +then + ((a[2]++)) +fi + +echo "${a[0]}.${a[1]}.${a[2]}" + From b9c16c74185d2aae15b2ba61da41747c8e52c5f6 Mon Sep 17 00:00:00 2001 From: Stephen James Date: Wed, 6 Dec 2023 15:57:49 +0000 Subject: [PATCH 5/9] Fix tag error message Adds NAME container variables. --- makefile | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/makefile b/makefile index b5b8a6b..a26377a 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,10 @@ SHELL := '/bin/bash' REGISTRY := ghcr.io GITHUB_OWNER := $$(git config remote.origin.url | cut -d : -f 2 | cut -d / -f 1) -NAME := ${GITHUB_OWNER}/nvvs/juniper-mist-integration/app +GITHUB_REPO := $$(basename `git rev-parse --show-toplevel`) +TEAM_NAME := "nvvs" +CONTAINER_NAME := "app" +NAME := ${GITHUB_OWNER}/${TEAM_NAME}/${GITHUB_REPO}/${CONTAINER_NAME} TAG := $$(git log -1 --pretty=%h) IMG := ${NAME}:${TAG} LATEST := ${NAME}:latest @@ -35,6 +38,10 @@ preview_version: ## increment version eg v3.4.1 > v3.5.0. Use SEMVAR=[ patch | m @echo "CURRENT_VERSION := $(CURRENT_VERSION)" @echo "$(SEMVAR) := $(NEXT_VERSION)" +.PHONY: preview_name +preview_name: ## view container name + @echo "NAME := $(NAME)" + .PHONY: tag tag: ## Tag branch in git repo with next version number. Use SEMVAR=[ patch | minor | major ] @echo "tagging with $(NEXT_VERSION)" @@ -79,11 +86,5 @@ shell: ## Make interactive docker container -v $(shell pwd)/data_src:/data_src \ -e RUN_UNIT_TESTS=True juniper-mist -.PHONY: tag -tag: ## Tag branch in git repo with next version number. Use SEMVAR=[ patch | minor | major ] - @echo "tagging with $(NEXT_VERSION)" - @git tag -a "$(NEXT_VERSION)" -m "Bump from $(CURRENT_VERSION) to $(NEXT_VERSION)" - @git push origin main --follow-tags - help: @grep -h -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' From 7055114f605c4a99c7e8e23d7ae0f2602ffe3455 Mon Sep 17 00:00:00 2001 From: James Green Date: Wed, 6 Dec 2023 16:06:30 +0000 Subject: [PATCH 6/9] updated github action --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4406a65..0548b5c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,7 +35,7 @@ jobs: - name: Build and push Docker image uses: docker/build-push-action@v5 with: - context: . + context: docker/Dockerfile . push: true tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} From d466f4eff75c9f7e9b29e6fcf8af0fdfc48a15be Mon Sep 17 00:00:00 2001 From: Stephen James Date: Wed, 6 Dec 2023 16:17:10 +0000 Subject: [PATCH 7/9] Adding empty var protection incorrect or empty SEMVAR values when needed will stop with an eror. --- makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/makefile b/makefile index a26377a..09e17b6 100644 --- a/makefile +++ b/makefile @@ -35,6 +35,9 @@ current_version: ## Get current version eg v3.4.1 .PHONY: preview_version preview_version: ## increment version eg v3.4.1 > v3.5.0. Use SEMVAR=[ patch | minor | major ] +ifeq ($(filter $(SEMVAR), patch minor major),) + $(error invalid `SEMVAR` value) +endif @echo "CURRENT_VERSION := $(CURRENT_VERSION)" @echo "$(SEMVAR) := $(NEXT_VERSION)" @@ -44,6 +47,9 @@ preview_name: ## view container name .PHONY: tag tag: ## Tag branch in git repo with next version number. Use SEMVAR=[ patch | minor | major ] +ifeq ($(filter $(SEMVAR), patch minor major),) + $(error invalid `SEMVAR` value) +endif @echo "tagging with $(NEXT_VERSION)" @git tag -a "$(NEXT_VERSION)" -m "Bump from $(CURRENT_VERSION) to $(NEXT_VERSION)" @git push origin main --follow-tags From d2b45a03532208f5bc8d61a0e3de1122ca04a13c Mon Sep 17 00:00:00 2001 From: James Green Date: Wed, 6 Dec 2023 16:21:58 +0000 Subject: [PATCH 8/9] update action to use dockerfile --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0548b5c..fdfac53 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,7 +35,7 @@ jobs: - name: Build and push Docker image uses: docker/build-push-action@v5 with: - context: docker/Dockerfile . + file: docker/Dockerfile push: true tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} From 96a93344651c8a45228e148435e1260b19a6ccf0 Mon Sep 17 00:00:00 2001 From: Stephen James Date: Wed, 6 Dec 2023 16:58:35 +0000 Subject: [PATCH 9/9] Use container name for build and usage --- makefile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/makefile b/makefile index 09e17b6..cf319a3 100644 --- a/makefile +++ b/makefile @@ -56,7 +56,8 @@ endif .PHONY: build build: ## Build the docker container - docker build -t juniper-mist -f docker/Dockerfile . + docker build --tag ${IMG} -f docker/Dockerfile . + docker tag ${IMG} ${LATEST} .PHONO: create-dir make create-dir: ## Creates a directory for end user to put CSV file into @@ -68,7 +69,7 @@ run-prod: ## Run the python script only mounting the host for csv-file. Format: docker run -v $(shell pwd)/data_src:/data_src \ -e MIST_API_TOKEN=$$MIST_API_TOKEN \ -e ORG_ID=$$ORG_ID \ - juniper-mist + $(NAME) .PHONY: run-dev run-dev: ## Run the python script while mounting the host. This enables using the latest local src code without needing to wait for a container build. Format: MIST_API_TOKEN=foo ORG_ID=bar make run-dev @@ -76,13 +77,13 @@ run-dev: ## Run the python script while mounting the host. This enables using th -v $(shell pwd)/data_src:/data_src \ -e MIST_API_TOKEN=$$MIST_API_TOKEN \ -e ORG_ID=$$ORG_ID \ - juniper-mist + $(NAME) .PHONY: tests tests: ## Run unit tests for the python app docker run -v $(shell pwd)/src:/app/src \ -v $(shell pwd)/test:/app/test \ - -e RUN_UNIT_TESTS=True juniper-mist + -e RUN_UNIT_TESTS=True $(NAME) .PHONY: shell shell: ## Make interactive docker container @@ -90,7 +91,7 @@ shell: ## Make interactive docker container -v $(shell pwd)/src:/app/src \ -v $(shell pwd)/test:/app/test \ -v $(shell pwd)/data_src:/data_src \ - -e RUN_UNIT_TESTS=True juniper-mist + -e RUN_UNIT_TESTS=True $(NAME) help: @grep -h -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'