diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml new file mode 100644 index 0000000..4ea91f1 --- /dev/null +++ b/.github/workflows/pr.yaml @@ -0,0 +1,41 @@ +name: pr + +on: + pull_request: + types: + - opened + - edited + - synchronize + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Molnctl Setup + uses: molnett/setup-molnctl-action@v2 + with: + api-token-client-id: ${{ secrets.MOLNETT_CLIENT_ID }} + api-token-client-secret: ${{ secrets.MOLNETT_CLIENT_SECRET }} + default-org: personal + - name: Check molnctl usage + run: molnctl orgs list + - name: Build & Push Image to Molnett + run: | + molnctl auth docker + IMAGE_NAME=`molnctl svcs image-name -u molnett.yaml` + docker buildx build . -t $IMAGE_NAME + docker push $IMAGE_NAME + - name: Deploy Ephemeral Environment + uses: ./ + with: + action: deploy + manifest-path: molnett.yaml + - name: Delete Ephemeral environment + uses: ./ + with: + action: delete + manifest-path: molnett.yaml + - name: Cleanup + run: rm -r ~/.config/molnett diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..232a84b --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +* @bittermandel @tmlye @mikn diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..19afcac --- /dev/null +++ b/action.yml @@ -0,0 +1,59 @@ +name: 'Molnett - Ephemeral Environments' +description: 'Deploy or delete an ephemeral environment' +branding: + icon: 'download-cloud' + color: 'orange' +inputs: + action: + description: 'Either "deploy" or "delete"' + required: true + manifest-path: + description: 'Path to molnett manifest' + required: true +outputs: + environment-name: + description: 'Name of the created environment' + value: ${{ steps.random-number-generator.outputs.random-number }} +runs: + using: 'composite' + steps: + - name: Check Action + if: ${{ inputs.action != 'deploy' && inputs.action != 'delete' }} + shell: bash + run: | + echo "::error title=⛔ error hint::action needs to be deploy or delete" + exit 1 + - name: Check Runner OS + if: ${{ runner.os != 'Linux' }} + shell: bash + run: | + echo "::error title=⛔ error hint::Support Linux Only" + exit 1 + - name: Environment Name + id: env-name + env: + ENV_NAME: pr-${{ github.event.number }}-${{ github.head_ref }} + run: | + # Replace special characters in branch name with dash + ENV_NAME=`echo $ENV_NAME | tr $'!\'#$%&()+,-./;<=>@]_\`{|}' '-'` + echo "env=$ENV_NAME" >> $GITHUB_OUTPUT + - name: Deploy Ephemeral Environment + if: ${{ inputs.action == 'deploy' }} + shell: bash + env: + ENV_NAME: ${{ steps.env-name.outputs.env }} + MANIFEST: ${{ inputs.manifest-path }} + run: | + sed -i "s/environment:.*/environment: $ENV_NAME/" $MANIFEST + molnctl environments create $ENV_NAME || true + molnctl services deploy --no-confirm $MANIFEST + FINAL_NAME=$(sed -n 's/name: //p' $MANIFEST | awk '{$1=$1};1') + echo "name=$FINAL_NAME" >> $GITHUB_OUTPUT + echo "env=$ENV_NAME" >> $GITHUB_OUTPUT + - name: Delete Ephemeral Environment + if: ${{ inputs.action == 'delete' }} + shell: bash + env: + ENV_NAME: ${{ steps.env-name.outputs.env }} + run: | + molnctl environments delete --no-confirm $ENV_NAME