Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run docker commands with env mentioned in the workflow? #35

Open
sathyamolagodaarimac opened this issue Aug 29, 2022 · 1 comment

Comments

@sathyamolagodaarimac
Copy link

sathyamolagodaarimac commented Aug 29, 2022

Run garygrossgarten/github-action-ssh@release
Establishing a SSH connection to ***.
using provided private key
(node:1514) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
🤝 Connected to ***.
Executing command: echo $DOCKER_TOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker pull $REGISTRY:$IMAGE_NAME
docker run -p 3000:3000 $REGISTRY:$IMAGE_NAME
Error: Cannot perform an interactive login from a non TTY device

invalid reference format

docker: invalid reference format.
See 'docker run --help'.

⚠️ An error happened executing command echo $DOCKER_TOKEN | docker login ghcr.io -u $USERNAME --password-stdin
docker pull $REGISTRY:$IMAGE_NAME
docker run -p 3000:3000 $REGISTRY:$IMAGE_NAME. Command exited with code 125
Error: Command exited with code 125
 1: 0xa1a640 node::Abort() [/home/runner/runners/2.296.0/externals/node12/bin/node]
 2: 0xa90649  [/home/runner/runners/2.296.0/externals/node12/bin/node]
 3: 0xc06599  [/home/runner/runners/2.296.0/externals/node12/bin/node]
 4: 0xc08387 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [/home/runner/runners/2.296.0/externals/node12/bin/node]
 5: 0x140dd19  [/home/runner/runners/2.296.0/externals/node12/bin/node]

I'm getting the above issue when I'm using the env variables mentioned in the workflow. And here is my workflow

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  pull_request:
    branches: ['main']

env:
  REGISTRY: 'ghcr.io/testing/api-gateway'
  IMAGE_NAME: ${{ format('{0}-{1}', github.event.repository.name, github.sha) }}
  USERNAME: ${{ secrets.USERNAME }}
  DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm install
      - run: npm run build

      - name: Build & Push docker
        run: docker build -t $REGISTRY:$IMAGE_NAME .

      - name: Login to github package repository
        run: echo $DOCKER_TOKEN | docker login ghcr.io -u $USERNAME --password-stdin

      - name: Push docker image
        run: docker push $REGISTRY:$IMAGE_NAME

  deploy:
    runs-on: ubuntu-latest
    needs: build

    steps:
      - name: Deploy to Digital Ocean droplet via SSH action
        uses: garygrossgarten/github-action-ssh@release
        with:
          host: ${{ secrets.DO_HOST }}
          username: ${{ secrets.DO_USER }}
          privateKey: ${{ secrets.DO_KEY }}
          passphrase: ${{ secrets.DO_PASSPHRASE }}
          command: |
            echo $DOCKER_TOKEN | docker login ghcr.io -u $USERNAME --password-stdin
            docker pull $REGISTRY:$IMAGE_NAME
            docker run -p 3000:3000 $REGISTRY:$IMAGE_NAME

What is the correct way to use these variables here?

@1CoolUsername
Copy link

The Context being used by the workflow is not the .env in the host machine; when you set those commands. The variables must be on from the workflow context as secrets or inputs.

${{ secrets.REGISTRY }} - The syntax is the same as you did for the host, username, privateKey, passphrase.

I can confirm this because one of my commands uses the ${{input.key}} which is derived from the Repo Name to cd into the respective directory.

i.e.
command: cd ${{inputs.directory}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants