Skip to content

#SBCOSS-520 feat: adding github actions to build and publish the dock… #3

#SBCOSS-520 feat: adding github actions to build and publish the dock…

#SBCOSS-520 feat: adding github actions to build and publish the dock… #3

Workflow file for this run

name: Build and Push Docker Image
on:
push:
#tags:
#- '*'
jobs:
root-pom-build:
runs-on: ubuntu-latest
outputs:
maven-cache-key: ${{ steps.cache-maven.outputs.cache-primary-key }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Cache Maven packages
id: cache-maven
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: 'maven'
- name: Set up Maven 3.8.7
uses: s4u/setup-maven-action@v1.11.0
with:
maven-version: 3.8.7
- name: Build root POM and all modules
run: |
# Build root POM first
mvn clean install -N -DskipTests
# Build all modules (jobs-core, notification, lms-jobs, user-org-jobs, ml-jobs)
mvn clean install -DskipTests
- name: Upload Maven local repository
uses: actions/upload-artifact@v4.6.2
with:
name: maven-repo
path: ~/.m2/repository
retention-days: 1
build-and-push-docker:
needs: root-pom-build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Restore Maven local repository
uses: actions/download-artifact@v4.3.0
with:
name: maven-repo
path: ~/.m2/repository
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: 'maven'
- name: Set up Maven 3.8.7
uses: s4u/setup-maven-action@v1.11.0
with:
maven-version: 3.8.7
- name: Login Docker Registry
run: |
case "${{ vars.REGISTRY_PROVIDER }}" in
"gcp")
echo "${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}" | base64 --decode > $HOME/gcloud-key.json
gcloud auth activate-service-account --key-file=$HOME/gcloud-key.json
gcloud auth configure-docker ${{ secrets.REGISTRY_NAME }}
REGISTRY_URL=$(echo "${{ secrets.REGISTRY_URL }}" | tr '[:upper:]' '[:lower:]')
;;
"azure" | "dockerhub")
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login "${{ secrets.REGISTRY_NAME }}" \
--username "${{ secrets.REGISTRY_USERNAME }}" --password-stdin
REGISTRY_URL="$(echo "${{ secrets.REGISTRY_URL }}/$(basename "${{ github.workspace }}")" | tr '[:upper:]' '[:lower:]')"
;;
*)
REPO_NAME_LOWERCASE=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]')
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
REGISTRY_URL="ghcr.io/$REPO_NAME_LOWERCASE"
;;
esac
echo "REGISTRY_URL=${REGISTRY_URL}" >> $GITHUB_ENV
- name: Build jobs-distribution
working-directory: jobs-distribution
run: |
mvn package -DskipTests
- name: Build Docker Image
working-directory: jobs-distribution
run: |
IMAGE_TAG=$(echo "${{ github.ref_name }}_$(echo $GITHUB_SHA | cut -c1-7)" | tr '[:upper:]' '[:lower:]')
docker build -t ${REGISTRY_URL}:${IMAGE_TAG} .
echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV
- name: Push Docker Image
run: |
docker push ${REGISTRY_URL}:${IMAGE_TAG}
echo "Pushed Docker image: ${REGISTRY_URL}:${IMAGE_TAG}"