Reuse Variables in other steps

Hello,

i have a workflow and wanted to use variables in more than one steps. In step 1 i build the variable and in step 2 i reuse it.

Here is my workflow:


name: Docker und Rancher on Tag

on:
  push:
    tags:
      - v*
env:
  IMAGE_NAME: image

jobs:
  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Push image
        run: |
          IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME

          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')

          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')

          # Use Docker `latest` tag convention
          [ "$VERSION" == "main" ] && VERSION=latest

          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION

          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

      - name: Rancher Deploy
        uses: yuyitech/github-action-rancher2@v0.0.1
        env:
          RANCHER_ACCESS_KEY: ${{ secrets.RANCHER_ACCESS_KEY }}
          RANCHER_SECRET_KEY: ${{ secrets.RANCHER_SECRET_KEY }}
          RANCHER_URL: https://RANCHERURL/v3/project/local:p-g7r2v/workloads/deployment:test:deployment
          RANCHER_DATA: |
            [
              {
                "name": $IMAGE_NAME
                "image": $IMAGE_ID:$VERSION
              }
            ]

I will build my Docker, push it to private repository and redeploy my rancher with the new image. How can i use the Variables IMAGE_NAME, IMAGE_ID and VERSION in step “Rancher Deploy”?

I also tested it like this:

name: Docker und Rancher on Tag

on:
  push:
    tags:
      - v*
env:
  IMAGE_NAME: image
  IMAGE_ID:
  VERSION:

jobs:
  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Push image
        run: |
          IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME

          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')

          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')

          # Use Docker `latest` tag convention
          [ "$VERSION" == "main" ] && VERSION=latest

          echo IMAGE_ID=$IMAGE_ID >> $GITHUB_ENV
          echo VERSION=$VERSION >> $GITHUB_ENV
          echo IMAGE_NAME >> $GITHUB_ENV

          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION


      - name: Rancher Deploy
        uses: yuyitech/github-action-rancher2@v0.0.1
        env:
          RANCHER_ACCESS_KEY: ${{ secrets.RANCHER_ACCESS_KEY }}
          RANCHER_SECRET_KEY: ${{ secrets.RANCHER_SECRET_KEY }}
          RANCHER_URL: https://RANCHERURL/v3/project/local:p-g7r2v/workloads/deployment:test:deployment
          RANCHER_DATA: |
            [
              {
                "name": ${{ env.NEW_IMAGE_NAME }},
                "image": ${{ env.IMAGE_ID }}:${{ env.VERSION }}
              }
            ]

But with this version i get an error

Unable to process file command ‘env’ successfully.
Invalid environment variable format ‘IMAGE_NAME’

I hope you can help me ! :slight_smile:

The second version is almost correct, the problem is this line:

The main problem is that it has only the variable name, when lines written to GITHUB_ENV must be NAME=VALUE. But it’s also redundant, because you already set IMAGE_NAME in the env section above, so best just remove that line. :slightly_smiling_face:

That does not work, too. Can you edit my Workflow like your suggestion? I dont know if i change it correctly.

Is it possible to reuse the variables in the env section of the “yuyitech/github-action-rancher2@v0.0.1” step?

I mean you should change this part of the “Push image” step

to:

          echo IMAGE_ID=$IMAGE_ID >> $GITHUB_ENV
          echo VERSION=$VERSION >> $GITHUB_ENV

If that doesn’t work, what’s the error?

No, the env section of a step applies only to that step. If you want them to last for other steps in the job you need to use GITHUB_ENV, or a job/workflow env section.

My workflow:

name: Docker und Rancher on Tag

on:
  push:
    tags:
      - v*
env:
  IMAGE_NAME: image
jobs:
  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

      - name: Push image
        run: |
          IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME

          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')

          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')

          # Use Docker `latest` tag convention
          [ "$VERSION" == "main" ] && VERSION=latest

          echo IMAGE_ID=$IMAGE_ID >> $GITHUB_ENV
          echo VERSION=$VERSION >> $GITHUB_ENV

          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

      - name: Rancher Deploy
        uses: yuyitech/github-action-rancher2@v0.0.1
        env:
          RANCHER_ACCESS_KEY: ${{ secrets.RANCHER_ACCESS_KEY }}
          RANCHER_SECRET_KEY: ${{ secrets.RANCHER_SECRET_KEY }}
          RANCHER_URL: https://RANCHERURL/v3/project/local:p-g7r2v/workloads/deployment:test:deployment
          RANCHER_DATA: |
          RANCHER_DATA: |
            [
              {
                "name": ${{ env.IMAGE_NAME }},
                "image": ${{ env.IMAGE_ID }}:${{ env.VERSION }}
              }
            ]

It looks very good, but i get an error at the step Deploy to Rancher

Run yuyitech/github-action-rancher2@v0.0.1
  env:
    IMAGE_NAME: image
    IMAGE_ID: ghcr.io/company/image-ui/image
    VERSION: 0.9.11
    RANCHER_ACCESS_KEY: 
    RANCHER_SECRET_KEY: 
    RANCHER_URL: https://RANCHERURL/v3/project/local:p-g7r2v/workloads/deployment:test:deployment
    RANCHER_DATA: [
    {
      "name": image,
      "image": ghcr.io/company/image-ui/image:0.9.11
    }
  ]
  
/usr/bin/docker run --name e1cc51ab76d8541fa74f418bb4535bb085f4e6_48ebe3 --label e1cc51 --workdir /github/workspace --rm -e IMAGE_NAME -e IMAGE_ID -e VERSION -e RANCHER_ACCESS_KEY -e RANCHER_SECRET_KEY -e RANCHER_URL -e RANCHER_DATA -e HOME -e GITHUB_JOB -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_REPOSITORY_OWNER -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RETENTION_DAYS -e GITHUB_RUN_ATTEMPT -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_SERVER_URL -e GITHUB_API_URL -e GITHUB_GRAPHQL_URL -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e GITHUB_ACTION_REPOSITORY -e GITHUB_ACTION_REF -e GITHUB_PATH -e GITHUB_ENV -e RUNNER_OS -e RUNNER_NAME -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -e ACTIONS_RUNTIME_URL -e ACTIONS_RUNTIME_TOKEN -e ACTIONS_CACHE_URL -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/plan/plan":"/github/workspace" e1cc51:ab76d8541fa74f418bb4535bb085f4e6
2021/09/23 15:02:59 
 API https://RANCHERURL/v3/project/local:p-g7r2v/workloads/deployment:test:deployment 
 ACCESS_KEY: 
 SECRET_KEY: 
 DATA: [
  {
    "name": image,
    "image": ghcr.io/company/image-ui/image:0.9.11
  }
]
2021/09/23 15:02:59 An error occurred during pasre params: data,  invalid character 'i' looking for beginning of value

It seems that i use the image variable wrong… But i dont know what i can do…

Looking at the example in the yuyitech/github-action-rancher2 Readme it seems like RANCHER_DATA must be valid JSON. You’re missing quotes around the values you’re providing. Try:

          RANCHER_DATA: |
            [
              {
                "name": "${{ env.IMAGE_NAME }}",
                "image": "${{ env.IMAGE_ID }}:${{ env.VERSION }}"
              }
            ]

Sorry!
That seems working!

Thanks!

1 Like