|
| 1 | +name: Java Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["main"] |
| 6 | + pull_request: |
| 7 | + branches: ["main"] |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + name: Build and Test |
| 12 | + runs-on: ubuntu-latest |
| 13 | + env: |
| 14 | + GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }} |
| 15 | + GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }} |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up JDK 17 |
| 22 | + uses: actions/setup-java@v4 |
| 23 | + with: |
| 24 | + distribution: 'temurin' |
| 25 | + java-version: '17' |
| 26 | + |
| 27 | + - name: Cache SonarQube packages |
| 28 | + uses: actions/cache@v4 |
| 29 | + with: |
| 30 | + path: ~/.sonar/cache |
| 31 | + key: ${{ runner.os }}-sonar |
| 32 | + restore-keys: ${{ runner.os }}-sonar |
| 33 | + |
| 34 | + - name: Cache Maven packages |
| 35 | + uses: actions/cache@v4 |
| 36 | + with: |
| 37 | + path: ~/.m2 |
| 38 | + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} |
| 39 | + restore-keys: ${{ runner.os }}-m2 |
| 40 | + |
| 41 | + - name: Build with Maven |
| 42 | + run: mvn clean install -DskipTests |
| 43 | + |
| 44 | + # - name: Run Unit Tests |
| 45 | + # run: mvn test |
| 46 | + |
| 47 | + # - name: Sonarqube analysis |
| 48 | + # env: |
| 49 | + # SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 50 | + # SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} |
| 51 | + # run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=java-starter -Dsonar.projectName='java-starter' |
| 52 | + |
| 53 | + - name: Package Artifact |
| 54 | + run: mvn package -DskipTests |
| 55 | + |
| 56 | + - name: Upload Artifact |
| 57 | + uses: actions/upload-artifact@v4 |
| 58 | + with: |
| 59 | + name: java-app |
| 60 | + path: target/*.war |
| 61 | + |
| 62 | + - name: Create Maven settings.xml |
| 63 | + run: | |
| 64 | + mkdir -p ~/.m2 |
| 65 | + cat <<EOF > ~/.m2/settings.xml |
| 66 | + <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" |
| 67 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 68 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 69 | + <servers> |
| 70 | + <server> |
| 71 | + <id>github</id> |
| 72 | + <username>${GHCR_USERNAME}</username> |
| 73 | + <password>${GHCR_TOKEN}</password> |
| 74 | + </server> |
| 75 | + </servers> |
| 76 | + </settings> |
| 77 | + EOF |
| 78 | + |
| 79 | + - name: Publish WAR to GitHub Packages |
| 80 | + run: mvn deploy -DskipTests --settings ~/.m2/settings.xml |
| 81 | + |
| 82 | + - name: Log in to GHCR |
| 83 | + run: echo "${GHCR_TOKEN}" | docker login ghcr.io -u ${GHCR_USERNAME} --password-stdin |
| 84 | + |
| 85 | + - name: Build Docker image |
| 86 | + run: | |
| 87 | + docker build -t ghcr.io/${{ github.repository }}/java-app:${{ github.sha }} . |
| 88 | +
|
| 89 | + - name: Push Docker image to GHCR |
| 90 | + run: docker push ghcr.io/${{ github.repository }}/java-app:${{ github.sha }} |
0 commit comments