diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ecc8446..07c1e05 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,65 +1,177 @@ name: CI Docker and Helm on: - push: - branches: - - main + push: + branches: + - main env: - IMAGE_NAME: amelieloulou/transform-and-deliver-assets - CHART_PATH: ./opensource + IMAGE_NAME: bouyguestelecom/tada + CHART_PATH: ./opensource jobs: - build-and-push-docker: - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - with: - install: true - - - 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 }}:latest ./src/api - - - name: Build and push Docker image Job - run: | - docker buildx build --platform linux/amd64,linux/arm64 --push -t ${{ env.IMAGE_NAME }}:job-latest ./src - publish-helm-chart: - runs-on: ubuntu-latest - needs: build-and-push-docker - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - 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 opensource - - - name: Push Helm chart to Docker Hub - run: | - helm push $(ls *.tgz) oci://registry-1.docker.io/amelieloulou \ No newline at end of file + determine-version: + runs-on: ubuntu-latest + outputs: + new_version: ${{ steps.set_output.outputs.new_version }} + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Get the latest tag + id: get_latest_tag + run: | + git fetch --tags + latest_tag=$(git tag -l --sort=-v:refname | head -n 1) + if [ -z "$latest_tag" ]; then + latest_tag="0.0.0" + fi + echo "Latest tag: $latest_tag" + echo "tag=$latest_tag" >> $GITHUB_ENV + + - name: Determine next version + id: set_output + run: | + latest_tag=${{ env.tag }} + echo "Latest tag: $latest_tag" + IFS='.' read -r -a version_parts <<< "$latest_tag" + major=${version_parts[0]} + minor=${version_parts[1]} + patch=${version_parts[2]} + if [[ "${{ github.event.head_commit.message }}" == *"major"* ]]; then + major=$((major + 1)) + minor=0 + patch=0 + elif [[ "${{ github.event.head_commit.message }}" == *"minor"* ]]; then + minor=$((minor + 1)) + patch=0 + else + patch=$((patch + 1)) + branch_name=$(echo "${{ github.ref }}" | sed 's/refs\/heads\///' | sed 's/\//-/g') + new_version="$major.$minor.$patch-${branch_name}" + fi + while git rev-parse "refs/tags/$new_version" >/dev/null 2>&1; do + patch=$((patch + 1)) + new_version="$major.$minor.$patch-${branch_name}" + done + echo "New version: $new_version" + echo "new_version=$new_version" >> $GITHUB_ENV + echo "new_version=$new_version" >> $GITHUB_OUTPUT + + - name: Create and push new tag + run: | + git config --global user.name 'github-actions' + git config --global user.email 'github-actions@github.com' + git tag ${{ steps.set_output.outputs.new_version }} + git push origin ${{ steps.set_output.outputs.new_version }} + + build-and-push-docker-api: + runs-on: ubuntu-latest + needs: determine-version + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Cache npm dependencies for API + uses: actions/cache@v3 + 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@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ hashFiles('src/api/Dockerfile', 'src/api/.dockerignore') }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ hashFiles('src/api/Dockerfile', 'src/api/.dockerignore') }}- + + - 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: | + new_version=${{ needs.determine-version.outputs.new_version }} + echo "Docker api with version $new_version" + docker buildx build --platform linux/amd64,linux/arm64 --push -t ${{ env.IMAGE_NAME }}:api-${new_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 + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + with: + install: true + + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ hashFiles('src/Dockerfile', 'src/.dockerignore') }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ hashFiles('src/Dockerfile', 'src/.dockerignore') }}- + + - 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: | + new_version=${{ needs.determine-version.outputs.new_version }} + docker buildx build --platform linux/amd64,linux/arm64 --push -t ${{ env.IMAGE_NAME }}:job-${new_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: + - determine-version + - build-and-push-docker-api + - build-and-push-docker-job + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - 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_CHART_USERNAME }} + password: ${{ secrets.DOCKER_CHART_PASSWORD }} + + - name: Install Helm + uses: azure/setup-helm@v3 + with: + version: v3.11.1 + + - name: Package Helm chart + run: | + helm package opensource --version "${{ needs.determine-version.outputs.new_version }}" + + - name: Push Helm chart to Docker Hub + run: | + helm push $(ls *.tgz) oci://registry-1.docker.io/bouyguestelecomcharts diff --git a/opensource/Chart.yaml b/opensource/Chart.yaml index 2504ef7..fcd0676 100644 --- a/opensource/Chart.yaml +++ b/opensource/Chart.yaml @@ -1,6 +1,6 @@ apiVersion: v2 -name: transform-and-deliver-assets +name: tada description: TADA 🎉 - Transform And Deliver Assets - Bouygues Telecom type: application -version: 0.0.2-beta -appVersion: '0.0.1' \ No newline at end of file +version: '0.1.1' +appVersion: '0.1.1' diff --git a/package.json b/package.json index 5e9e62a..123d0be 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "scripts": { "update-local-bru": "node local/update-local-bru.js", - "format": "prettier --write ." + "format": "prettier --write .", + "build": "tsc" } } diff --git a/src/api/Dockerfile b/src/api/Dockerfile index 7f6d24e..b7c86f5 100644 --- a/src/api/Dockerfile +++ b/src/api/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20-alpine AS builder +FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm set strict-ssl false && npm install @@ -7,7 +7,7 @@ RUN npm run build WORKDIR /images COPY ./images . -FROM node:20-alpine +FROM node:18-alpine WORKDIR / COPY --from=builder /app/package*.json ./ @@ -15,4 +15,4 @@ COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/dist ./dist COPY --from=builder /app/images ./tmp/images -CMD ["node", "dist/server.js"] \ No newline at end of file +CMD ["node", "dist/server.js"] diff --git a/src/api/Dockerfile.dev b/src/api/Dockerfile.dev index c8bfafc..2485dac 100644 --- a/src/api/Dockerfile.dev +++ b/src/api/Dockerfile.dev @@ -1,4 +1,4 @@ -FROM node:20-alpine AS builder +FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm set strict-ssl false && npm install @@ -7,7 +7,7 @@ RUN npm run build WORKDIR /images COPY ./images . -FROM node:20-alpine +FROM node:18-alpine WORKDIR / COPY --from=builder /app/package*.json ./