From e2e24382eee9aaa05cb6ec59e860b39895f567bd Mon Sep 17 00:00:00 2001 From: Nico Koprowski Date: Thu, 6 Jun 2024 16:30:35 +0800 Subject: [PATCH] cicd: create docker image tags based on maven versioning - now only deploy docker images when the apps have been changed - deploy snapshot or release version based on maven version --- ...docker-all.yaml => deploy-docker-all.yaml} | 37 ++------ .../{build-docker.yaml => deploy-docker.yaml} | 84 +++++++++++-------- docs/developer/README.md | 78 ++++++++++------- 3 files changed, 107 insertions(+), 92 deletions(-) rename .github/workflows/{build-docker-all.yaml => deploy-docker-all.yaml} (61%) rename .github/workflows/{build-docker.yaml => deploy-docker.yaml} (55%) diff --git a/.github/workflows/build-docker-all.yaml b/.github/workflows/deploy-docker-all.yaml similarity index 61% rename from .github/workflows/build-docker-all.yaml rename to .github/workflows/deploy-docker-all.yaml index 1cb164381..e99a49710 100644 --- a/.github/workflows/build-docker-all.yaml +++ b/.github/workflows/deploy-docker-all.yaml @@ -17,59 +17,40 @@ # SPDX-License-Identifier: Apache-2.0 ################################################################################ -name: Build All - Docker images (SemVer) +name: Deploy - All Docker Images on: + workflow_dispatch: push: - # only execute when source specific files or workflows change paths: - - pom.xml - - bpdm-pool/** - - bpdm-gate/** - - bpdm-common/** - - bpdm-gate-api/** - - bpdm-bridge-dummy/** - - bpdm-cleaning-service-dummy/** - - bpdm-orchestrator/** - - .github/workflows/** - tags: - - 'v*.*.*' - - 'v*.*.*-*' + - 'pom.xml' + - 'bpdm-**' branches: - main - - rc/** - pull_request: - branches: - - main - - rc/** jobs: build-docker-pool: - uses: ./.github/workflows/build-docker.yaml + uses: ./.github/workflows/deploy-docker.yaml secrets: inherit with: imageName: bpdm-pool dockerfilePath: ./docker/pool - push: ${{ github.event_name != 'pull_request' }} build-docker-gate: - uses: ./.github/workflows/build-docker.yaml + uses: ./.github/workflows/deploy-docker.yaml secrets: inherit with: imageName: bpdm-gate dockerfilePath: ./docker/gate - push: ${{ github.event_name != 'pull_request' }} build-docker-cleaning-service: - uses: ./.github/workflows/build-docker.yaml + uses: ./.github/workflows/deploy-docker.yaml secrets: inherit with: imageName: bpdm-cleaning-service-dummy dockerfilePath: ./docker/cleaning-service-dummy - push: ${{ github.event_name != 'pull_request' }} build-docker-orchestrator: - uses: ./.github/workflows/build-docker.yaml + uses: ./.github/workflows/deploy-docker.yaml secrets: inherit with: imageName: bpdm-orchestrator - dockerfilePath: ./docker/orchestrator - push: ${{ github.event_name != 'pull_request' }} \ No newline at end of file + dockerfilePath: ./docker/orchestrator \ No newline at end of file diff --git a/.github/workflows/build-docker.yaml b/.github/workflows/deploy-docker.yaml similarity index 55% rename from .github/workflows/build-docker.yaml rename to .github/workflows/deploy-docker.yaml index 36229f4a0..7803e5b56 100644 --- a/.github/workflows/build-docker.yaml +++ b/.github/workflows/deploy-docker.yaml @@ -17,10 +17,9 @@ # SPDX-License-Identifier: Apache-2.0 ################################################################################ -name: Build - Docker image (SemVer) - +name: Deploy - Docker Image on: - workflow_call: + workflow_dispatch: inputs: imageName: required: true @@ -30,11 +29,16 @@ on: required: true type: string description: Path to where the Dockerfile to build is - push: + workflow_call: + inputs: + imageName: required: true - type: boolean - description: Whether to also push created image or just build it - + type: string + description: Name the built image should get + dockerfilePath: + required: true + type: string + description: Path to where the Dockerfile to build is env: IMAGE_NAMESPACE: "tractusx" @@ -43,56 +47,64 @@ env: jobs: docker: runs-on: ubuntu-latest - permissions: - packages: write - steps: - name: Checkout uses: actions/checkout@v4 - # Create SemVer or ref tags dependent of trigger event - - name: Docker meta - id: meta - uses: docker/metadata-action@v5 + - uses: dorny/paths-filter@v3 + id: changes with: - images: | - ${{ env.IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }} - # Automatically prepare image tags; See action docs for more examples. - # semver patter will generate tags like these for example :1 :1.2 :1.2.3 - tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}} - type=semver,pattern={{major}}.{{minor}} - type=raw,value=latest-alpha,enable={{is_default_branch}} + filters: | + apps: + - 'pom.xml' + - 'bpdm-**' + + - name: Extract Maven Project Version + if: steps.changes.outputs.apps == 'true' + id: pomVersion + run: | + POM_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' -B --non-recursive exec:exec) + echo "version=$POM_VERSION" >> $GITHUB_OUTPUT + + - name: Parse semantic version from string + if: steps.changes.outputs.apps == 'true' + id: semVer + uses: release-kit/semver@v2 + with: + string: 'v${{ steps.pomVersion.outputs.version }}' + + - name: Create Docker Tags + id: dockerTags + run: | + SUFFIX="${{ steps.semVer.outputs.prerelease == 'SNAPSHOT' && '-SNAPSHOT' || '' }}" + LATEST="latest$SUFFIX" + MAJOR="${{ steps.semVer.outputs.major }}" + MINOR="${MAJOR}.${{ steps.semVer.outputs.minor }}" + PATCH="${MINOR}.${{ steps.semVer.outputs.patch }}" + MAJOR_VER="${MAJOR}${SUFFIX}" + MINOR_VER="${MINOR}${SUFFIX}" + PATCH_VER="${PATCH}${SUFFIX}" + echo "tags=${LATEST},${MAJOR_VER},${MINOR_VER},${PATCH_VER}" >> $GITHUB_OUTPUT - name: DockerHub login - if: inputs.push uses: docker/login-action@v3 with: - # Use existing DockerHub credentials present as secrets username: ${{ secrets.DOCKER_HUB_USER }} password: ${{ secrets.DOCKER_HUB_TOKEN }} - name: Build and push + if: steps.changes.outputs.apps == 'true' uses: docker/build-push-action@v5 with: context: . - # Build image for verification purposes on every trigger event. Only push if event is not a PR - push: ${{ inputs.push }} + push: true file: ${{ inputs.dockerfilePath }}/Dockerfile - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + tags: ${{ steps.dockerTags.outputs.tags }} - # https://github.com/peter-evans/dockerhub-description - # Important step to push image description to DockerHub - name: Update Docker Hub description - if: inputs.push + if: steps.changes.outputs.apps == 'true' uses: peter-evans/dockerhub-description@v4 with: - # readme-filepath defaults to toplevel README.md, Only necessary if you have a dedicated file with your 'Notice for docker images' - # readme-filepath: path/to/dedicated/notice-for-docker-image.md username: ${{ secrets.DOCKER_HUB_USER }} password: ${{ secrets.DOCKER_HUB_TOKEN }} repository: ${{ env.IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }} diff --git a/docs/developer/README.md b/docs/developer/README.md index c79c2ee74..07643a7f3 100644 --- a/docs/developer/README.md +++ b/docs/developer/README.md @@ -25,42 +25,64 @@ Check the [Eclipse Dash License Tool documentation](https://github.com/eclipse/d ```mermaid --- -title: BPDM Branching Strategy +title: BPDM Branching Strategy (App Tags) --- -gitGraph +gitGraph TB: + commit tag: "v1.0.0-alpha.0" + branch "app-feature A" + branch "app-feature B" + checkout "app-feature A" commit commit - branch "release/1.0.x" - checkout "release/1.0.x" - commit id: "ver(1.0.0)" tag: "1.0.0" - - checkout main - commit id: "ver(0.1.0-SNAPSHOT)" - commit id: "fix(A)" + checkout "main" + merge "app-feature A" tag: "v1.0.0-alpha.1" + checkout "app-feature B" + commit + checkout "main" + merge "app-feature B" tag: "v1.0.0" + branch "chart-feature C" + commit + checkout "main" + merge "chart-feature C" + branch "app-feature D" commit - checkout "release/1.0.x" - branch "fix/1.0.x/A" - cherry-pick id: "fix(A)" - checkout "release/1.0.x" - merge "fix/1.0.x/A" + checkout "main" + merge "app-feature D" tag: "v1.0.1-alpha.0" + branch "app-feature E" + commit + checkout "main" + merge "app-feature E" tag: "v1.1.0-alpha.0" +``` - checkout "release/1.0.x" - commit id: "ver(1.0.1)" tag: "1.0.1" - checkout main +```mermaid +--- +title: BPDM Branching Strategy (Chart Tags) +--- +gitGraph TB: + commit tag: "bpdm-1.0.0-alpha.0" + branch "app-feature A" + branch "app-feature B" + checkout "app-feature A" + commit + commit + checkout "main" + merge "app-feature A" tag: "bpdm-1.0.0-alpha.1" + checkout "app-feature B" + commit + checkout "main" + merge "app-feature B" tag: "bpdm-1.0.0" + branch "chart-feature C" commit + checkout "main" + merge "chart-feature C" tag: "bpdm-1.0.1" + branch "app-feature D" commit - branch "release/1.1.x" - checkout "release/1.1.x" - commit id: "ver(1.1.0)" tag: "1.1.0" - checkout main - commit id: "ver(1.2.0-SNAPSHOT)" + checkout "main" + merge "app-feature D" tag: "bpdm-1.0.1-alpha.0" + branch "app-feature E" commit - commit id: "ver(2.0.0-SNAPSHOT)" - branch "feat(B)" - commit id: "feat(B)" - checkout main - branch "fix/C" - commit id: "fix(C)" + checkout "main" + merge "app-feature E" tag: "bpdm-1.1.0-alpha.0" ``` ## NOTICE