Repository name in environment variable?

name: Publish
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Build the Docker image
      env:
        CONTAINER_IMAGE: docker.pkg.github.com/${{github.repository}}/dist:${{github.sha}}
        CONTAINER_IMAGE_LATEST: docker.pkg.github.com/${{github.repository}}/dist:latest
      run: |
        docker login -u $GITHUB_ACTOR -p ${{secrets.GITHUB_TOKEN}} docker.pkg.github.com
        docker build -t $CONTAINER_IMAGE -t $CONTAINER_IMAGE_LATEST .
        docker push $CONTAINER_IMAGE
        docker push $CONTAINER_IMAGE_LATEST

This will push the “dist” docker package to GitHub packages, but it seems like, the packages are user-level not repo-level, because it says: “Cannot find package version with version id” 

So, i decided to replace the “dist” package name to the repo name, but it seems like you can only retrieve the repository name from the “github.repository” variable (which is in “user/repository” format)

Would appreciate any help on that :slight_smile:

Something like this?

- run: echo ::set-env name=REPOSITORY_NAME::$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}')
shell: bash

- run: echo "$REPOSITORY_NAME"
shell: bash
8 Likes

Prettty good. I ended up with this:

name: Publish
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Build the Docker image
      env:
        CONTAINER_IMAGE: docker.pkg.github.com/${{github.repository}}/${{secrets.REPO}}:${{github.sha}}
        CONTAINER_IMAGE_LATEST: docker.pkg.github.com/${{github.repository}}/${{secrets.REPO}}:latest
      run: |
        docker login -u $GITHUB_ACTOR -p ${{secrets.GITHUB_TOKEN}} docker.pkg.github.com
        docker build -t $CONTAINER_IMAGE -t $CONTAINER_IMAGE_LATEST .
        docker push $CONTAINER_IMAGE
        docker push $CONTAINER_IMAGE_LATEST

And added repo name as a secret manually :slight_smile:

1 Like

Just an enhancement on this:

- run: echo ::set-env name=REPOSITORY_NAME::$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//")
shell: bash

- run: echo "$REPOSITORY_NAME"
shell: bash

I have added the additional filter “sed” to remove the hanging “:refs”  

4 Likes

Since the variable github.event includes the webhook payload, we can access the repository name by ${{ github.event.repository.name }}.

@itchyny

${{ github.event.repository.name }} does not seem to be populated when an action is run ‘on.schedule’

The schedule event does not have an event payload according to the docs:

I suppose you could use this instead:

- run: echo "REPOSITORY_NAME=$(echo '${{ github.repository }}' | awk -F '/' '{print $1}')" >> $GITHUB_ENV
  shell: bash