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

Prototyping adding in a dynamic secrets.yaml file from GitHub secrets #95

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

jzucker2
Copy link

I found this and thought it was cool but stumbled on incorporating the secrets.yaml file and made this as a rough prototype.

I'm currently using it in a private repo but I'd set up a public one as an example if there's an interest.

A rough example of a .github/workflows/build.yaml actions is below:

# borrowed from https://github.com/esphome/home-assistant-voice-pe/blob/dev/.github/workflows/build.yml

name: Build

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    # every Sunday at UTC midnight
    - cron: '0 0 * * 0'
  push:
    branches:
      - master
  pull_request:
  workflow_dispatch:
  release:
    types: [published]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  # https://github.com/esphome/build-action
  # https://github.com/esphome/workflows/tree/main
  build-firmware:
    name: Build Firmware
    uses: jzucker2/workflows/.github/workflows/[email protected]
    secrets: inherit
    with:
      files: |
        esphome/first_sensor.yaml
        esphome/second_sensor.yaml
        esphome/second_sensor_v2.yaml
      cache: true
      esphome-version: 2024.10.1
      release-summary: ${{ github.event_name == 'published' && github.event.release.body || '' }}
      release-url: ${{ github.event_name == 'published' && github.event.release.html_url || '' }}
      release-version: ${{ github.event_name == 'published' && github.event.release.tag_name || '' }}
      # My nonsense below
      secrets-environment: super_secret_environment
      # secrets-yaml-file: ${{ secrets.ESPHOME_SECRETS_YAML }}
      secrets-yaml-file-path: esphome/secrets.yaml

  comment:
    if: github.event_name == 'pull_request'
    name: Comment on PR
    runs-on: ubuntu-latest
    needs:
      - build-firmware
    steps:
      - name: Comment on PR
        uses: actions/[email protected]
        with:
          script: |-
            // post the all the artifacts (gzip)
            const url = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}#artifacts`
            await github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `Firmware built successfully! :tada:

            [Download][download] and extract the firmware to install with https://web.esphome.io

            Make sure to choose \`<file name>.bin\`.

            [download]: ${url}`
            })

  upload-on-release:
    name: Upload artifacts on release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    needs:
      - build-firmware
    steps:
      - name: Download artifacts
        uses: actions/[email protected]
        with:
          path: files

      - name: Display structure of downloaded files
        run: ls -R

      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        # TODO: also include the manifest.json
        with:
          files: |
            files/**/*.bin
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

I'd be interested in cleaning this up if it's useful. Or is there an easier way to go about this that I'm misunderstanding?

Comment on lines +107 to +111
uses: akiojin/[email protected]
id: decode-base64
with:
base64: ${{ secrets.ESPHOME_SECRETS_YAML }}
output-path: ${{ inputs.secrets-yaml-file-path }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably don't need to import a whole action for this

Suggested change
uses: akiojin/[email protected]
id: decode-base64
with:
base64: ${{ secrets.ESPHOME_SECRETS_YAML }}
output-path: ${{ inputs.secrets-yaml-file-path }}
id: decode-base64
run: echo "${{ secrets.ESPHOME_SECRETS_YAML }}" | base64 -d > ${{ inputs.secrets-yaml-file-path }}

Comment on lines +49 to +53
secrets-yaml-file-path:
description: Location of secrets.yaml file
required: false
type: string
default: "secrets.yaml"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we know the path of each yaml file that is going to be compiled, and secrets.yaml needs to be in the same directory as that, we can just use $(dirname ${{ matrix.file }})/secrets.yaml to stick it in the right place.

Comment on lines +10 to +14
cache:
description: Cache build directory
required: false
type: boolean
default: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this in a separate PR please

.gitignore Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate PR please

@esphome
Copy link

esphome bot commented Nov 4, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@esphome esphome bot marked this pull request as draft November 4, 2024 19:33
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

Successfully merging this pull request may close these issues.

2 participants