Giter VIP home page Giter VIP logo

write-file-action's Introduction

Write File Action

This action writes a file.

Inputs

path

Required The path to the file to write.

contents

Required The contents of the file.

write-mode

Optional The mode of writing to use: overwrite, append, or preserve.

Modes:

  • overwrite - overwrite the file if it exists
  • append - if the file exists, it will be appended to
  • preserve - if the file already exists the contents will not be written to

Default append

Outputs

size

Returns the file size.

Example usage

uses: DamianReeves/write-file-action@master
with:
  path: ${{ env.home}}/.bashrc
  contents: |
    Hello World!
  write-mode: append

Example usage with checkout, commit and push

name: Overwrite some file

on:
  push:
    branches: [ main ]
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Overwrite file
        uses: "DamianReeves/write-file-action@master"
        with:
          path: path/to/file.js
          write-mode: overwrite
          contents: |
            console.log('some contents')
            
      - name: Commit & Push
        uses: Andro999b/[email protected]
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: main
          force: true
          message: 'Overwritten by Github Actions - ${date}'

write-file-action's People

Contributors

alexandre97costa avatar damianreeves avatar dependabot[bot] avatar github-actions[bot] avatar nogara avatar paulrberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

write-file-action's Issues

using variables in "content" doesn't seem to work

on:
  push:
    branches:
      - main
    paths-ignore:
      - 'scores'
jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: get commit message
        run: echo "MESSAGE=$(git log --format=%B -n 1 ${{ github.event.after }})" >> $GITHUB_ENV
      - name: show commit message
        run : echo $MESSAGE
      - name: Save score
        uses: DamianReeves/[email protected]
        with:
          path: ${{ github.workspace }}/scores
          contents: |
            echo $MESSAGE
          write-mode: append
      # push updated file back to main branch
      - run: git config --local user.email "[email protected]"
      - run: git config --local user.name "GitHub Action"
      - run: git add scores
      - run: git commit -m "scores updated"
      - run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This action works and writes back to the repo with something. Only it's not the message on the current commit but rather echo $MESSAGE literally. It doesn't appear that the action executes what it recieves similar to other actions, but take the "contents" parameter literally as text.

fails in windows

Attempt your example with "runs-on: windows-latest". the word "echo" will be written to the file.

Line chomping not working as expected

Trying to use the action to update a csv file but line breaks don't seem to be working with YAML block chomping (keep).

Example below reads a new issue and adds to a csv.

on:
  issues:
    types:
      - opened

jobs:
  label_issue:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Checkout
      - name: Append file
        uses: DamianReeves/write-file-action@master
        with:
          path: data.csv
          contents: |+
            ${{ github.event.issue.title }},${{ github.event.issue.body }}
              
          write-mode: append
      - name: Commit & Push
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          # github_token: ${{ secrets.GITHUB_TOKEN }}
          file_pattern: 'data.csv'
          branch: main
          commit_message: 'Overwritten by Github Actions - ${date}'

I tried using https://yaml-online-parser.appspot.com to parse the yaml to check the block chomping |+ setting and it appears to be correct (replacing GitHub vars with cell1,cell2,cell3 e.g

          contents: |+
            cell1,cell2,cell3
              
          write-mode: append

The output includes multiple \n\n at the end of the string as expected but when running within the action, it just appends the last line without line breaks.

Also strange, it appears to work differently on the first run - hope this makes sense.

Step 1
Start data.csv with single line for columns (no line breaks)

Heading1,Heading2,Heading3

Step 2
Run the action. Result is

Heading1,Heading2,Heading3
cell1,cell2,cell3

Works perfectly. Exactly what I'm after

Step3
Run action again, Result is

Heading1,Heading2,Heading3
cell1,cell2,cell3,cell1,cell2,cell3

The second time around, the |+ setting doesn't seem respected and the last line does not include line breaks

I think my YAML is formatted correctly but happy to be told I'm making YAML mistake.

How would you grab a output from aws cli

See example below

      - id: install-aws-cli
        uses: unfor19/install-aws-cli-action@v1
        with:
         version: 2     # default
         verbose: false # default
         arch: amd64    # allowed values: amd64, arm64
         rootdir: ""    # defaults to "PWD"
         workdir: ""    # defaults to "PWD/unfor19-awscli"
      - run: aws acm list-certificates --output json
        shell: bash

      - name: Overwrite file
        uses: "DamianReeves/write-file-action@master"
        with:
          path: Lists/certificates.json
          write-mode: overwrite
          contents: |
            <PLACEHOLDER>

How to write newlines?

I'd like to use your action to prepend generated changelog text to my CHANGELOG.md file, something like this:

- name: Write to CHANGELOG.md
  uses: DamianReeves/write-file-action@master
  if: ${{ !env.ACT }}
  with:
    path: ./CHANGELOG.md
    contents: ${{ steps.changelog.outputs.changelog }}${{ steps.package.outputs.content }}
    write-mode: overwrite

Except that I want a blank line between the new content and the existing content. I am not familiar enough with the syntax of actions and yaml.. how can add those newlines?

I tried this, but that just literally adds echo and \n\n to my file.

contents: |
  echo ${{ steps.changelog.outputs.changelog }}\n\n${{ steps.package.outputs.content }}

Is the file consistent across steps?

The REAME does not mention whether the file remains persistent across steps in a job. In addition, I think that it should also clarify whether the file remains persistent across jobs.

The action passes… but nothing is written on the file

Hey,

so I’ve managed to make this action work (very basic : go to branch, write something) :

`name: Append File
on:
pull_request:
types: [labeled, unlabeled]

jobs:
build:
name: Append Text
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: writefile
- name: Write File
uses: DamianReeves/[email protected]
with:
path: ${{github.workspace}}/test
contents: |
echo "Hello World!"
write-mode: overwrite`

The action is a success, but the file does not change at all… maybe I need to commit every thing ? I have no clue. If you have an idea I’d love to hear it !

Thanks a lot !

1.3 was a breaking release

Upgrading the node version is a breaking change.
So actually 1.3 should have been 2.0.
I also did this wrong in the past and learned this from my action's users.
If the action is used on a custom self-hosted runner, there is no guarantee that the new Node version is available and so updating the node version is a breaking change.

Feel free to decide how you proceed or whether you ignore the breaking change.
Back then I reverted the change, cut a new release and did the change in a directly following major version release.

Unexpected end of JSON input

My code : P.S., I am trying to print the output in .md file

  - name: Overwrite file
    uses: testing/POC@master
    with:
      file: './Data.md'
      write-mode: overwrite
      contents: |
        "${{ steps.chart-name.outputs.data }}"

Error :

Warning: Unexpected input(s) 'write-mode', 'contents', valid inputs are ['file', 'key-path']
Run testing/POC@master
with:
file: ./Data.md
write-mode: overwrite
contents: "chart name"

Error: Unexpected end of JSON input

@DamianReeves , Could you please help me with the above error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.