Configuring gitlab-ci.yml for simple java project - java

I have a simple java project with HelloWorld class , I want to add the GitLab CI/CD for this project by using the .gitlab-ci.yml and runnable configuration files , I am trying to find the reference but am unable to find for simple java project , any help in configuring the above project is appreciated.
Thanks
Rama

I used this as a simple setup for my .gitlab-ci.yml
build:
stage: build
image: openjdk:8-jdk
script: javac src/javaTest/JavaTest.java
artifacts:
untracked: true
https://gitlab.com/peterfromearth/java-test/blob/master/.gitlab-ci.yml

That will build & test your application using gradle :
image: gradle:alpine
stages:
- build
- test
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
build:
stage: build
script:
gradle --build-cache assemble
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
test:
stage: test
script:
gradle check
after_script:
- echo "End CI"

Related

Buildx failed with error: "No such file or directory" when trying to push docker on Github

I'm very new to Docker and I'm trying to get my Github repository setup with a docker when I push an update. However, when the github workfile script goes to push my code to Docker I get the following error.
Error: buildx failed with: error: failed to solve: failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount1164878607/target: lstat /var/lib/docker/tmp/buildkit-mount1164878607/target: no such file or directory
It appears Docker can't access the file I set for it in my Dockerfile yet no amount of tweaking with the code appears to fix it, aside for me completely removing the COPY tag which renders my Docker unrunnable, though it does send. I'm using maven compiler plugin along with maven shade plugin and have setup my pom.xml file accordingly.
Here is my Github WorkFile:
name: Docker
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Set up JDK
uses: actions/setup-java#v3.3.0
with:
java-version: 18
distribution: adopt
- name: Build with Maven
run: mvn clean package
- name: Login to DockerHub
uses: docker/login-action#v2.0.0
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push Docker images
uses: docker/build-push-action#v3.0.0
with:
push: true
tags: sudden/discordbot:latest
Here is my DockerFile:
FROM openjdk:11-jdk-slim
WORKDIR /tmp
COPY target/discordbot-1.0-SNAPSHOT-shaded.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
What I've tried:
Copying another random file in my "src" directory.
Changing the name of my jar file and changing the path.
Changing my WORKDIR.
Changed the Docker's "context" and other tags in my Workfile.
Any help is appreciated. This might be a simple fix, but I've been stuck on it for a day despite my best efforts so asking here is a last resort. Thanks.
Make sure that you haven't excluded your target folders in your .dockerignore file.
That's what fixed it for me after several days of despairing search...

How to fix this workflow issue "there is no POM in this directory" in GitHub Actions? (Applying the concept of calling reusable workflows)

I was trying apply the concept of calling reusable workflows. I have a workflow for automated tests in repo1, and an actions workflow in repo2. The goal is to call on workflow/s from another repository and test execution/s should be successful.
However, I have encountered an error that says "The goal you specified requires a project to execute but there is no POM in this directory (D:\a\github-actions-course-test\github-actions-course-test). Please verify you invoked Maven from the correct directory. -> [Help 1]"
Please see images/details below. How do we solve this kind of issue/s? Sorry I'm new to github actions since this will be used in my project for the CI/CD. I may have missed something or probably this is not the right way of reusing workflows. I'm open for suggestions and ideas that might resolve this issue. Thank you so much!!
error:
The goal you specified requires a project to execute but there is no POM in this directory (D:\a\github-actions-course-test\github-actions-course-test). Please verify you invoked Maven from the correct directory. -> [Help 1]
repo 1 workflow
name: test_execution
on:
schedule:
- cron: "0 9,12,15,18,20,0,3,6 * 1-12 1-5"
push:
branches: [ master ]
workflow_call:
secrets:
SOME_SECRET:
required: true
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout#v3
- name: Set up JDK 11
uses: actions/setup-java#v3
with:
java-version: '11'
distribution: 'adopt'
- name: Build with Maven and Test execution of Google Smoke Test
run: mvn clean test -DsuiteXmlFile="google.xml"
repo 2 workflow
name: Actions Workflow
on:
push:
branches: [main]
jobs:
run-github-actions:
runs-on: windows-latest
steps:
- name: List Files
run: |
ls
echo $GITHUB_SHA
echo $GITHUB_REPOSITORY
echo $GITHUB_WORKSPACE
echo "${{ github.token }}"
echo "for testing purposes"
automation-test:
needs: ["run-github-actions"]
uses: mirandaxyrus13/testautomationframework/.github/workflows/test_execution.yml#master
secrets:
SOME_SECRET: ${{ secrets.SOME_SECRET }}

Azure Linux Webapp cannot seem to find deployed Spring .jar from DevOps repo

I have a Spring Web Application in a DevOps repository, with a .yml that looks like this (as generated by the tool in the DevOps web client):
# Build your Java project and deploy it to Azure as a Linux web app
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/java
trigger:
- master
variables:
Version: '0.0.1'
# Azure Resource Manager connection created during pipeline creation
azureSubscription: '-snipped-'
# Web app name
webAppName: 'SlackCentralTestApp'
# Environment name
environmentName: 'SlackCentralTestApp'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: MavenPackageAndPublishArtifacts
displayName: Maven Package and Publish Artifacts
pool:
vmImage: $(vmImageName)
steps:
- task: Maven#3
displayName: 'Maven Package'
inputs:
mavenPomFile: 'pom.xml'
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.11'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
- task: CopyFiles#2
displayName: 'Copy Files to artifact staging directory'
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)'
Contents: '**/target/SlackbotTest-$(Version)-SNAPSHOT.?(war|jar)'
TargetFolder: $(Build.ArtifactStagingDirectory)
- upload: $(Build.ArtifactStagingDirectory)
artifact: drop
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: DeployLinuxWebApp
displayName: Deploy Linux Web App
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp#1
displayName: 'Azure Web App Deploy: SlackCentralTestApp'
inputs:
azureSubscription: 'Azure for Students (4998490e-1bc4-43fc-a370-80744706d1f5)'
appType: 'webAppLinux'
appName: 'SlackCentralTestApp'
package: '$(Pipeline.Workspace)/drop/target/SlackbotTest-$(Version)-SNAPSHOT.jar'
runtimeStack: 'JAVA|11-java11'
startUpCommand: 'java -jar $(Pipeline.Workspace)/drop/target/SlackbotTest-$(Version)-SNAPSHOT.jar'
The deployment process seems to be successful as seen in this screenshot, yet when I take a look at the server log, I sadly get greeted with the following error:
2020-04-14T09:03:55.658599508Z Initializing App Insights applicationinsights-agent-codeless-2.5.0.jar....
2020-04-14T09:03:55.669449167Z STARTUP_FILE=
2020-04-14T09:03:55.676322304Z STARTUP_COMMAND=java -jar /home/vsts/work/1/drop/target/SlackbotTest-0.0.1-SNAPSHOT.jar
2020-04-14T09:03:55.676350305Z No STARTUP_FILE available.
2020-04-14T09:03:55.676428905Z Running STARTUP_COMMAND: java -jar /home/vsts/work/1/drop/target/SlackbotTest-0.0.1-SNAPSHOT.jar
2020-04-14T09:03:55.681352232Z Error: Unable to access jarfile /home/vsts/work/1/drop/target/SlackbotTest-0.0.1-SNAPSHOT.jar
2020-04-14T09:03:55.694784805Z Finished running startup command 'java -jar /home/vsts/work/1/drop/target/SlackbotTest-0.0.1-SNAPSHOT.jar'. Exiting with exit code 1.
This would lead me to conclude that the path as given in the 'startUpCommand' property from the .yml file is incorrect, yet I have not been able to find what the correct path should be.
I have attempted the following:
Specify no directories, only the filename. Leads to the same result, sadly.
Use the 'find' command in Bash to find any .jars on the Web App, which tells me that there are none.
Manually building the application from the Web App does not seem to be an option either, as it's a Java 11 application and the java SE that seems to be included is version 1.8, regardless of the version I specify during the creation of the Web App resource in Azure.

Reading a file from resources not working in gitlab CI

I am getting an error while running tests on gitlab CI using the command:
./gradlew clean test
I am using test containers to run my tests: https://www.testcontainers.org/modules/docker_compose/
Here is the code I am using to load the docker compose file which is at src/test/resources.
#ClassRule
public static DockerComposeContainer container = new DockerComposeContainer(new File(BaseServiceTest.class.getClassLoader().getResource("docker-compose.yml").getFile()));
It runs fine when I run locally, buy when running ci on gitlab, I get the following error:
{"timestamp":"2019-06-17T18:47:44.903Z","level":"ERROR","thread":"Test worker","logger":"docker[docker/compose:1.8.0]","message":"Log output from the failed container:\n.IOError: [Errno 2] No such file or directory: '/builds/xxxx/xxxx/xxxx-xxxx/src/test/resources/docker-compose.yml'\n","context":"default"}
Following is my gitlab-ci.yml file:
include:
- project: 'xxxx/xxxx/sdlc'
file: '/sdlc.yml'
variables:
CONTAINER_NAME: xxxx-xxxx
test:
stage: test
image: registry.xxxx.com/xxxx-alpine-jdk8:v1_8_181
script:
- ls -la src/test/resources
- ./gradlew clean test -i
In my script, I have ls -la src/test/resources and I can see the docker-compose.yml file when that script is run. Not sure why it is not available when running the code.
Based on the Testcontainers docs here.
DinD service is required for Testcontainers on Gitlab CI
Here an example:
# DinD service is required for Testcontainers
services:
- docker:dind
variables:
# Instruct Testcontainers to use the daemon of DinD.
DOCKER_HOST: "tcp://docker:2375"
# Improve performance with overlayfs.
DOCKER_DRIVER: overlay2
test:
image: gradle:5.0
stage: test
script: ./gradlew test

How to configure continuous integration for aws lambda with java using the services of aws aws-codebuild, codedeploy, cloudformation

I am implementing a lambda function with the tool of continuous integrations of aws . CodeSource , CodeBuild CodePipeLine.
After set up all, when i test the lambda the result is
{
"errorMessage": "Class not found: com.ad.client.App",
"errorType": "java.lang.ClassNotFoundException"
}
Class not found: com.ad.client.App: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.ad.client.App
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
All stage of Pipeline are succeeded(Source , Build , Deploy)
If a load the jar directly in the lambda console the result is the correct
I review the log of the build and found this:
[Container] 2019/06/13 13:09:38 Running command echo THE PATH WORK IS !!!
THE PATH WORK IS !!!
[Container] 2019/06/13 13:09:38 Running command pwd
/codebuild/output/src748698927/src
[Container] 2019/06/13 13:09:38 Running command echo The list of file is !!
The list of file is !!
[Container] 2019/06/13 13:09:38 Running command ls
Readme.md
buildspec.yml
dependency-reduced-pom.xml
ftc-client.iml
outputtemplate.yaml
pom.xml
src
target
template.yaml
[Container] 2019/06/13 13:09:38 Running command echo CODE BUILD SRC DIRECTORY
CODE BUILD SRC DIRECTORY
[Container] 2019/06/13 13:09:38 Running command echo $CODEBUILD_SRC_DIR
/codebuild/output/src748698927/src
INFO] skip non existing resourceDirectory /codebuild/output/src748698927/src/src/main/resources
In some portion of code show me that the path src is duplicated, i don't know if it has something to related with the problem
My config files are:
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Ftc-client
Resources:
FtcClientFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.ad.client.App::handleRequest
Runtime: java8
CodeUri: ./
Events:
MyFtcClientApi:
Type: Api
Properties:
Path: /client
Method: GET
buildspec.yml
version: 0.2
phases:
install:
runtime-versions:
java: openjdk8
build:
commands:
- echo Build started on `date`
- mvn test
- export BUCKET=my-bucket-for-test
- aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
finally:
- echo THE PATH WORK IS !!!
- pwd
- echo The list of file is !!
- ls
- echo CODE BUILD SRC DIRECTORY
- echo $CODEBUILD_SRC_DIR
post_build:
commands:
- echo Build completed on `date`
- mvn package
artifacts:
files:
- target/ftc-client-1.0-SNAPSHOT.jar
- template.yaml
- outputtemplate.yaml
discard-paths: yes
The source code structure is :
/fclient/src/main/java/com/ad/App.java
/tclient/buildspec.yml
/fclient/pom.xml
/fclient/template.yaml
I want to make this but with Java : https://docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html
thank for everyone whom can give me a cue
This is the solution - it was necessary to unzip the jar in root of my code:
version: 0.2
phases:
install:
runtime-versions:
java: openjdk8
pre_build:
commands:
- echo Test started on `date`
- mvn clean compile test
build:
commands:
- echo Build started on `date`
- export BUCKET=my-bucket-for-test
- mvn package shade:shade
- mv target/ftc-client-1.0-SNAPSHOT.jar
- unzip ftc-client-1.0-SNAPSHOT.jar
- rm -rf target tst src buildspec.yml pom.xml ftc-client-1.0-SNAPSHOT.jar
- aws cloudformation package --template-file template.yaml --s3-bucket $BUCKET --output-template-file outputtemplate.yaml
post_build:
commands:
- echo Build completed on `date` !!!
artifacts:
files:
- target/ftc-client-1.0-SNAPSHOT.jar
- template.yaml
- outputtemplate.yaml
https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html

Categories