Github action cannot download artifact from Github package repository - maven

I have two different repositories. One for the library an another for the application that needs the library from the other repository. When I run the Github action of the second repository I got a
Authentication failed for https://maven.pkg.github.com/XXXX/YYYY/file.pom 401 Unauthorized
I tried already to change the maven settings with (this is working when I change my local maven settings and install my application):
- uses: DamianReeves/write-file-action#v1.0
with:
path: ~/.m2/settings.xml
contents: ${{ secrets.MAVEN_SETTINGS }}
write-mode: overwrite
Settings.xml file was:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
Also I tried using maven-settings-action:
- name: Configure Maven
uses: s4u/maven-settings-action#v2.2.0
with:
servers: |
[{
"id": "imdb-github",
"username": "${{ secrets.MAVEN_USERNAME }}",
"password": "${{ secrets.MAVEN_PASSWORD }}"
}]
I also tried to use the GITHUB_TOKEN for it but without any luck.
The pom.xml file of the project that needs the library has a repositories section that looks like:
<repositories>
<repository>
<id>imdb-github</id>
<url>https://maven.pkg.github.com/user/repo</url>
</repository>
</repositories>
Does somebody knows the solution for this?

I found the issue. My flow was using actions/cache after s4u/maven-settings-action. So the cache overwrite the settings file. Removing the actions/cache step the issue was resoled. Also swapping the actions/cache and s4u/maven-settings-action so that the maven-settings-action is after actions/cache has solved my issue.

In order to diagnose such issue, please run maven with debug options -X and diagnose logs which serverId is used.
mvn -X ...
Please also run the same on local environment.
Configuration look ok, so cause maybe wrong user name or password are provided

Related

Github action push java artifact to github repo

I have a Github repo project using GitHub actions with a docker file used to build the SpringBoot Java project.
I want to download packages from the Github repo for custom artifacts from GitHub repo and also be able upload artifact to it.
So I followed the link Configuring Apache Maven for use with GitHub Packages by adding the section to a settings.xml file:
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>${GITHUB_TOKEN}</password>
</server>
</servers>
</settings>
To publish the package from the dockerfile build I added the following to my pom.xml:
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</distributionManagement>
The following is the content of my dockerfile:
FROM adoptopenjdk/maven-openjdk10 as build
WORKDIR /app
ADD pom.xml /app/pom.xml
ADD src /app/src
ADD settings.xml /root/.m2/settings.xml
RUN ["mvn", "clean", "install", "deploy"]
Is it possible to deploy from the dockerfile to Github repo? Somehow the deploy piece does not seem to work. I have tried few times but not sure what's wrong with my sections.
Currently this is my error in my docker build:
Could not transfer metadata
com.chg.sa:demo-sa-java-service:1.0-SNAPSHOT/maven-metadata.xml
from/to github (https://maven.pkg.github.com/OWNER/REPOSITORY): Not
authorized -> [Help 1]
I got the push working using Dockerfile building by passing in the github token as build arg and switching the owner and repo name to their values.

How to see what Maven is sending to a server during deploy?

I'm trying to use Github's new Actions CI server to deploy packages to Github's new packages feature. It's not going well.
I think it's all set up correctly, but I get this error:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy
(default-deploy) on project myproject: Failed to deploy artifacts: Could not
find artifact com.mycompany:myproject:pom:1.5 in github
(https://maven.pkg.github.com/mycompany/mycompany_repository) -> [Help 1]
This happens after it appears to upload that same pom successfully:
Uploading to github: https://maven.pkg.github.com/mycompany/mycompany_repository
/com/mycompany/myproject/1.5/myproject-1.5.pom
Progress (1): myproject-1.5.pom (4.1/6.1 kB)
Progress (1): myproject-1.5.pom (6.1 kB)
So, it looks to me like it is successfully uploading the pom, but then it fails to download the same pom a few seconds later.
I'm running the deploy with debug switches on: mvn -X -e deploy, but I can't see the exact http commands that Maven is sending to the server.
How do I debug this? Is there some Maven/Aether transport or something that will log what is going on under the covers?
In case anyone else lands here looking for a solution to OPs issue publishing to github, I had a similar issue and found that the URLs needed in settings.xml and pom.xml are inconsistent. In your settings.xml, the repo URL needs to be of the form https://maven.pkg.github.com/myuser/com/mycompany/mypackage, whereas in your project's pom file, it needs to be of the form https://maven.pkg.github.com/myuser/mypackage. So, for example, your settings.xml file in ~/.m2 would look something like this:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/myuser/com/mycompany/mypackage</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>myuser</username>
<password>mypersonalaccesstoken</password>
</server>
</servers>
</settings>
Whereas the pom.xml file in the root of your project would need to look like this:
<project>
...
<groupId>org.mycompany</groupId>
<artifactId>mypackage</artifactId>
<version>1.0.0</version>
...
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/myuser/mypackage</url>
</repository>
</distributionManagement>
...
</project>
Other than this minor (but crucial) detail, my steps were the same as those outlined here. This allowed me to publish my Maven package to github package registry.
You can enable debug logging in the workflows.
Just add the secret:
ACTIONS_RUNNER_DEBUG
And set to true
See a similar answer here
I just spend 3 hours debugging why the guide on the page did not work for me. If you are following the guide posted here 1.
OWNER is your github username, and REPOSITORY is - you guessed it, the repo name.
Just remember to use lowercase in both OWNER and REPOSITORY.
When generating the personal access token, make sure the scopes for the token are the repo:* scopes as well as the more obvious write:packages and read:packages scopes (do not disable the repo scopes)
Otherwise it does just that
The following solution works for me:
Create a repository for packages e.g. maven-packages
Add <server></server> settings under <servers> in settings.xml: (do this per id used below)
<server>
<id>github</id>
<username>YOUR GITHUB USERNAME</username>
<password>A GITHUB TOKEN YOU CREATE FOR PUBLISHING PACKAGES</password>
</server>
Do NOT add <activeProfiles>, <profile> or <repositories> to settings.xml (only add <server> elements) as this is redundant for publishing and I am adding them to consuming projects' maven.xml so no need for duplication.
Add repository/ies to distributionManagement in pom.xml as follows:
<distributionManagement>
<snapshotRepository>
<id>github-snapshot</id>
<name>GitHub snapshot</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
<repository>
<id>github-release</id>
<name>GitHub release</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
<uniqueVersion>false</uniqueVersion>
</repository>
</distributionManagement>
Where OWNER is the GitHub account your project is / projects are under and maven-packages is the repositories you want to publish you project(s) to.
This enables using a dedicated repository for listing packages instead of publishing each project's package to a different (its own) repository, making consumption of multiple packages from your GitHub account easier, as you only need to configure a single repository for these packages:
<repositories>
<repository>
<id>github</id>
<name>GitHub</name>
<url>https://maven.pkg.github.com/OWNER/maven-packages/</url>
</repository>
</repositories>
Note: in the <servers> section of your settings.xml define a <server> per id used in repositories and distributionManagement e.g. github-snapshot, github-release, github in the above examples.

Configure Maven settings for deployment

I want to deploy artifacts to Nexus from Jenkins to different repositories (like builds-all, builds-verified, releases). The thing is that I want to keep minimal configuration in the project POM file. My settings file now looks like:
<servers>
<server>
<id>orion-nexus</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
<localRepository>~/.m2/repository</localRepository>
<profiles>
<!-- Deployment configuration for CI builds for mainline -->
<profile>
<id>build</id>
<repositories>
<repository>
<id>builds-all</id>
<url>http://orion-nexus:8081/</url>
<snapshots>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
Project POM:
<distributionManagement>
<repository>
<id>orion-nexus</id>
<layout>default</layout>
<url><!-- how to avoid explicit URL? --></url>
</repository>
</distributionManagement>
I wan to run deploy like mvn -B -P build clean install deploy. However, I don't understand how to avoid setting explicit URL in distribution management section. Can I set a variable in settings file and propagate it to my POM?
Is there any step-by-step guide for such workflow?
You can declare a property inside a profile on your settings.xml and use its name within <distributionManagement/> configuration.
settings.xml
<profiles>
<profile>
<id>distmgt</id>
<properties>
<distUrl>scp://...</distUrl>
<properties/>
</profile>
</profiles>
pom.xml
<distributionManagement>
<repository>
<id>orion-nexus</id>
<layout>default</layout>
<url>${distUrl}</url>
</repository>
</distributionManagement>
And finally
mvn -P distmgt clean deploy
You can avoid the -P build params using activation.

Why Artifactory Maven and Oracle fail to communicate?

I hope an answer will be a universal guide on how to connect Maven, Oracle And Artifactory and will become most usefull webpage on the net. Most likely this question will be marked down, but I just give up on Maven, Artifactory and Oracle.
I am running windows 7-64 with maven installed as a part of Oracle jdev. Company has Artifactory setted up and running on vpn network.... but somethings are not right.
Following simple instructions on:
http://biemond.blogspot.co.uk/2013/07/maven-support-in-weblogic-jdeveloper.html
fails at first instruction:
Install the oracle maven sync plugin to your local repository
the command:
mvn deploy:deploy-file -DpomFile=oracle-maven-sync.12.1.2.pom -Dfile=oracle-maven-sync.12.1.2.jar
the error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: The parameters 'url' for goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file are missing or invalid -> [Help 1]
adding
-Durl=file://C:\Users\{user}\.m2
seems to install that artifact to my local .m2 repo
C:\Users\{user}\.m2\com\oracle\maven\oracle-maven-sync\12.1.2-0-0
but only seems, then I get to number 3 on that tutorial:
mvn com.oracle.maven:oracle-maven-sync:help
gives an error:
[ERROR] Error resolving version for plugin 'com.oracle.maven:oracle-maven-sync' from the repositories [local (C:\Users\{user}\.m2), central (http://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
So the plugin failed to install?
Oracle Maven repository is password protected due to weird oracle thoughts, Artifactory has external Central and Oracle repositories why letting it mirror everything stops maven from even finding deploy module?
here is my settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servers>
<server>
<username>user</username>
<password>xxx</password>
<id>central</id>
</server>
<server>
<username>user</username>
<password>xxx</password>
<id>snapshots</id>
</server>
</servers>
<profiles>
<profile>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release-local</name>
<url>http://art.host.ru:8081/artifactory/libs-release-local</url>
</repository>
<repository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot-local</name>
<url>http://art.host.ru:8081/artifactory/libs-snapshot-local</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-release-local</name>
<url>http://art.host.ru:8081/artifactory/plugins-release-local</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshot-local</name>
<url>http://art.host.ru:8081/artifactory/plugins-snapshot-local</url>
</pluginRepository>
</pluginRepositories>
<id>artifactory</id>
</profile>
<profile>
<id>oracle-maven</id>
<properties>
<oracle-maven-sync.oracleHome>C:\Oracle\Middleware\Oracle_Home</oracle-maven-sync.oracleHome>
<oracle-maven-sync.testOnly>false</oracle-maven-sync.testOnly>
<oracle-maven-sync.failOnError>false</oracle-maven-sync.failOnError>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>oracle-maven</activeProfile>
</activeProfiles>
</settings>
Well, I think you just follow the wrong guide. All this manual deployment of some random maven plugins is just wrong.
Just follow the simple and detailed instructions in the Artifactory User Guide and you're good to go.
The problem is with the -Dmaven.repo.local
Do not provide the argument.
Use http://jcenter.bintray.com for most of the artifacts
And for Oracle: oracle-maven-sync plugin
oracle instructions
$ORACLE_HOME = where jdev 12.1.2
$M2_HOME = $ORACLE_HOME/oracle_common/modules/org.apache.maven_3.0.4
$M2 = $M2_HOME/bin (add to $PATH)
artifactory->home->maven settings generate settings.xml put it in ~/.m2
create local repo in artifactory"oracle-local-12.1.2"
settings.xml add following code
<profiles>
<profile>
<properties>
<oracle-maven-sync.oracleHome>$ORACLE_HOME</oracle-maven-sync.oracleHome>
<oracle-maven-sync.testOnly>false</oracle-maven-sync.testOnly>
<oracle-maven-sync.failOnError>false</oracle-maven-sync.failOnError>
<oracle-maven-sync.serverId>oracle-local-12.1.2</oracle-maven-sync.serverId></properties>
artifactory -> username(top right corner) -> enter pass -> unlock -> забрать encrypted password(copy)
add aquired pass to settings.xml for all
go to $ORACLE_HOME/oracle_common/plugins/maven/com/oracle/maven/oracle-maven-sync/12.1.2 (use cmd/terminal)
mvn deploy:deploy-file -DpomFile=oracle-maven-sync-12.1.2.pom -Dfile=oracle-maven-sync-12.1.2.jar
test it: mvn com.oracle.maven:oracle-maven-sync:help
push artifacts oracle 12.1.2 mvn com.oracle.maven:oracle-maven-sync:push
long time wait (~3000 artifacts > 2 hours)
complete set up of repos and in settings.xml

Maven deployment user can deploy, but not read repository

The last few days I was trying to set a maven-development environment up. We're using TeamCity for the CI, SonarQube for analysis and SonaType Nexus for the repository management.
TeamCity and SonarQube are working like a charm - The nexus however gives us lots of trouble.
This kind of set up is nothing special, I've done it several times now. This time however, I got a very weird bug: TeamCity is able to deploy artifacts to the nexus, but gives an org.apache.maven.wagon.authorization.AuthorizationException: Not authorized , ReasonPhrase:Unauthorized. error when reading dependencies from the nexus.
I even tried changing the user that TeamCity uses from deployment to admin, same issue. chown returned the correct values as well (owned by the "buildagent" user).
I seriously have no clue what could cause this issue, I already tried reinstalling the nexus 3 times, even added the admin role to the "deployment" user - no change whatsoever.
The settings.xml the "buildagent" user uses has the following content (passwords marked with XXX):
<servers>
<!-- This is the username password used to access the nexus repository -->
<server>
<id>central</id>
<username>deployment</username>
<password>XXX</password>
</server>
<server>
<id>rn-releases</id>
<username>deployment</username>
<password>XXX</password>
</server>
<server>
<id>rn-snapshots</id>
<username>deployment</username>
<password>XXX</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<url>http://build.example.com:8301/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
For your information: The build log from TeamCity states that the settings are getting read correctly as well, so that's not the cause either.
I really would like to fix this asap, since we have developers waiting to continue on their projects (and we don't want to give out the url if it's not secured, we have private projects running on the CI).
Thanks in advance!
Edit:
So I now even tried installing artifactory - and I still have the same issue. It seems to be something with either A: Maven or B:TeamCity.
The mirror to Nexus has the id nexus, but there's no <server>-entry with this id. If you add that to the settings.xml with the correct credentials, it should all work again.

Resources