Github Actions 在部署之前替换字符串

mic*_*ael 2 yaml version github github-actions building-github-actions

我正在尝试开发一个 Github Action,以在上传到 FTP 服务器之前更新我的(大部分)php 文件中的版本号和数据库凭据。

在我的主分支内,我有一个名为VERSION.mdcontent 的文本文件2.0.1

该操作应该在每次推送提交时运行。

在部署到 FTP 服务器之前,需要进行这些替换(当然,不应更改真正的存储库文件,只更改上传到服务器的文件)。

  • 对于每个文件,该字符串__VERSION__应替换为我的文件的内容VERSION.md
  • 另外,仅对于文件db_connection.php,应将字符串__DB-PASSWORD__替换为相应的密钥DB_PASSWORD

这是我当前的工作流程:

on: push
name: FTP Deploy
jobs:
  web-deploy:
    name: Ubuntu VM
    runs-on: ubuntu-latest
    steps:
    - name: Getting latest Code
      uses: actions/checkout@v2.3.2
    
    # at this position I tried the string replacing

    - name: Sync Files
      uses: SamKirkland/FTP-Deploy-Action@4.0.0
      with:
        server: ${{ secrets.ftp_server }}
        username: ${{ secrets.ftp_username }}
        password: ${{ secrets.ftp_password }}
        server-dir: "public_html/"
        exclude: .git*
          - .git*/**
          - fonts/ttf/**
          - symbols/**
          - README.md
          - VERSION.md
Run Code Online (Sandbox Code Playgroud)

作为第一次测试,我尝试过

- name: Set Tokens
      run: perl -pi -e 's/getString\(R\.string\.token\)/"$ENV{TOKEN}"/' 'index.php'
      env:
        TOKEN: ${{ secrets.TOKEN }}
Run Code Online (Sandbox Code Playgroud)

仅用存储在我的秘密中的令牌替换index.php字符串TOKEN,但这失败了。一切看起来都很好,但是服务器没有变化。

Shi*_*hur 7

你可以用来sed做这个

例如用“bar”替换“foo” -sed -i 's/foo/bar/g' input_file

- name: Replace credentials
  run: |
    find . -name "*" -exec sed -i "s/__VERSION__/$(cat VERSION.md)/g" {} +
    sed -i 's/__DB-PASSWORD__/${{ secrets.DB_PASSWORD }}/g' db_connection.php
Run Code Online (Sandbox Code Playgroud)