Skip to content

Commit

Permalink
Set up registry auth for Docker (#2)
Browse files Browse the repository at this point in the history
* Set up registry auth for Docker

Fixes NSL-456

* fix interpolation
  • Loading branch information
n-g authored Mar 14, 2023
1 parent 5356a87 commit e6173bc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ jobs:
- name: Destroy cluster
run: |
cat /tmp/cluster_id.txt | xargs -I % nsc cluster destroy % --force
push_image:
runs-on: ubuntu-latest
name: Push an image to the Namespace Cloud registry
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Namespace Cloud CLI
id: nscloud
uses: ./ # Uses an action in the root directory
- name: Push to private registry
run: |
echo "Pushing to ${{ steps.nscloud.outputs.registry-address }}..."
docker pull alpine:3.17.2
docker tag alpine:3.17.2 ${{ steps.nscloud.outputs.registry-address }}/nscloud-alpine
docker push ${{ steps.nscloud.outputs.registry-address }}/nscloud-alpine
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: '"Set up Namespace Cloud CLI" Action For GitHub Actions'
description: "Download and configure the Namespace Cloud CLI"
outputs:
registry-address:
description: "Endpoint address of the Namespace Cloud registry."
runs:
using: node16
main: dist/main/index.js
25 changes: 25 additions & 0 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6757,12 +6757,19 @@ var __webpack_exports__ = {};
(() => {
"use strict";
__nccwpck_require__.r(__webpack_exports__);
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
/* harmony export */ "tmpFile": () => (/* binding */ tmpFile)
/* harmony export */ });
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(2186);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _actions_tool_cache__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(7784);
/* harmony import */ var _actions_tool_cache__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_tool_cache__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2__ = __nccwpck_require__(1514);
/* harmony import */ var _actions_exec__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__nccwpck_require__.n(_actions_exec__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_3__ = __nccwpck_require__(7147);
/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__nccwpck_require__.n(fs__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4__ = __nccwpck_require__(1017);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__nccwpck_require__.n(path__WEBPACK_IMPORTED_MODULE_4__);
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -6775,11 +6782,15 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume





function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield installNsc();
yield ensureFreshTenantToken();
const registry = yield dockerLogin();
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setOutput("registry-address", registry);
}
catch (error) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(error.message);
Expand Down Expand Up @@ -6831,6 +6842,20 @@ function ensureFreshTenantToken() {
yield _actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec("nsc auth exchange-github-token");
});
}
function dockerLogin() {
return __awaiter(this, void 0, void 0, function* () {
const out = tmpFile("registry.txt");
yield _actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec(`nsc cluster docker-login --output_registry_to=${out}`);
return fs__WEBPACK_IMPORTED_MODULE_3__.readFileSync(out, "utf8");
});
}
function tmpFile(file) {
const tmpDir = path__WEBPACK_IMPORTED_MODULE_4__.join(process.env.RUNNER_TEMP, "ns");
if (!fs__WEBPACK_IMPORTED_MODULE_3__.existsSync(tmpDir)) {
fs__WEBPACK_IMPORTED_MODULE_3__.mkdirSync(tmpDir);
}
return path__WEBPACK_IMPORTED_MODULE_4__.join(tmpDir, file);
}
run();

})();
Expand Down
22 changes: 22 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as exec from "@actions/exec";
import * as fs from "fs";
import * as path from "path";

async function run(): Promise<void> {
try {
await installNsc();

await ensureFreshTenantToken();

const registry = await dockerLogin();
core.setOutput("registry-address", registry);
} catch (error) {
core.setFailed(error.message);
}
Expand Down Expand Up @@ -62,4 +67,21 @@ async function ensureFreshTenantToken() {
await exec.exec("nsc auth exchange-github-token");
}

async function dockerLogin() {
const out = tmpFile("registry.txt");
await exec.exec(`nsc cluster docker-login --output_registry_to=${out}`);

return fs.readFileSync(out, "utf8");
}

export function tmpFile(file: string): string {
const tmpDir = path.join(process.env.RUNNER_TEMP, "ns");

if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}

return path.join(tmpDir, file);
}

run();

0 comments on commit e6173bc

Please sign in to comment.