Unverified Commit d8ada524 authored by Konrad Pabjan's avatar Konrad Pabjan Committed by GitHub
Browse files

Merge pull request #29 from clarkbw/maven-auth

Adding maven auth support
parents cfdbba3e 9b11fe4b
Loading
Loading
Loading
Loading
+2
Changes for .gitattributes: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
dist/index.js    -diff -merge
dist/index.js    linguist-generated=true
+1
Changes for .gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -93,3 +93,4 @@ typings/

# DynamoDB Local files
.dynamodb/
.vscode/
+123 −3
Changes for README.md: 123 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ This action sets up a java environment for use in actions by:

See [action.yml](action.yml)

Basic:
## Basic
```yaml
steps:
- uses: actions/checkout@v1
@@ -25,7 +25,7 @@ steps:
- run: java -cp java HelloWorldApp
```

From local file:
## Local file
```yaml
steps:
- uses: actions/checkout@v1
@@ -37,7 +37,7 @@ steps:
- run: java -cp java HelloWorldApp
```

Matrix Testing:
## Matrix Testing
```yaml
jobs:
  build:
@@ -56,6 +56,126 @@ jobs:
      - run: java -cp java HelloWorldApp
```

## Publishing using Apache Maven
```yaml
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - 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

    - name: Publish to GitHub Packages Apache Maven
      run: mvn deploy
      env:
        GITHUB_TOKEN: ${{ github.token }} # GITHUB_TOKEN is the default env for the password

    - name: Set up Apache Maven Central
      uses: actions/setup-java@v1
      with: # running setup-java again overwrites the settings.xml
        java-version: 1.8
        server-id: maven # Value of the distributionManagement/repository/id field of the pom.xml
        server-username: MAVEN_USERNAME # env variable for username in deploy
        server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy

    - name: Publish to Apache Maven Central
      run: mvn deploy 
      env:
        MAVEN_USERNAME: maven_username123
        MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
```

The two `settings.xml` files created from the above example look like the following.

`settings.xml` file created for the first deploy to GitHub Packages
```xml
<servers>
    <server>
      <id>github</id>
      <username>${env.GITHUB_ACTOR}</username>
      <password>${env.GITHUB_TOKEN}</password>
    </server>
</servers>
```

`settings.xml` file created for the second deploy to Apache Maven Central
```xml
<servers>
    <server>
      <id>maven</id>
      <username>${env.MAVEN_USERNAME}</username>
      <password>${env.MAVEN_CENTRAL_TOKEN}</password>
    </server>
</servers>
```

***NOTE: The `settings.xml` file is created in the Actions $HOME directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.***	

See the help docs on [Publishing a Package](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages#publishing-a-package) for more information on the `pom.xml` file.

## Publishing using Gradle
```yaml
jobs:

  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1

    - name: Set up JDK 1.8
      uses: actions/setup-java@v1

    - name: Build with Gradle
      run: gradle build

    - name: Publish to GitHub Packages
      run: gradle publish
      env:
        USERNAME: ${{ github.actor }}
        PASSWORD: ${{ secrets.GITHUB_TOKEN }}
```

***NOTE: The `USERNAME` and `PASSWORD` need to correspond to the credentials environment variables used in the publishing section of your `build.gradle`.***	

See the help docs on [Publishing a Package with Gradle](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages#example-using-gradle-groovy-for-a-single-package-in-a-repository) for more information on the `build.gradle` configuration file.

## Apache Maven with a settings path

When using an Actions self-hosted runner with multiple shared runners the default `$HOME` directory can be shared by a number runners at the same time which could overwrite existing settings file. Setting the `settings-path` variable allows you to choose a unique location for your settings file.

```yaml
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up JDK 1.8 for Shared Runner
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
        server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
        settings-path: ${{ github.workspace }} # location for the settings.xml file

    - name: Build with Maven
      run: mvn -B package --file pom.xml

    - name: Publish to GitHub Packages Apache Maven
      run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
      env:
        GITHUB_TOKEN: ${{ github.token }}
```

# License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
+144
Changes for __tests__/auth.test.ts: 144 added lines, 0 removed lines.
Original line number Diff line number Diff line
import io = require('@actions/io');
import fs = require('fs');
import os = require('os');
import path = require('path');

// make the os.homedir() call be local to the tests
jest.doMock('os', () => {
  return {
    homedir: jest.fn(() => __dirname)
  };
});

import * as auth from '../src/auth';

const m2Dir = path.join(__dirname, auth.M2_DIR);
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);

describe('auth tests', () => {
  beforeEach(async () => {
    await io.rmRF(m2Dir);
  }, 300000);

  afterAll(async () => {
    try {
      await io.rmRF(m2Dir);
    } catch {
      console.log('Failed to remove test directories');
    }
  }, 100000);

  it('creates settings.xml in alternate locations', async () => {
    const id = 'packages';
    const username = 'UNAMI';
    const password = 'TOLKIEN';

    const altHome = path.join(__dirname, 'runner', 'settings');
    const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE);
    process.env[`INPUT_SETTINGS-PATH`] = altHome;
    await io.rmRF(altHome); // ensure it doesn't already exist

    await auth.configAuthentication(id, username, password);

    expect(fs.existsSync(m2Dir)).toBe(false);
    expect(fs.existsSync(settingsFile)).toBe(false);

    expect(fs.existsSync(altHome)).toBe(true);
    expect(fs.existsSync(altSettingsFile)).toBe(true);
    expect(fs.readFileSync(altSettingsFile, 'utf-8')).toEqual(
      auth.generate(id, username, password)
    );

    delete process.env[`INPUT_SETTINGS-PATH`];
    await io.rmRF(altHome);
  }, 100000);

  it('creates settings.xml with username and password', async () => {
    const id = 'packages';
    const username = 'UNAME';
    const password = 'TOKEN';

    await auth.configAuthentication(id, username, password);

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(id, username, password)
    );
  }, 100000);

  it('overwrites existing settings.xml files', async () => {
    const id = 'packages';
    const username = 'USERNAME';
    const password = 'PASSWORD';

    fs.mkdirSync(m2Dir, {recursive: true});
    fs.writeFileSync(settingsFile, 'FAKE FILE');
    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);

    await auth.configAuthentication(id, username, password);

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(id, username, password)
    );
  }, 100000);

  it('does not create settings.xml without required parameters', async () => {
    await auth.configAuthentication('FOO');

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD)
    );

    await auth.configAuthentication(undefined, 'BAR', undefined);

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD)
    );

    await auth.configAuthentication(undefined, undefined, 'BAZ');

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ')
    );

    await auth.configAuthentication();

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(
        auth.DEFAULT_ID,
        auth.DEFAULT_USERNAME,
        auth.DEFAULT_PASSWORD
      )
    );
  }, 100000);

  it('escapes invalid XML inputs', () => {
    const id = 'packages';
    const username = 'USER';
    const password = '&<>"\'\'"><&';

    expect(auth.generate(id, username, password)).toEqual(`
  <settings>
      <servers>
        <server>
          <id>${id}</id>
          <username>\${env.${username}}</username>
          <password>\${env.&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;}</password>
        </server>
      </servers>
  </settings>
  `);
  });
});
+21 −3
Changes for action.yml: 21 added lines, 3 removed lines.
Original line number Diff line number Diff line
name: 'Setup Java JDK'
description: 'Set up a specific version of the Java JDK and add the command-line tools to the PATH'
description: 'Set up a specific version of the Java JDK and add the
   command-line tools to the PATH'
author: 'GitHub'
inputs:
  java-version:
    description: 'The Java version to make available on the path. Takes a whole or semver Java version, or 1.x syntax (e.g. 1.8 => Java 8.x)'
    description: 'The Java version to make available on the path. Takes a whole
       or semver Java version, or 1.x syntax (e.g. 1.8 => Java 8.x)'
    required: true
  java-package:
    description: 'The package type (jre, jdk, jdk+fx)'
@@ -14,7 +16,23 @@ inputs:
    required: false
    default: 'x64'
  jdkFile:
    description: 'Path to where the compressed JDK is located. The path could be in your source repository or a local path on the agent.'
    description: 'Path to where the compressed JDK is located. The path could
       be in your source repository or a local path on the agent.'
    required: false
  server-id:
    description: 'ID of the distributionManagement repository in the pom.xml
       file. Default is `github`'
    required: false
  server-username:
    description: 'Environment variable name for the username for authentication
       to the Apache Maven repository. Default is $GITHUB_ACTOR'
    required: false
  server-password:
    description: 'Environment variable name for password or token for
       authentication to the Apache Maven repository. Default is $GITHUB_TOKEN'
    required: false
  settings-path:
    description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
    required: false
runs:
  using: 'node12'
Loading