Try to update version with commit message #57
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI Docker and Helm | |
on: | |
push: | |
branches: | |
- workflow/api-improve | |
env: | |
IMAGE_NAME: amelieloulou/transform-and-deliver-assets | |
CHART_PATH: ./opensource | |
jobs: | |
determine-version: | |
runs-on: ubuntu-latest | |
outputs: | |
version: ${{ steps.set_version.outputs.next_version }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Git | |
run: | | |
git config --global --add safe.directory /github/workspace | |
- name: Get the current version | |
id: get_version | |
run: | | |
current_version="0.0.0" | |
if [ $(git tag -l | wc -l) -ne 0 ]; then | |
latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1)) | |
echo "Found latest tag: $latest_tag" | |
current_version=$latest_tag | |
fi | |
echo "current_version=$current_version" >> $GITHUB_ENV | |
echo "Current version: $current_version" | |
- name: Prepare the next version | |
id: set_version | |
run: | | |
current_version=${{ env.current_version }} | |
read_major_minor_patch() { | |
IFS='.' read -a version_parts <<< "$1" | |
echo "${version_parts[0]:-0} ${version_parts[1]:-0} ${version_parts[2]:-0}" | |
} | |
commit_message=$(git log -1 --pretty=%B) | |
read major minor patch < <(read_major_minor_patch "$current_version") | |
if [[ $commit_message =~ major ]]; then | |
major=$((major + 1)) | |
minor=0 | |
patch=0 | |
elif [[ $commit_message =~ minor ]]; then | |
minor=$((minor + 1)) | |
patch=0 | |
else | |
patch=$((patch + 1)) | |
fi | |
next_version="$major.$minor.$patch" | |
echo "next_version=$next_version" >> $GITHUB_ENV | |
echo "Creating new tag: $next_version" | |
echo "next_version=$next_version" | |
env: | |
current_version: ${{ env.current_version }} | |
- name: Set the next tag | |
run: | | |
next_version=${{ steps.set_version.outputs.next_version }} | |
git tag $next_version | |
git push origin $next_version | |
build-and-push-docker-api: | |
runs-on: ubuntu-latest | |
needs: determine-version | |
env: | |
IMAGE_VERSION: ${{ needs.determine-version.outputs.version }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Cache npm dependencies for API | |
uses: actions/cache@v4 | |
with: | |
path: src/api/node_modules | |
key: ${{ runner.os }}-api-node-modules-${{ hashFiles('src/api/package-lock.json') }} | |
restore-keys: | | |
${{ runner.os }}-api-node-modules- | |
- name: Install npm dependencies for API | |
working-directory: src/api | |
run: npm install | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
with: | |
install: true | |
- name: Cache Docker layers | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image API | |
run: | | |
docker buildx build --platform linux/amd64,linux/arm64 --push -t ${{ env.IMAGE_NAME }}:api-${{ env.IMAGE_VERSION }} --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,dest=/tmp/.buildx-cache,mode=max ./src/api | |
build-and-push-docker-job: | |
runs-on: ubuntu-latest | |
needs: determine-version | |
env: | |
IMAGE_VERSION: ${{ needs.determine-version.outputs.version }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
with: | |
install: true | |
- name: Cache Docker layers | |
uses: actions/cache@v4 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{ runner.os }}-buildx-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-buildx- | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image Job | |
run: | | |
docker buildx build --platform linux/amd64,linux/arm64 --push -t ${{ env.IMAGE_NAME }}:job-${{ env.IMAGE_VERSION }} --cache-from=type=local,src=/tmp/.buildx-cache --cache-to=type=local,dest=/tmp/.buildx-cache,mode=max ./src | |
publish-helm-chart: | |
runs-on: ubuntu-latest | |
needs: | |
- build-and-push-docker-api | |
- build-and-push-docker-job | |
env: | |
IMAGE_VERSION: ${{ needs.determine-version.outputs.version }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Install Helm | |
uses: azure/setup-helm@v3 | |
with: | |
version: v3.11.1 | |
- name: Package Helm chart | |
run: | | |
helm package ${{ env.CHART_PATH }} | |
- name: Push Helm chart to Docker Hub | |
run: | | |
helm push $(ls *.tgz) oci://registry-1.docker.io/${{ env.IMAGE_NAME }}-helm:${{ env.IMAGE_VERSION }} |