getpopper / popper

Container-native task automation engine.
https://getpopper.io
MIT License
302 stars 61 forks source link

support `env` options in translator_drone #978

Closed shumbo closed 3 years ago

shumbo commented 3 years ago

Overview

This PR adds support for environment variable options in Popper-to-Drone translator.

env options

Popper has the env option to set environment variables in the workflow. You can place env under steps to define variables for a step, or you can place env under options to define variables throughout the steps.

Drone has a similar feature environment and has almost identical syntax for defining environment variables. However, it only supports per-pipeline variables (equivalent of options.env) in Docker pipelines.

Hence, I made a decision to inject global variables in each step.

GIT_ Variables

Popper injects multiple variables that represent the status of the git repository. The translator automatically generates definitions of such environment variables in Drone so that scripts written for Popper can continue to work.

The following is the list of variables supported by Popper and corresponding Drone template variables.

Popper Drone
GIT_COMMIT ${DRONE_COMMIT_SHA}
GIT_BRANCH ${DRONE_COMMIT_BRANCH}
GIT_SHA_SHORT ${DRONE_COMMIT_SHA:0:7}
GIT_REMOTE_ORIGIN_URL ${DRONE_GIT_HTTP_URL}
GIT_TAG ${DRONE_TAG}

The translator automatically generates bindings and injects them into the output.

JayjeetAtGithub commented 3 years ago

@shumbo Can you please open a issue for this PR ?

JayjeetAtGithub commented 3 years ago

@shumbo Thanks a lot for working on this. Can you please check if the DRONE_ variables are properly transferred to the GIT_ variables. Looks like environment variables cannot be expanded in the environment section. See here. I created an example workflow to double check,

kind: pipeline
name: default

steps:
  - name: docker-tag-demo
    image: debian:stable-slim
    environment:
      GIT_BRANCH: ${DRONE_BRANCH}
    commands:
      - echo "The current branch is ${GIT_BRANCH}"
      - echo "The current branch is ${DRONE_BRANCH}"
      - echo "The current commit hash is ${DRONE_COMMIT_SHA}"
      - echo "The image tag is ${DRONE_BRANCH//\//-}-${DRONE_COMMIT_SHA:0:8}"

You can put this workflow in a directory, commit it, and run this using

drone exec --branch master --sha <the commit sha>

You would see the GIT_BRANCH echo in the first command is blank. I think a quick solution to this will be intelligently adding manual commands like

export GIT_BRANCH=$DRONE_BRANCH

to inject the runtime environment variables supported by Popper. Let me know your thoughts.

shumbo commented 3 years ago

@JayjeetAtGithub Thanks for your comment.

${VARIABLE} (dollar sign followed by curly braces) is a Drone's syntax to substitute variables available in Drone before running workflows, whereas $VARIABLE is variable expansion handled by the shell.

https://docs.drone.io/pipeline/environment/substitution/

In your example, - echo "The current branch is ${GIT_BRANCH}" should be - echo "The current branch is $GIT_BRANCH" because GIT_BRANCH is an environment variable.

kind: pipeline
name: default
type: docker
steps:
  - name: docker-tag-demo
    image: debian:stable-slim
    environment:
      GIT_BRANCH: ${DRONE_BRANCH}
    commands:
      - echo "The current branch is $GIT_BRANCH"

This should print the name of the branch correctly with drone exec --branch master.

JayjeetAtGithub commented 3 years ago

Thanks a lot @shumbo . That works.

JayjeetAtGithub commented 3 years ago

Everything seems to work great !