Search code examples
gitgithub-actionsgithub-pages

[GitHub Actions]: Remove files and push changes


Hey I am trying to remove the content of a directory (the directory is called scss) via a GitHub Workflow and push to a seperate branch. I am using actions-js/push@master for the pushing. However, before that I am compressing some other files. So the workflow looks something like this:

  1. Compress HTML, CSS and JS files.
  2. Delete the folder scss and his content.
  3. Push the everything to another branch.

Step 1 & 3 are working perfectly fine however, the scss folder is still there. My yaml files looks like this:

name: Compress Website
...
jobs:
  test:
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04]
    env:
      prepo: ${{github.repository}}
    steps:
      - uses: actions/checkout@v2
      - name: Set up Minify Actions
        uses: amireshoon/minifyAction@main
        with:
          repo: $prepo
      - name: Minifying Codes
        run: |
          minifier.sh $prepo
================== THIS FAILS ================== 
      - name: Removing unnecessary files
        run: |
          ls -la
          rm -rf scss
          ls -la
================================================ 
      - name: Pushing to production branch
        uses: actions-js/push@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          directory: github/workspace
          branch: compressed-master
          message: Minified css, js and html
          author_name: MinifyAction[bot]
          author_email: [email protected]
          force: true

The output of the ls statements shows me that the folder has been deleted but when I look at the pushed files it is still there. Why does step 2 not work?


Solution

  • The comments of @torek and @joanis pointed me to the right direction but the issue lies not with modifying the working tree.

    Although it is true that I am only modifying the working tree the action actions-js/push@master takes care of committing the changes. So modifying the working tree should indeed be enough to see the changes reflected in my new branch. However, I passed the argument directory: github/workspace for the action which switches the directory before pushing.

    First I did not know where this directory comes from (I copied a working.yaml file) but when looking at the source code of the minifying action (amireshoon/minifyAction@main) the picture becomes clearer:

    #!/bin/sh
    
    # Creating workspace
    mkdir github && cd github
    git clone https://github.com/$1.git workspace
    cd workspace
    ...
    

    The minifying action creates said directory by cloning the project again and proceeds to minify the files in that directory. So naturally, all the changes I made in my working tree are not reflected because the push is performed for a different local repository that is contained in github/workspace.

    So to fix the issue you can perform your changes in that directory. Although I ended up using another minifying action that modifies the working tree directly. So all changes are made in the same directory and finally pushed with actions-js/push@master without specifying a special directory.

    My working action looks like this:

    jobs:
      test:
        runs-on: ${{ matrix.os }}
    
        strategy:
          fail-fast: false
          matrix:
            os: [ubuntu-20.04]
        env:
          prepo: ${{github.repository}}
        steps:
          - uses: actions/checkout@v2
          - name: Removing unnecessary files
            run: |
              rm -r scss
              ls -la
          - name: minify css
            uses: nizarmah/[email protected]
            with:
              overwrite: true
              directory: 'css'
          - name: minify js
            uses: nizarmah/[email protected]
            with:
              overwrite: true
              directory: 'js/custom'
          - name: Pushing to production branch
            uses: actions-js/push@master
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              branch: compressed-master
              message: Minified css, js and html
              author_name: MinifyAction[bot]
              author_email: [email protected]
              force: true
    

    This was a really long answer for a really silly mistake but it hopes it helps someone out in the future. 😅