I am new to circleCI and i want to run some spring's tests, so far this is my config.yml file:
# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs:
spring-test:
docker:
- image: cimg/openjdk:17.0.1
steps:
- checkout
- run:
name: "spring tests"
working_directory: ./spring/test/
command: "./gradlew test"
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
say-hello-workflow:
jobs:
- spring-test
But when i run it i get this exceptions on each tests:
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.BeanCreationException at AbstractAutowireCapableBeanFactory.java:1804
Caused by: org.hibernate.service.spi.ServiceException at AbstractServiceRegistryImpl.java:275
Caused by: org.hibernate.HibernateException at DialectFactoryImpl.java:100
I run those test on local and i passed it. I was trying to follow this guide but i could not make it works.
This spring app use postgres as data base, this is the application.properties file:
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=1234
Related
I have an app that does CRUD basically. I am able to run my unit tests locally but on the CI(GitHub Action) it's failing. I am getting the error because of PostgreSQL. Here you can see the error. I couldn't be able to fix that. You can access the whole repository on this LINK. You can see my ci.yaml file below;
name: CI
on:
pull_request:
push:
branches: [develop, main]
concurrency:
group: ci-${{ github.ref }}-group
cancel-in-progress: true
jobs:
default:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Set up JDK
uses: actions/setup-java#v3
with:
java-version: '17'
distribution: 'temurin'
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action#571e99aab1055c2e71a1e2309b9691de18d6b7d6
- name: Build Jar file
run: ./project-dev build-jar
- name: Save Jar file
uses: actions/upload-artifact#v3
with:
name: demo-0.0.1-SNAPSHOT
path: target/demo-0.0.1-SNAPSHOT.jar
retention-days: 1
Can someone help me to run my unit tests on the CI, please?
You need to make sure that the database runs.
Your program expects a Posgres DB named school_management to be available under localhost:5432.
However, such a database isn't available in your script.
For setting up the database, you could use the an existing action like this one :
steps:
- uses: harmon758/postgresql-action#v1
with:
postgresql version: '11'
postgresql db: school_management
postgresql user: learning
postgresql password: sa123456
Alternatively, you could use PosgreSQL service containers as described here:
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: sa123456
POSTGRES_USER: learning
POSTGRES_DB: school_management
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
However this makes it run using a different hostname so you have to change your spring.datasource.url to jdbc:postgresql://localhost:5432/school_management or similar.
Integrated in your workflow, it could look like the following:
name: CI
on:
pull_request:
push:
branches: [develop, main]
concurrency:
group: ci-${{ github.ref }}-group
cancel-in-progress: true
jobs:
default:
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: sa123456
POSTGRES_USER: learning
POSTGRES_DB: school_management
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout#v3
- name: Set up JDK
uses: actions/setup-java#v3
with:
java-version: '17'
distribution: 'temurin'
# override spring.datasource.url
- name: Setup config
run: |
mkdir config
echo 'spring.datasource.url=jdbc:postgresql://postgres:5432/school_management' > config/application.properties
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action#571e99aab1055c2e71a1e2309b9691de18d6b7d6
- name: Build Jar file
run: ./project-dev build-jar
- name: Save Jar file
uses: actions/upload-artifact#v3
with:
name: demo-0.0.1-SNAPSHOT
path: target/demo-0.0.1-SNAPSHOT.jar
retention-days: 1
Another possibility is to use an embedded database like H2 for tests.
With this, you don't have to setup any database.
Looking at your logs line 1351
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Your tests are trying to connect to a local Postgres instance that is not available. Also looking at your tests you have both unit and integration tests. Whereas an integration test needs to load the application context meaning that your running application inside of the pipeline will not be able to connect to Postgres. Hence, all of your integration tests will fail that utilize Postgres.
However, your other tests are passing, line 2085:
2023-02-14 12:13:39.378 INFO 1740 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:d00124ab-b172-4fd1-bf29-b4836ae2f938;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
these are working since your application is connecting correctly to the h2 database that you have.
the StudentRepositoryTest are working since you have the following annotation in your class #DataJpaTest which will boot up this integration test and connect to the in-memory database.
I think the test that is failing is the following DemoApplicationTests:
#SpringBootTest
class DemoApplicationTests {
#Test
void contextLoads() {
}
}
Since this test load the application context (the whole application) and will automatically try to connect with postgres.
So to fix the issue just delete the file. or a better solution which I would recommend (which is a bit more advanced) is to use something called testcontainers and actually run a postgres database inside of a container.
The reason why am suggesting the latter solution is normally once you want to run an integration test you try to have the exact solution that your application runs on production. Hence, an h2 database might have edge cases that does not match postgres database
I have a Spring Boot application with MSSQL database. I would like to run github action and run tests for pull requests and merges to master. However I have problem with connecting to database from GA tests. My application uses YAML configuration and I have separate config file for CI tests.
Here is workflow:
name: Java CI with Maven
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
services:
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
env:
SA_PASSWORD: myPassword
ACCEPT_EULA: 'Y'
DBNAME: test
ports:
- 1433:1433
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 -ntp -U clean test -P junit-ci
And junit-ci config file:
spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://mssql:1433;database=test;
username: sa
password: myPassword
And here is error:
[main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host mssql, port 1433 has failed. Error: "mssql. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:285)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2434)
Suggest to use Testcontainers instead. This way it is the testcontainers Java library which takes care of starting a database to use it for Integration Testing. It is sooo much easier than the path you are currently on. IMHO.
Your GitHub Action YAML then becomes simpler. It will just be pure Maven actions. You also make your test code less dependent on GitHub Actions as your CI system.
Using Testcontainers has other advantages: Your test can have full control over the database (or the container in which it runs). For example you can have a test where you kill the container during the test, thereby similating the effect on your application on a database which suddenly is lost.
Btw: Strictly speaking, what you are attempting is not Unit Tests, but Integration Tests. Maven makes a distinction. It is advisable to make this distinction. Integration Tests are often quite heavy. By using Testcontainers approach the database container will only be started when you tell Maven to execute Integration Tests. In your example, the database container is always started, regardless if it is needed.
I'm currently on my final phase of my bachelor thesis, implementing a web application. Now I need to host it on Azure. I've an application.war with Angular Frontend in it and a services.war with the Java REST Calls. Both are packed through Maven.
When I deploy it from Github with GitHub Actions to Azure, I get the following Error:
"Deployment Failed with Error: Error: More than one package matched with specified pattern: *.war. Please restrain the search pattern."
Do you have a tutorial or can help me how to setup the yml-file to deploy the application.war to ROOT and services.war to /services/...
on:
push:
branches:
- master
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up Java version
uses: actions/setup-java#v1
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install
- name: Upload artifact for deployment job
uses: actions/upload-artifact#v2
with:
name: java-app
path: '${{ github.workspace }}/**/target/*.war'
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact#v2
with:
name: java-app
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy#v2
with:
app-name: 'app-name'
slot-name: 'Production'
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}
package: '*.war'
Thank you very much!
Janick
If you check this Deploy Angular app to Azure App Service running Linux from GitHub, you will understand how to deploy an Angular single page application, from GitHub to Azure App Service.
In order to deploy to App Service using GitHub Actions we need a workflow file. A workflow is defined by a YAML (.yml) file in the /.github/workflows/ path in your repository. This definition contains the various steps and parameters that make up the workflow.
Build and deploy a Java app to Azure using an Azure publish profile. The publish-profile input references the AZURE_WEBAPP_PUBLISH_PROFILE secret that you created earlier.
name: Java CI with Maven
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 1.8
uses: actions/setup-java#v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
working-directory: my-app-path
- name: Azure WebApp
uses: Azure/webapps-deploy#v2
with:
app-name: my-app-name
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: my/target/*.war
For more information check this Deploy to App Service using GitHub Actions document from Microsoft. Also read this Create Azure Web App and deploy using GitHub Actions document from GitHub and check the example templates.
I created a job Maven Project in Jenkins for the purpose to Compile and execute all my automatic tests, but when i build the job i got this error :
i got the same message error for all the scenarios
should i create a pipeline in place of project maven ?
i recupare my project with the link ssh GitLab and im working behind a proxy
Thnaks(y)
Here is a docker-compose file that will open 7 instances of FF and 1 instance on chrome. I use it with azure pipeline but you can integrate it with jenkins. You'll will have to add a jenkins task that runs docker-compose
To try on command line , just install docker desktop ( i use it with mac) and run below command
docker-compose -f /path/of/file up
version: "3"
services:
selenium-hub:
image: selenium/hub:3.141.59-20210607
container_name: selenium-hub
ports:
- "65299:4444"
chrome:
image: selenium/node-chrome:3.141.59-20210607
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
firefox:
image: selenium/node-firefox:3.141.59-20210607
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
deploy:
mode: replicated
replicas: 7
For using docker-compose with azure pipeline , I am using the below.
Make sure you have dockerRegistryEndpoint setup (in below ex: Dockerhub) . I use this to run my cucumber tests and integrate third party cucumber report (PublishCucumberReport#1)in pipeline
trigger:
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build and Push image
jobs:
- job: Build
displayName: Build and Push
pool:
vmImage: 'ubuntu-latest'
steps:
- task: DockerCompose#0
displayName: open browser instances
inputs:
containerregistrytype: 'Container Registry'
dockerRegistryEndpoint: Dockerhub
dockerComposeFile: '**/docker-compose.yml'
action: 'Run a Docker Compose command'
dockerComposeCommand: 'up -d'
detached: true
- task: Maven#3
inputs:
mavenPomFile: 'pom.xml'
mavenOptions: '-Xmx3072m'
jdkArchitectureOption: 'x64'
publishJUnitResults: true
testResultsFiles: '**/target/cucumber.html'
goals: 'clean verify -P acceptanceTests -e -X'
- task: PublishPipelineArtifact#1
displayName: Publish cucumber report
inputs:
pathToPublish: $(System.DefaultWorkingDirectory)/s/target/cucumber-report/
artifactName: 'cucumber.html'
- task: PublishCucumberReport#1
inputs:
jsonDir: ./target/cucumber-report/
outputPath: ./target/
For documentation , refer - https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/docker-compose?view=azure-devops
I am trying to deploy a spring boot microservices application to Google App Engine from Bitbucket Pipelines. I am getting this below error after commiting the code,
INFO: Setting up environment.
echo "${KEY_FILE}" | base64 -d >> /tmp/key-file.json
gcloud auth activate-service-account --key-file /tmp/key-file.json --quiet --verbosity=warning
Activated service account credentials for: [testproject-iamserviceaccoun#testproject.iam.gserviceaccount.com]
gcloud config set project testproject --quiet --verbosity=warning
Updated property [core/project].
> *INFO: Starting deployment to GCP app engine...*
gcloud app --quiet deploy 'app.yaml' --verbosity=warning
**ERROR: (gcloud.app.deploy) [/opt/atlassian/pipelines/agent/build/app.yaml] does not exist.
✖ Deployment failed.**
Please help me to solve this issue. Build Tool used is Maven.
bitbucket-pipelines.yml:
pipelines:
default:
- step:
name: Deploy to Google cloud
deployment: test
script:
- pipe: atlassian/google-app-engine-deploy:0.7.3
variables:
KEY_FILE: $KEY_FILE
PROJECT: 'testproject'
src/main/appengine/app.yaml:
runtime: java
env: flex
handlers:
- url: /.*
script: this field is required, but ignored
You need to specify DEPLOYABLES in your bitbucket-pipelines.yml, pointing to the path of your app.yaml file.
pipelines:
default:
- step:
name: Deploy to Google cloud
deployment: test
script:
- pipe: atlassian/google-app-engine-deploy:0.7.3
variables:
KEY_FILE: $KEY_FILE
PROJECT: 'testproject'
DEPLOYABLES: src/main/appengine/app.yaml
Reference: https://bitbucket.org/atlassian/google-app-engine-deploy/src/master/