Search code examples
springoracle-cloud-infrastructure

GIthub Actions to OCI with Uploading Docker Image


I want to upload Docker Image of Java Spring Boot project to my OCI with Github Actions.

Can I have pipeline Script example?


Solution

  • This is the workflow I created:

    name: Publish Docker image
    
    on:
      release:
        types: [ published ]
    
    env:
      IMAGE_NAME: my-org/my-image
    
    jobs:
      push_to_registry:
      name: Push Docker image to Docker Hub
      runs-on: ubuntu-latest
      steps:
      - name: Check out the repo
        uses: actions/checkout@v4
    
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.IMAGE_NAME }}
    
      - name: Update version in build.gradle.kts
        run: >-
          sed -i "s#{{VERSION_PLACEHOLDER}}#${{ steps.meta.outputs.tags }}#g" build.gradle.kts
    
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: 17
    
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v3
    
      - name: Build docker image
        run: ./gradlew bootBuildImage
    
      - name: Tag docker image
        run: docker tag ${{ env.IMAGE_NAME }} ${{ steps.meta.outputs.tags }}
    
      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
    
      - name: Push docker image to DockerHub
        run: docker push ${{ steps.meta.outputs.tags }}
    

    And added this on the build.gradle.kts:

    // Dockerize
    tasks.named<BootBuildImage>("bootBuildImage") {
        environment.set(
            mapOf("BP_JVM_VERSION" to "17")
        )
        imageName = "my-org/my-image"
    }
    

    This will pick the tag version every time you release and publish the respective version on docker hub