How to use GitHub actions to execute your python codes automatically with secrets variables

As I just know, the GitHub actions has a function that could execute your scripts automatically. The next will illustrate how to achieve that:|

step_1 You should be familiar with the basic uses of GitHub actions firstly.

在这里插入图片描述

step_2 You will make a file and write some codes in it.

name: automatic_punch_in 

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  schedule:
    - cron: "10 20 * * 1" # 此处设置为每周一的八点十分触发下面的jobs

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: set up python                
        uses: 'actions/setup-python@v2'
        with:
          python-version: '3.8'

      - name: install modules
        run: 'pip install -r requirements.txt'

      - name: auto checkin
        run: python Punch_in_automatically.py
        env:
          SCKEY: ${{ secrets.SCKEY }} #this codes will make your python scripts could use os.environ.get() get the               
       

If you just want to run your python scripts without any additional variables to use, you don’t need to add env: stuff.
The corresponding code to SCKEY in python is as follows:

user = json.loads(environ["SCKEY"] if 'SCKEY' in environ else '{}')
    print(type(user))
    assert 'userName' in user and 'passWord' in user
    userName = user["userName"]
    passWord = user["passWord"]

It is easy to understand that the username and password are secret info. that we don’t want others know that. But we also hope others could use these codes. The above python code just tell you how to use the variables in secrets.
The following will tell you that how to set the secret variables:
create_secrets_variable
format of value of secret variable

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值