diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ee1e22aa..047e7a83 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,83 +3,103 @@ name: Build release candidate on: release: types: [created] + push: branches: - main jobs: - check-version: + test: runs-on: ubuntu-latest - outputs: - release-version: ${{ steps.version.outputs.version }} steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Get version - id: version - run: echo "version=$(mvn -f pom.xml help:evaluate -Dexpression=project.version -q -DforceStdout)-SNAPSHOT" >> $GITHUB_OUTPUT - - - name: Print version - run: echo ${{ steps.version.outputs.version }} - - - uses: mukunku/tag-exists-action@v1.6.0 - name: Check tag existence - id: check-tag-exists + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v2 # Updated to use the latest version of the setup-java action + with: + distribution: 'temurin' # Best practice to specify the Java distribution + java-version: '17' + - name: Set current version + run: mvn -B versions:set -DnewVersion=${{ github.ref_name }} -DprocessAllModules -DgenerateBackupPoms=false + - name: Test with Maven + run: mvn test + - name: Upload test artifact + uses: actions/upload-artifact@v4 with: - tag: ${{ steps.version.outputs.version }} + name: test-artifact + path: target/ - - name: Tag verification - id: check-tag - run: | - if [[ "${{ steps.check-tag-exists.outputs.exists }}" == "true" ]]; then - echo "Nothing to tag/release, the tag ${{ steps.version.outputs.version }} already exists" - exit 1 - fi - build-release: + sonarcloud: runs-on: ubuntu-latest + needs: test steps: - - name: Extract branch name - shell: bash - run: echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >>$GITHUB_OUTPUT - id: extract_branch - - uses: actions/checkout@v4 with: - ref: ${{ steps.extract_branch.outputs.branch }} - + fetch-depth: 0 - name: Set up JDK 17 - uses: actions/setup-java@v4 + uses: actions/setup-java@v2 with: - distribution: "temurin" - java-version: "17" - - - name: Build API - run: mvn package -Dchangelist=-SNAPSHOT --no-transfer-progress + distribution: 'temurin' + java-version: '17' + - name: Test, package, and analyze with Maven & SonarCloud + run: mvn verify sonar:sonar -Dsonar.projectKey=InseeFr_Metadata-API -Dsonar.organization=inseefr -Dsonar.host.url=https://sonarcloud.io + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - - name: Upload API war + build: + runs-on: ubuntu-latest + needs: sonarcloud + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + distribution: 'temurin' + java-version: '17' + - name: Set current version + run: mvn -B versions:set -DnewVersion=${{ github.ref_name }} -DprocessAllModules -DgenerateBackupPoms=false + - name: Build with Maven + run: mvn package -DskipTests + - name: Upload build artifact uses: actions/upload-artifact@v4 with: - name: app-war - path: target/*.war + name: build-artifact + path: target/ docker: - needs: - - check-version - - build-release runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + needs: build steps: - uses: actions/checkout@v4 - - - name: Download uploaded war + - name: Download build artifact uses: actions/download-artifact@v4 with: - name: app-war + name: build-artifact path: target/ - - - name: Publish to Docker Hub - uses: elgohr/Publish-Docker-Github-Action@v5 + - name: Docker meta + id: docker_meta + uses: docker/metadata-action@v3 # Ensure to use the correct version + with: + images: inseefr/metadata-api + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to DockerHub + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 with: - name: inseefr/metadata-api username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - tags: "latest, ${{ needs.check-version.outputs.release-version }}" + password: ${{ secrets.DOCKERHUB_PASSWORD }} + - name: Build and push + id: docker_build + uses: docker/build-push-action@v3 + with: + context: . + file: Dockerfile + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.docker_meta.outputs.tags }} + labels: ${{ steps.docker_meta.outputs.labels }} + - name: Image digest + run: | + echo "Docker Image Digest: ${{ steps.docker_build.outputs.digest }}"