6 Eylül 2023 Çarşamba

Github Actions - maven

Örnek - checkout + Set up JDK + Build
GitHub'daki repository'ye gidilir. Üst başlıktaki "Actions" düğmesine tıklanır. "New workflow" düğmesine tıklanır. "Java with Maven" düğmesine tıklanır. Şeklen şöyle

Karşımıza çıkan dosya şöyle. Dosya yolu şöyle "myrepository/.github/workflows/maven.yaml"
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/ building-and-testing-java-with-maven

name: Java CI with Maven

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'
        cache: maven
    - name: Build with Maven
      run: mvn -B package --file pom.xml
Örnek - checkout + Set up JDK + Build
Şöyle yaparız
name: Java CI with Maven

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up JDK 11
      uses: actions/setup-java@v2
      with:
        java-version: '11'
        distribution: 'adopt'

    - name: Build with Maven
      run: ./mvnw clean install

Örnek
Push işleminden sonra testleri çalıştırıp, Docker'a image push'lamak için şöyle yaparız
name: CI/CD Pipeline

## The following codes trigger the pipeline when the code is pushed on the main branch.
on:
  push:
    branches: [ main ]

jobs:
  ## First Job: Test
  test:
    name: Unit Test
    ## Run on Ubuntu using the Latest Version
    runs-on: ubuntu-latest
    ## Job's steps
    steps:
      - uses: actions/checkout@v1
      ## Set up JDK 11
      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: '11'
      ## Set up Maven Cache
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      ## Run Maven Tests
      - name: Run Tests
        run: mvn -B test

  ## Second Job: Build and Push
  build-and-push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 11
        uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'adopt'
          cache: maven
      - name: Build with Maven
        run: mvn -B package --file pom.xml

      - name: Dockerize & Push Docker Image
        uses: mr-smithers-excellent/docker-build-push@v5
        with:
          ## Docker Hub Account Number / Repository Name
          image: 19812381238/product-microservice
          tags: latest
          registry: docker.io
          ## Dockerfile Path
          dockerfile: product/Dockerfile
          ## Keep these secrets on GitHub for the privacy of our information
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
Örnek
Github sayfasına release yapmak için şöyle yaparız
name: Java CI to create and upload release on pull request
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  build-number: ${GITHUB_RUN_NUMBER}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'
          cache: 'maven'
      - run: mvn clean package -DskipTests
      - run: mkdir staging && mv target/yb-workload-sim.jar target/yb-workload-sim-${{ env.build-number }}.jar && cp target/*.jar staging
      - uses: actions/upload-artifact@v3
        with:
          name: Package
          path: staging
          retention-days: 1
      - uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: "${{ secrets.YOUR-GITHUB-TOKEN }}"
          automatic_release_tag: "${{ github.run_number }}"
          title: "Automated Build ${{ github.run_number }}"
          prerelease: true
          files: staging/*.jar

Hiç yorum yok:

Yorum Gönder