Skip to content

Commit

Permalink
cicd: create docker image tags based on maven versioning
Browse files Browse the repository at this point in the history
- now only deploy docker images when the apps have been changed
- deploy snapshot or release version based on maven version
  • Loading branch information
nicoprow committed Jun 6, 2024
1 parent 1f2f9e0 commit 67bd4d6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' }}
dockerfilePath: ./docker/orchestrator
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -43,56 +47,65 @@ env:
jobs:
docker:
runs-on: ubuntu-latest
permissions:
packages: write

steps:
- uses: dorny/paths-filter@v3
id: changes
with:
filters: |
apps:
- 'pom.xml'
- 'bpdm-**'
- name: Checkout
if: steps.changes.outputs.apps == 'true'
uses: actions/checkout@v4

# Create SemVer or ref tags dependent of trigger event
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
- 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:
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}}
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 }}
Expand Down

0 comments on commit 67bd4d6

Please sign in to comment.