diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 49eac4c..ec65028 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,19 +1,18 @@ name: "Test Run" on: push: - paths: + paths: - "dist/index.js" jobs: test: runs-on: ubuntu-latest steps: - - name: Checkout Repository - uses: actions/checkout@v1 - - name: Run the Action - id: run-action - uses: ./ - with: - username: matootie - personalAccessToken: ${{ secrets.PERSONAL_ACCESS_TOKEN }} - - name: Test output - run: echo ${{ steps.run-action.outputs.imageURL }} + - name: Checkout Repository + uses: actions/checkout@v2 + - name: Run the Action + id: run-action + uses: ./ + with: + accessToken: ${{ secrets.GITHUB_TOKEN }} + - name: Test output + run: echo ${{ steps.run-action.outputs.imageURL }} diff --git a/README.md b/README.md index 973009c..171b189 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,25 @@ Build and publish your repository as a Docker image and push it to GitHub Packag ## Inputs -### `username` +### `accessToken` -**Required**. GitHub user to publish the image on behalf of. +**Required**. GitHub Token for the user. Must have write permissions for packages. Recommended set up would be to use the provided GitHub Token for your repository; `${{ secrets.GITHUB_TOKEN }}`. -### `personalAccessToken` +### `username` -**Required**. GitHub Personal Access Token for the user. Must have write permissions for packages. +*Optional*. GitHub user to publish the image on behalf of. Defaults to the user who triggered the action to run. ### `repositoryName` -Optional. The repository to push the image to. Defaults to current repository. Must be specified in format `user/repo`. +*Optional*. The repository to push the image to. Defaults to current repository. Must be specified in format `user/repo`. ### `imageName` -Optional. The desired name for the image. Defaults to current repository name. +*Optional*. The desired name for the image. Defaults to current repository name. ### `imageTag` -Optional. The desired tag for the image. Defaults to current branch or release version number. +*Optional*. The desired tag for the image. Defaults to current branch or release version number. ## Outputs @@ -33,9 +33,10 @@ The full URL of the image. ## Example usage ```yaml +- name: Checkout Repository + uses: actions/checkout@v2 - name: Publish Image - uses: matootie/github-docker@v1.0.1 + uses: matootie/github-docker@v2.0.0 with: - username: matootie - personalAccessToken: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + accessToken: ${{ secrets.GITHUB_TOKEN }} ``` diff --git a/action.yml b/action.yml index f5547a9..7f6f540 100644 --- a/action.yml +++ b/action.yml @@ -1,27 +1,31 @@ -name: 'GitHub Docker Action' -description: 'Build and publish your repository as a Docker image and push it to GitHub Package Registry in one step' +name: "GitHub Docker Action" +description: "Build and publish your repository as a Docker image and push it to GitHub Package Registry in one step" inputs: - username: - description: 'GitHub user to publish the image on behalf of.' - required: true - personalAccessToken: - description: 'GitHub Personal Access Token for the user.' + accessToken: + description: "GitHub Repository Token to log in using." required: true + context: + description: "Where should GitHub Docker find the Dockerfile, relative to the root of the repository." + required: false + default: "." + username: + description: "GitHub user to publish the image on behalf of." + required: false repositoryName: - description: 'The repository to push the image to. Defaults to current repository. Must be specified in format user/repo' + description: "The repository to push the image to. Defaults to current repository. Must be specified in format user/repo" required: false imageName: - description: 'The desired name for the image. Defaults to current repository name.' + description: "The desired name for the image. Defaults to current repository name." required: false imageTag: - description: 'The desired tag for the image. Defaults to current branch or release version number.' + description: "The desired tag for the image. Defaults to current branch or release version number." required: false outputs: imageURL: - description: 'The URL of the image.' + description: "The URL of the image." runs: - using: 'node12' - main: 'dist/index.js' + using: "node12" + main: "dist/index.js" branding: - icon: 'anchor' - color: 'blue' + icon: "anchor" + color: "blue" diff --git a/dist/index.js b/dist/index.js index 5ded4d5..de14e70 100644 --- a/dist/index.js +++ b/dist/index.js @@ -635,6 +635,7 @@ module.exports = require("os"); /***/ (function(__unusedmodule, __unusedexports, __webpack_require__) { const process = __webpack_require__(765); +const path = __webpack_require__(622); const core = __webpack_require__(470); const exec = __webpack_require__(986); @@ -663,12 +664,16 @@ async function run() { */ - // Set the workspace directory. + // Set the workspace directory and context. + const home = process.env['HOME']; const workspace = process.env['GITHUB_WORKSPACE']; + const context = core.getInput('context', { required: true }); + path.join(workspace, context); // Log in to Docker. - const username = core.getInput('username', { required: true }); - const password = core.getInput('personalAccessToken', { required: true }); + let username = core.getInput('username', { required: false }); + if (!username) username = process.env['GITHUB_ACTOR']; + const password = core.getInput('accessToken', { required: true }); await exec.exec( `docker`, ['login', 'docker.pkg.github.com', '--username', username, '--password', password]); @@ -690,18 +695,26 @@ async function run() { const refArray = ref.split('/'); if (!imageTag) imageTag = refArray[refArray.length - 1]; + // Set some variables. + const imageURL = `docker.pkg.github.com/${repository}/${imageName}:${imageTag}` + // Build the Docker image. await exec.exec( `docker`, - ['build', '--tag', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`, workspace]); + ['build', '--tag', imageURL, workspace]); // Push the Docker image. await exec.exec( `docker`, - ['push', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`]); + ['push', imageURL]); // Output the image URL. - core.setOutput('imageURL', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`); + core.setOutput('imageURL', imageURL); + + // Delete the Docker config. + exec.exec( + 'rm', + ['-v', `${home}/.docker/config.json`]); } catch (error) { core.setFailed(error.message); diff --git a/index.js b/index.js index 9d5cfc4..1a38566 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const process = require('process'); +const path = require('path'); const core = require('@actions/core'); const exec = require('@actions/exec'); @@ -27,12 +28,16 @@ async function run() { */ - // Set the workspace directory. + // Set the workspace directory and context. + const home = process.env['HOME']; const workspace = process.env['GITHUB_WORKSPACE']; + const context = core.getInput('context', { required: true }); + path.join(workspace, context); // Log in to Docker. - const username = core.getInput('username', { required: true }); - const password = core.getInput('personalAccessToken', { required: true }); + let username = core.getInput('username', { required: false }); + if (!username) username = process.env['GITHUB_ACTOR']; + const password = core.getInput('accessToken', { required: true }); await exec.exec( `docker`, ['login', 'docker.pkg.github.com', '--username', username, '--password', password]); @@ -54,18 +59,26 @@ async function run() { const refArray = ref.split('/'); if (!imageTag) imageTag = refArray[refArray.length - 1]; + // Set some variables. + const imageURL = `docker.pkg.github.com/${repository}/${imageName}:${imageTag}` + // Build the Docker image. await exec.exec( `docker`, - ['build', '--tag', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`, workspace]); + ['build', '--tag', imageURL, workspace]); // Push the Docker image. await exec.exec( `docker`, - ['push', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`]); + ['push', imageURL]); // Output the image URL. - core.setOutput('imageURL', `docker.pkg.github.com/${repository}/${imageName}:${imageTag}`); + core.setOutput('imageURL', imageURL); + + // Delete the Docker config. + exec.exec( + 'rm', + ['-v', `${home}/.docker/config.json`]); } catch (error) { core.setFailed(error.message); diff --git a/package-lock.json b/package-lock.json index 0f07632..3f5ba21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "github-docker", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b901545..44d48fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "github-docker", - "version": "1.0.0", + "version": "2.0.0", "description": "Build and publish your repository as a Docker image and push it to GitHub Package Registry in one step", "main": "index.js", "scripts": {