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

dev: debug container workflow #413

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

da2ce7
Copy link
Contributor

@da2ce7 da2ce7 commented Sep 11, 2023

No description provided.

@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 10:28 — with GitHub Actions Inactive
@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from 86d90ec to 7a18092 Compare September 11, 2023 10:58
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 10:58 — with GitHub Actions Inactive
@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from 7a18092 to 3d33c18 Compare September 11, 2023 11:00
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 11:00 — with GitHub Actions Inactive
@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from 3d33c18 to 0541c6b Compare September 11, 2023 11:03
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 11:03 — with GitHub Actions Inactive
@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from 0541c6b to c7cb3f0 Compare September 11, 2023 11:06
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 11:07 — with GitHub Actions Inactive
@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from c7cb3f0 to da476a7 Compare September 11, 2023 11:09
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 11:09 — with GitHub Actions Inactive
@josecelano
Copy link
Member

josecelano commented Sep 11, 2023

Hey @da2ce7 this is refactored version extracting functions (not tested):

context:
  name: Context
  runs-on: ubuntu-latest

  outputs:
    continue: ${{ steps.check.outputs.continue }}
    type: ${{ steps.check.outputs.type }}
    version: ${{ steps.check.outputs.version }}

  steps:
    - id: check
      name: Check Context
      run: |
        REPO="${{ github.repository }}"
        EVENT="${{ github.event_name }}"
        REF="${{ github.ref }}"

        function handleMainBranch() {
          echo "type=development" >> $GITHUB_OUTPUT
          echo "continue=true" >> $GITHUB_OUTPUT
          echo "On \`main\` Branch, Type: \`development\`"
        }

        function handleDevelopBranch() {
          echo "type=development" >> $GITHUB_OUTPUT
          echo "continue=true" >> $GITHUB_OUTPUT
          echo "On \`develop\` Branch, Type: \`development\`"
        }

        function handleReleaseBranch() {
          version=$(echo "$REF" | sed -n -E 's/^(refs\/heads\/releases\/)//p')
          echo "version=$version" >> $GITHUB_OUTPUT
          echo "type=release" >> $GITHUB_OUTPUT
          echo "continue=true" >> $GITHUB_OUTPUT
          echo "In \`releases/$version\` Branch, Type: \`release\`"
        }

        # Verify the repository
        if [[ "$REPO" != "torrust/torrust-tracker" ]]; then
          echo "On a Forked Repository. Will Not Continue"
          exit 0
        fi

        # Ensure it's a push event
        if [[ "$EVENT" != "push" ]]; then
          echo "Not a Push Event. Will Not Continue"
          exit 0
        fi

        # Check branch and set context accordingly
        case "$REF" in
          "refs/heads/main")
            handleMainBranch
            ;;

          "refs/heads/develop")
            handleDevelopBranch
            ;;

          refs/heads/releases/*)
            if [[ "$REF" =~ ^(refs\/heads\/releases\/)(v)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$ ]]; then
              handleReleaseBranch
            else
              echo "Not Correct Branch. Will Not Continue"
            fi
            ;;

          *)
            echo "Not Correct Branch. Will Not Continue"
            ;;
        esac

On the other hand, I'm always tempted not to use bash but TypeScript, so we can even add tests for the workflow code.

// releaseLogic.ts

interface GithubContext {
  repository: string;
  eventName: string;
  ref: string;
}

function main(context: GithubContext) {
  // Your TypeScript logic here
  // ... 

  // To set GitHub Action outputs, use the `setOutput` function:
  // setOutput('continue', 'true');
  // ...
}

function setOutput(name: string, value: string) {
  console.log(`::set-output name=${name}::${value}`);
}

// Get context from environment variables or however you wish to pass them
const githubContext: GithubContext = {
  repository: process.env.GITHUB_REPOSITORY || '',
  eventName: process.env.GITHUB_EVENT_NAME || '',
  ref: process.env.GITHUB_REF || ''
};

main(githubContext);
context:
  name: Context
  runs-on: ubuntu-latest

  outputs:
    continue: ${{ steps.check.outputs.continue }}
    type: ${{ steps.check.outputs.type }}
    version: ${{ steps.check.outputs.version }}

steps:
  - name: Checkout code
    uses: actions/checkout@v2
  
  - name: Set up Node.js
    uses: actions/setup-node@v2
    with:
      node-version: '14'  # or whichever version you prefer

  - name: Install dependencies
    run: npm install typescript

  - name: Transpile TypeScript
    run: npx tsc releaseLogic.ts --target ES6 --module commonjs

  - name: Run Workflow Logic
    id: check
    run: node workflowLogic.js
    env:
      GITHUB_REPOSITORY: ${{ github.repository }}
      GITHUB_EVENT_NAME: ${{ github.event_name }}
      GITHUB_REF: ${{ github.ref }}

And maybe even in Rust. But maybe for the time being with a couple of comments is OK because the code is not too big.

@da2ce7 da2ce7 force-pushed the 20230911_debug_container_workflow branch from e1274e2 to da476a7 Compare September 11, 2023 12:50
@da2ce7 da2ce7 temporarily deployed to coverage September 11, 2023 12:50 — with GitHub Actions Inactive
@da2ce7
Copy link
Contributor Author

da2ce7 commented Sep 11, 2023

ACK da476a7

@da2ce7 da2ce7 merged commit 72d1264 into torrust:develop Sep 11, 2023
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants