diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b450d9d..ee27d58 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -3,61 +3,59 @@ name: Build release candidate on: release: types: [created] - push: branches: - main jobs: - test: + check-version: runs-on: ubuntu-latest + outputs: + release-version: ${{ steps.version.outputs.version }} steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v2 - with: - distribution: 'temurin' - java-version: '17' - - name: Test with Maven - run: mvn test - - name: Upload test artifact - uses: actions/upload-artifact@v4 + - 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)" >> $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 with: - name: test-artifact - path: target/ + tag: ${{ steps.version.outputs.version }} - sonarcloud: + - 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: 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: - fetch-depth: 0 - - name: Set up JDK 17 - uses: actions/setup-java@v2 - with: - 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 }} + ref: ${{ steps.extract_branch.outputs.branch }} - build: - runs-on: ubuntu-latest - needs: sonarcloud - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v2 + - name: Set up JDK 21 + uses: actions/setup-java@v4 with: - distribution: 'temurin' - java-version: '17' - - name: Extract Maven project version - id: mvn_version - run: echo "::set-output name=version::$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" - - name: Build with Maven + distribution: "temurin" + java-version: "21" + + - name: Build API run: mvn package -Dchangelist=-SNAPSHOT --no-transfer-progress + - name: Upload API war uses: actions/upload-artifact@v4 with: @@ -65,45 +63,23 @@ jobs: path: target/*.war 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 build artifact + + - name: Download uploaded war uses: actions/download-artifact@v4 with: name: app-war path: target/ - - name: Docker meta - id: docker_meta - uses: docker/metadata-action@v3 - with: - images: inseefr/metadata-api - tags: type=semver,pattern={{version}} - flavor: | - latest=false - env: - version: ${{ steps.mvn_version.outputs.version }} - - 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 + + - name: Publish to Docker Hub + uses: elgohr/Publish-Docker-Github-Action@v5 with: + name: inseefr/metadata-api username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - 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 }}" + tags: "latest, ${{ needs.check-version.outputs.release-version }}"